alert(dateObj) verir Wed Dec 30 2009 00:00:00 GMT+0800
Tarih biçiminde nasıl edinilir 2009/12/30?
alert(dateObj) verir Wed Dec 30 2009 00:00:00 GMT+0800
Tarih biçiminde nasıl edinilir 2009/12/30?
Yanıtlar:
var dateObj = new Date();
var month = dateObj.getUTCMonth() + 1; //months from 1-12
var day = dateObj.getUTCDate();
var year = dateObj.getUTCFullYear();
newdate = year + "/" + month + "/" + day;
veya yeni bir tarih ayarlayabilir ve yukarıdaki değerleri verebilirsiniz
getMonthve getUTCMonth?
getUTCDay()değiştirilmesi gerektiğini getUTCDate()günü haftasında (0-6) ve tarihi günün numarası ayda günde (1-31) sayısı olan olduğundan,.
var dt = new Date();
dt.getFullYear() + "/" + (dt.getMonth() + 1) + "/" + dt.getDate();
Ay endeksi 0 tabanlı olduğundan, 1 artırmanız gerekir.
Düzenle
Tarih nesnesi işlevlerinin tam listesi için bkz.
getMonth()
Yerel saate göre belirtilen tarihte ayı (0-11) döndürür.
getUTCMonth()
Evrensel saate göre belirtilen tarihte ayı (0-11) döndürür.
new Date().toISOString()
"2016-02-18T23:59:48.039Z"
new Date().toISOString().split('T')[0];
"2016-02-18"
new Date().toISOString().replace('-', '/').split('T')[0].replace('-', '/');
"2016/02/18"
new Date().toLocaleString().split(',')[0]
"2/18/2016"
Moment.js'yi kullanmanızı öneririm http://momentjs.com/
Sonra şunları yapabilirsiniz:
moment(new Date()).format("YYYY/MM/DD");
Not: new Date()Mevcut TimeDate'i istiyorsanız gerçekten eklemeniz gerekmez, bunu sadece bir tarih nesnesi iletebileceğiniz bir referans olarak ekledim. geçerli TimeDate için bu da işe yarar:
moment().format("YYYY/MM/DD");
bilgi
2 haneli bir ay ve tarih isteniyorsa (2016/01/01 - 2016/1/1)
kod
var dateObj = new Date();
var month = ('0' + (dateObj.getMonth() + 1)).slice(-2);
var date = ('0' + dateObj.getDate()).slice(-2);
var year = dateObj.getFullYear();
var shortDate = year + '/' + month + '/' + date;
alert(shortDate);
çıktı
2016/10/06
Vaktini boşa harcamak
https://jsfiddle.net/Hastig/1xuu7z7h/
kredi
Ve sitesinden daha fazla bilgi Bu yanıttan bu cevaba verilen kredi
Daha
W3schools'ta kendiniz denemek hakkında daha fazla bilgi edinmek .slice, onu nasıl kullanacağımı daha iyi anlamama yardımcı oldu.
Güzel biçimlendirme eklentisi: http://blog.stevenlevithan.com/archives/date-time-format .
Bununla yazabilirsiniz:
var now = new Date();
now.format("yyyy/mm/dd");
Date get yöntemlerini kullanın.
http://www.tizag.com/javascriptT/javascriptdate.php
http://www.htmlgoodies.com/beyond/javascript/article.php/3470841
var dateobj= new Date() ;
var month = dateobj.getMonth() + 1;
var day = dateobj.getDate() ;
var year = dateobj.getFullYear();
AVRUPA (İNGİLİZCE / İSPANYOLCA BİÇİMLENDİRME)
Şimdiki günü de almam gerekiyor, bunu kullanabilirsiniz.
function getFormattedDate(today)
{
var week = new Array('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday');
var day = week[today.getDay()];
var dd = today.getDate();
var mm = today.getMonth()+1; //January is 0!
var yyyy = today.getFullYear();
var hour = today.getHours();
var minu = today.getMinutes();
if(dd<10) { dd='0'+dd }
if(mm<10) { mm='0'+mm }
if(minu<10){ minu='0'+minu }
return day+' - '+dd+'/'+mm+'/'+yyyy+' '+hour+':'+minu;
}
var date = new Date();
var text = getFormattedDate(date);
* İspanyolca formatı için sadece WEEK değişkenini çevirin.
var week = new Array('Domingo', 'Lunes', 'Martes', 'Miércoles', 'Jueves', 'Viernes', 'Sábado');
Çıktı: Pazartesi - 16/11/2015 14:24
Neden basit değil?
dateObj.toISOString().slice(0, 10) // YYYY-MM-DD
Burayı kontrol et:
const date1 = new Date('December 17, 1995 03:24:00');
console.log(date1.toISOString().slice(0, 10))
İşte şablon değişmezleriyle Yıl / Ay / Gün elde etmenin daha temiz bir yolu:
var date = new Date();
var formattedDate = `${date.getFullYear()}/${(date.getMonth() + 1)}/${date.getDate()}`;
console.log(formattedDate);
let dateObj = new Date();
let myDate = (dateObj.getUTCFullYear()) + "/" + (dateObj.getMonth() + 1)+ "/" + (dateObj.getUTCDate());
Referans için aşağıdaki ayrıntıları görebilirsiniz
new Date().getDate() // Return the day as a number (1-31)
new Date().getDay() // Return the weekday as a number (0-6)
new Date().getFullYear() // Return the four digit year (yyyy)
new Date().getHours() // Return the hour (0-23)
new Date().getMilliseconds() // Return the milliseconds (0-999)
new Date().getMinutes() // Return the minutes (0-59)
new Date().getMonth() // Return the month (0-11)
new Date().getSeconds() // Return the seconds (0-59)
new Date().getTime() // Return the time (milliseconds since January 1, 1970)
let dateObj = new Date();
let myDate = (dateObj.getUTCFullYear()) + "/" + (dateObj.getMonth() + 1)+ "/" + (dateObj.getUTCDate());
console.log(myDate)
// Dakikaları (0-59) yeni Tarih () döndürün. GetMonth () // Ayı (0-11) yeni Tarih () döndürün. GetSeconds () // Saniyeleri (0-59) yeni Tarih () döndürün .getTime () // Zamanı döndür (1 Ocak 1970'den bu yana milisaniye)
Tarihi yıl-ay-tarih biçiminde almak için Bu tek satır kodunu kullanabilirsiniz
var date = new Date().getFullYear() + "-" + new Date().getMonth() + 1 + "-" + new Date().getDate();
Bir tarih obj veya js zaman damgası geçirirseniz çalışır bu kullanıyorum:
getHumanReadableDate: function(date) {
if (date instanceof Date) {
return date.getDate() + "/" + (date.getMonth() + 1) + "/" + date.getFullYear();
} else if (isFinite(date)) {//timestamp
var d = new Date();
d.setTime(date);
return this.getHumanReadableDate(d);
}
}
ES2018, gün, ay ve yılı yakalamak için kullanabileceğiniz normal ifade yakalama gruplarını tanıttı:
const REGEX = /(?<year>[0-9]{4})-(?<month>[0-9]{2})-(?<day>[0-9]{2});
const results = REGEX.exec('2018-07-12');
console.log(results.groups.year);
console.log(results.groups.month);
console.log(results.groups.day);
Bu yaklaşımın avantajı, standart olmayan dize tarih biçimleri için gün, ay, yıl yakalama olasılığıdır.
Ref. https://www.freecodecamp.org/news/es9-javascripts-state-of-art-in-2018-9a350643f29c/