JavaScript Ayar Tarihi Yöntemleri
Tarihi Ayarla yöntemleri, bir Tarih Nesnesi için tarih değerlerini (yıllar, aylar, günler, saatler, dakikalar, saniyeler, milisaniyeler) ayarlamanıza izin verir.
Tarih Yöntemlerini Ayarla
Tarihi Ayarla yöntemleri, bir tarihin bir bölümünü ayarlamak için kullanılır:
Method | Description |
---|---|
setDate() | Set the day as a number (1-31) |
setFullYear() | Set the year (optionally month and day) |
setHours() | Set the hour (0-23) |
setMilliseconds() | Set the milliseconds (0-999) |
setMinutes() | Set the minutes (0-59) |
setMonth() | Set the month (0-11) |
setSeconds() | Set the seconds (0-59) |
setTime() | Set the time (milliseconds since January 1, 1970) |
setFullYear() Yöntemi
Yöntem setFullYear()
, bir tarih nesnesinin yılını ayarlar. Bu örnekte 2020'ye:
Örnek
const d = new Date();
d.setFullYear(2020);
Yöntem setFullYear()
isteğe bağlı olarak ay ve günü ayarlayabilir:
Örnek
const d = new Date();
d.setFullYear(2020, 11, 3);
setMonth() Yöntemi
Yöntem setMonth()
, bir tarih nesnesinin (0-11) ayını ayarlar:
Örnek
const d = new Date();
d.setMonth(11);
setDate() Yöntemi
Yöntem setDate()
, bir tarih nesnesinin (1-31) gününü ayarlar:
Örnek
const d = new Date();
d.setDate(15);
Yöntem ayrıca bir tarihe gün eklemeksetDate()
için de kullanılabilir :
Örnek
const d = new Date();
d.setDate(d.getDate() + 50);
Günlerin eklenmesi ayı veya yılı değiştirirse, değişiklikler Date nesnesi tarafından otomatik olarak işlenir.
setHours() Yöntemi
Yöntem setHours()
, bir tarih nesnesinin (0-23) saatlerini ayarlar:
Örnek
const d = new Date();
d.setHours(22);
setMinutes() Yöntemi
Yöntem setMinutes()
, bir tarih nesnesinin (0-59) dakikalarını ayarlar:
Örnek
const d = new Date();
d.setMinutes(30);
setSeconds() Yöntemi
Yöntem setSeconds()
, bir tarih nesnesinin (0-59) saniyelerini ayarlar:
Örnek
const d = new Date();
d.setSeconds(30);
Tarihleri Karşılaştır
Tarihler kolayca karşılaştırılabilir.
Aşağıdaki örnek, bugünün tarihini 14 Ocak 2100 ile karşılaştırır:
Örnek
let text = "";
const today = new Date();
const someday = new Date();
someday.setFullYear(2100, 0, 14);
if (someday > today) {
text = "Today is before January 14, 2100.";
} else {
text = "Today is after January 14, 2100.";
}
JavaScript, 0'dan 11'e kadar olan ayları sayar. Ocak, 0. Aralık, 11'dir.
JavaScript Tarih Referansını Tamamlayın
Tam bir Tarih referansı için şuraya gidin:
JavaScript Tarih Referansını tamamlayın .
Referans, tüm Date özelliklerinin ve yöntemlerinin açıklamalarını ve örneklerini içerir.