JavaScript Tarih Alma Yöntemleri
Bu yöntemler, bir tarih nesnesinden bilgi almak için kullanılabilir:
Method | Description |
---|---|
getFullYear() | Get the year as a four digit number (yyyy) |
getMonth() | Get the month as a number (0-11) |
getDate() | Get the day as a number (1-31) |
getHours() | Get the hour (0-23) |
getMinutes() | Get the minute (0-59) |
getSeconds() | Get the second (0-59) |
getMilliseconds() | Get the millisecond (0-999) |
getTime() | Get the time (milliseconds since January 1, 1970) |
getDay() | Get the weekday as a number (0-6) |
Date.now() | Get the time. ECMAScript 5. |
getTime() Yöntemi
Yöntem getTime()
, 1 Ocak 1970'den bu yana geçen milisaniye sayısını döndürür:
Örnek
const d = new Date();
d.getTime();
getFullYear() Yöntemi
Yöntem getFullYear()
, bir tarihin yılını dört basamaklı bir sayı olarak döndürür:
Örnek
const d = new Date();
d.getFullYear();
getMonth() Yöntemi
Yöntem getMonth()
, bir tarihin ayını bir sayı (0-11) olarak döndürür:
Örnek
const d = new Date();
d.getMonth();
JavaScript'te ilk ay (Ocak) 0 aydır, bu nedenle Aralık ayı 11 sayısını döndürür.
Bir dizi ad kullanabilir getMonth()
ve ayı bir ad olarak döndürebilirsiniz:
Örnek
const months = ["January", "February", "March", "April", "May",
"June", "July", "August", "September", "October",
"November", "December"];
const d = new Date();
let month = months[d.getMonth()];
getDate() Yöntemi
Yöntem getDate()
, bir tarihin gününü bir sayı (1-31) olarak döndürür:
Örnek
const d = new Date();
d.getDate();
getHours() Yöntemi
Yöntem getHours()
, bir tarihin saatlerini bir sayı (0-23) olarak döndürür:
Örnek
const d = new Date();
d.getHours();
getMinutes() Yöntemi
Yöntem getMinutes()
, bir tarihin dakikalarını bir sayı (0-59) olarak döndürür:
Örnek
const d = new Date();
d.getMinutes();
getSeconds() Yöntemi
Yöntem getSeconds()
, bir tarihin saniyelerini bir sayı (0-59) olarak döndürür:
Örnek
const d = new Date();
d.getSeconds();
getMilliseconds() Yöntemi
Yöntem getMilliseconds()
, bir tarihin milisaniyesini bir sayı (0-999) olarak döndürür:
Örnek
const d = new Date();
d.getMilliseconds();
getDay() Yöntemi
Yöntem getDay()
, bir tarihin hafta içi gününü bir sayı (0-6) olarak döndürür:
Örnek
const d = new Date();
d.getDay();
JavaScript'te, dünyadaki bazı ülkeler haftanın ilk gününü "Pazartesi" olarak kabul etse bile, haftanın ilk günü (0) "Pazar" anlamına gelir.
Bir dizi ad kullanabilir getDay()
ve haftanın gününü bir ad olarak döndürebilirsiniz:
Örnek
const days = ["Sunday", "Monday", "Tuesday", "Wednesday",
"Thursday", "Friday", "Saturday"];
const d = new Date();
let day = days[d.getDay()];
UTC Tarih Yöntemleri
UTC tarih yöntemleri, UTC tarihleriyle çalışmak için kullanılır (Evrensel Saat Dilimi tarihleri):
Method | Description |
---|---|
getUTCDate() | Same as getDate(), but returns the UTC date |
getUTCDay() | Same as getDay(), but returns the UTC day |
getUTCFullYear() | Same as getFullYear(), but returns the UTC year |
getUTCHours() | Same as getHours(), but returns the UTC hour |
getUTCMilliseconds() | Same as getMilliseconds(), but returns the UTC milliseconds |
getUTCMinutes() | Same as getMinutes(), but returns the UTC minutes |
getUTCMonth() | Same as getMonth(), but returns the UTC month |
getUTCSeconds() | Same as getSeconds(), but returns the UTC seconds |
JavaScript Tarih Referansını Tamamlayın
Tam bir Tarih referansı için şuraya gidin:
JavaScript Tarih Referansını tamamlayın .
Başvuru, tüm Date özelliklerinin ve yöntemlerinin açıklamalarını ve örneklerini içerir.