Bu eğitimde, örnekler yardımıyla JavaScript'te tarih ve saat hakkında bilgi edineceksiniz.
JavaScript'te tarih ve saat Date
nesne tarafından temsil edilir . Date
Nesne tarih ve saat bilgilerini sağlar ve aynı zamanda çeşitli yöntemler sağlar.
Bir JavaScript tarihi, 1 Ocak 1970 UTC'den bu yana geçen milisaniyeleri temsil eden EcmaScript dönemini tanımlar . Bu tarih ve saat, UNIX dönemi ile aynıdır (bilgisayarda kaydedilen tarih ve saat değerleri için baskın temel değer).
Tarih Nesneleri Oluşturma
Bir tarih nesnesi oluşturmanın dört yolu vardır.
- yeni tarih()
- yeni Tarih (milisaniye)
- yeni Tarih (Tarih dizesi)
- yeni Tarih (yıl, ay, gün, saat, dakika, saniye, milisaniye)
yeni tarih()
Yapıcıyı kullanarak bir tarih nesnesi oluşturabilirsiniz new Date()
. Örneğin,
const timeNow = new Date(); console.log(timeNow); // shows current date and time
Çıktı
Pzt 06 Tem 2020 2020 12:03:49 GMT + 0545 (Nepal Saati)
Burada, new Date()
geçerli tarih ve yerel saat ile yeni bir tarih nesnesi oluşturur.
yeni Tarih (milisaniye)
Date
Nesne beri milisaniye temsil eden bir dizi içerir 1 Ocak 1970 UTC .
new Date(milliseconds)
milisaniyeleri sıfır zamana ekleyerek yeni bir tarih nesnesi oluşturur. Örneğin,
const time1 = new Date(0); // epoch time console.log(time1); // Thu Jan 01 1970 05:30:00 // 100000000000 milliseconds after the epoch time const time2 = new Date(100000000000) console.log(time2); // Sat Mar 03 1973 15:16:40
Not : 1000 milisaniye 1 saniyeye eşittir.
yeni Tarih (tarih dizesi)
new Date(date string)
bir tarih dizesinden yeni bir tarih nesnesi oluşturur.
JavaScript'te genellikle üç tarih giriş biçimi vardır.
ISO Tarih Biçimleri
ISO tarih formatlarını ileterek bir tarih nesnesi oluşturabilirsiniz. Örneğin,
// ISO Date(International Standard) const date = new Date("2020-07-01"); // the result date will be according to UTC console.log(date); // Wed Jul 01 2020 05:45:00 GMT+0545
Ayrıca yalnızca yılı ve ayı veya yalnızca yılı geçebilirsiniz. Örneğin,
const date = new Date("2020-07"); console.log(date); // Wed Jul 01 2020 05:45:00 GMT+0545 const date1 = new Date("2020"); console.log(date1); // Wed Jul 01 2020 05:45:00 GMT+0545
ISO tarihlerine de belirli bir zaman geçirebilirsiniz.
const date = new Date("2020-07-01T12:00:00Z"); console.log(date); // Wed Jul 01 2020 17:45:00 GMT+0545
Not : Tarih ve saat büyük T harfiyle ayrılmıştır . Ve UTC zamanı büyük Z ile tanımlanır .
Kısa ve Uzun tarih biçimi
Diğer iki tarih biçimi kısa tarih biçimi ve uzun tarih biçimidir .
// short date format "MM/DD/YYYY" const date = new Date("03/25/2015"); console.log(date); // Wed Mar 25 2015 00:00:00 GMT+0545 // long date format "MMM DD YYYY" const date1 = new Date("Jul 1 2020"); console.log(date1); // Wed Jul 01 2020 00:00:00 GMT+0545 // month and day can be in any order const date2 = new Date("1 Jul 2020"); console.log(date2); // Wed Jul 01 2020 00:00:00 GMT+0545 // month can be full or abbreviated. Also month names are insensitive. // comma are ignored const date3 = new Date("July 1 2020"); console.log(date3); // Wed Jul 01 2020 00:00:00 GMT+0545 const date4 = new Date("JULY, 1, 2020"); console.log(date4); // Wed Jul 01 2020 00:00:00
yeni Tarih (yıl, ay, gün, saat, dakika, saniye, milisaniye)
new Date(year, month,… )
belirli bir tarih ve saati geçerek yeni bir tarih nesnesi oluşturur. Örneğin,
const time1 = new Date(2020, 1, 20, 4, 12, 11, 0); console.log(time1); // Thu Feb 20 2020 04:12:11
Aktarılan bağımsız değişkenin belirli bir sırası vardır.
Dört sayı geçilirse, yıl, ay, gün ve saatleri temsil eder. Örneğin,
const time1 = new Date(2020, 1, 20, 4); console.log(time1); // Thu Feb 20 2020 04:00:00
Benzer şekilde, iki bağımsız değişken aktarılırsa, yıl ve ayı temsil eder. Örneğin,
const time1 = new Date(2020, 1); console.log(time1); // Sat Feb 01 2020 00:00:00
Not : Yalnızca bir bağımsız değişken iletirseniz, milisaniye olarak kabul edilir. Bu nedenle, bu tarih biçimini kullanmak için iki bağımsız değişken iletmeniz gerekir.
JavaScript'te aylar 0'dan 11'e kadar sayılır . Ocak 0 ve Aralık 11 .
JavaScript Tarih Yöntemleri
JavaScript Date nesnesinde kullanılabilen çeşitli yöntemler vardır.
Yöntem | Açıklama |
---|---|
şimdi () | Geçerli saate karşılık gelen sayısal değeri verir (1 Ocak 1970 00:00:00 UTC'den bu yana geçen milisaniye sayısı) |
getFullYear () | Yılı yerel saate göre alır |
getMonth () | Yerel saate göre 0'dan 11'e kadar ayı alır |
getDate () | Yerel saate göre ayın gününü (1-31) alır |
getDay () | Yerel saate göre haftanın gününü (0-6) alır |
getHours () | Yerel saate göre saati 0'dan 23'e alır |
getMinutes | Gets the minute from 0 to 59 according to local time |
getUTCDate() | Gets the day of the month (1-31) according to universal time |
setFullYear() | Sets the full year according to local time |
setMonth() | Sets the month according to local time |
setDate() | Sets the day of the month according to local time |
setUTCDate() | Sets the day of the month according to universal time |
Example: Date Methods
const timeInMilliseconds = Date.now(); console.log(timeInMilliseconds); // 1593765214488 const time = new Date; // get day of the month const date = time.getDate(); console.log(date); // 30 // get day of the week const year = time.getFullYear(); console.log(year); // 2020 const utcDate = time.getUTCDate(); console.log(utcDate); // 30 const event = new Date('Feb 19, 2020 23:15:30'); // set the date event.setDate(15); console.log(event.getDate()); // 15 // Only 28 days in February! event.setDate(35); console.log(event.getDate()); // 7
Formatting a Date
Unlike other programming languages, JavaScript does not provide a built-in function for formatting a date.
However, you can extract individual bits and use it like this.
const currentDate = new Date(); const date = currentDate.getDate(); const month = currentDate.getMonth(); const year = currentDate.getFullYear(); // show in specific format let monthDateYear = (month+1) + '/' + date + '/' + year; console.log(monthDateYear); // 7/3/2020
Note: The above program gives inconsistent length as date and month have single-digit and double-digit.
AutoCorrection in Date Object
When you assign out of range values in the Date
object, it auto-corrects itself. For example,
const date = new Date(2008, 0, 33); // Jan does not have 33 days console.log(date);
Output
Sat Feb 02 2008
JavaScript'teki tarih ve saat hakkında daha fazla bilgi edinmek için Tarih ve Saati Aydınlatmak sayfasını ziyaret edin.