Skip to content

Instantly share code, notes, and snippets.

@fanatikhamsi
Last active January 27, 2023 08:50
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fanatikhamsi/31c052240bb6b7c8a46e71fb210933b9 to your computer and use it in GitHub Desktop.
Save fanatikhamsi/31c052240bb6b7c8a46e71fb210933b9 to your computer and use it in GitHub Desktop.
Javascriptte tarih formatına göre Türkçe ay ve gün formatlarını da destekleyen js eklentisidir.
*
Based on fanatikhamsi code https://github.com/fanatikhamsi/
Contributors:
Selim Çoban - @fanatikhamsi
------------------
Örnek:
var simdikiTarih = new Date();
simdikiTarih.toTurkishFormatDate("dd.mm.yyyy")
Çıktı: 14.02.2016
simdikiTarih.toTurkishFormatDate("dd MM DD yyyy")
Çıktı: 14 Şubat Pazar 2016
simdikiTarih.toTurkishFormatDate("dd MM yyyy")
Çıktı: 14 Şubat 2016
*/
Date.prototype.toTurkishFormatDate = function(format) {
var date = this,
day = date.getDate(),
weekDay = date.getDay(),
month = date.getMonth() + 1,
year = date.getFullYear(),
hours = date.getHours(),
minutes = date.getMinutes(),
seconds = date.getSeconds();
var monthNames = new Array("Ocak", "Şubat", "Mart", "Nisan", "Mayıs", "Haziran", "Temmuz", "Ağustos", "Eylül", "Ekim", "Kasım", "Aralık");
var dayNames = new Array("Pazar", "Pazartesi", "Salı", "Çarşamba", "Perşembe", "Cuma", "Cumartesi");
if (!format) {
format = "dd.MM.yyyy";
}
format = format.replace("mm", month.toString().padStart(2, "0"));
format = format.replace("MM", monthNames[month]);
if (format.indexOf("yyyy") > -1) {
format = format.replace("yyyy", year.toString());
} else if (format.indexOf("yy") > -1) {
format = format.replace("yy", year.toString().substr(2, 2));
}
format = format.replace("dd", day.toString().padStart(2, "0"));
format = format.replace("DD", dayNames[weekDay]);
if (format.indexOf("HH") > -1) {
format = format.replace("HH", hours.toString().replace(/^(\d)$/, '0$1'));
}
if (format.indexOf("hh") > -1) {
if (hours > 12) {
hours -= 12;
}
if (hours === 0) {
hours = 12;
}
format = format.replace("hh", hours.toString().replace(/^(\d)$/, '0$1'));
}
if (format.indexOf("ii") > -1) {
format = format.replace("ii", minutes.toString().replace(/^(\d)$/, '0$1'));
}
if (format.indexOf("ss") > -1) {
format = format.replace("ss", seconds.toString().replace(/^(\d)$/, '0$1'));
}
return format;
};
@davutkara
Copy link

Merhaba paylaşım için teşekkürler, yalnız 34 ve 44. satırlarda padL metodu hata verdi doğrusu padStart sanırım.

@rmzncvk
Copy link

rmzncvk commented Nov 23, 2018

çalışmıyor

@edips
Copy link

edips commented Mar 3, 2021

padL hatası. Zaman kaybetmeyin.

@fanatikhamsi
Copy link
Author

@edips padL -> padStart olarak revize edildi

@qinglongoki
Copy link

Ay yanlış geliyor, format = format.replace("MM", monthNames[month - 1]); yapınız.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment