Tarihi Ayarla yöntemleri tarih değerlerini (yıl, Bir Tarih Nesnesi için aylar, günler, saatler, dakikalar, saniyeler, milisaniyeler).
Tarihi Ayarla yöntemleri, tarihin bir kısmını ayarlamak için kullanılır:
Günü sayı olarak ayarlayın (1-31)
Yılı ayarlayın (isteğe bağlı olarak ay ve gün)
Saati ayarlayın (0-23)
Milisaniyeyi ayarlayın (0-999)
Dakikayı ayarlayın (0-59)
Ayı ayarlayın (0-11)
Saniyeleri ayarlayın (0-59)
Saati ayarlayın (1 Ocak 1970'ten bu yana milisaniye)
setFullYear() yöntemi, bir tarih nesnesinin yılını ayarlar. Bu örnekte 2020'ye:
const d = new Date();
d.setFullYear(2020);Kendiniz Deneyin →
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript setFullYear()</h2>
<p>The setFullYear() method sets the year of a date object:</p>
<p id="demo"></p>
<script>
const d = new Date();
d.setFullYear(2020);
document.getElementById("demo").innerHTML = d;
</script>
</body>
</html>
setFullYear() yöntemi isteğe bağlı olarak ayı ve günü ayarlayabilir:
const d = new Date();
d.setFullYear(2020, 11, 3);Kendiniz Deneyin →
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript setFullYear()</h2>
<p>The setFullYear() method can optionally set month and day.</p>
<p>Please note that month counts from 0. December is month 11:</p>
<p id="demo"></p>
<script>
const d = new Date();
d.setFullYear(2020, 11, 3);
document.getElementById("demo").innerHTML = d;
</script>
</body>
</html>
setMonth() yöntemi, bir tarih nesnesinin ayını ayarlar (0-11):
const d = new Date();
d.setMonth(11);Kendiniz Deneyin →
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript setMonth()</h2>
<p>The setMonth() method sets the mont of a date object.</p>
<p>Note that months count from 0. December is month 11:</p>
<p id="demo"></p>
<script>
const d = new Date();
d.setMonth(11);
document.getElementById("demo").innerHTML = d;
</script>
</body>
</html>
setDate() yöntemi, bir tarih nesnesinin gününü ayarlar (1-31):
const d = new Date();
d.setDate(15);Kendiniz Deneyin →
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript setDate()</h2>
<p>The setDate() method sets the day of a date object:</p>
<p id="demo"></p>
<script>
const d = new Date();
d.setDate(15);
document.getElementById("demo").innerHTML = d;
</script>
</body>
</html>
setDate() yöntemi aynı zamanda bir tarihe gün eklemek için de kullanılabilir:
const d = new Date();
d.setDate(d.getDate() + 50);Kendiniz Deneyin →
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript setDate()</h2>
<p>The setDate() method can be used to add days to a date.</p>
<p id="demo"></p>
<script>
const d = new Date();
d.setDate(d.getDate() + 50);
document.getElementById("demo").innerHTML = d;
</script>
</body>
</html>
Günlerin eklenmesi ayı veya yılı kaydırırsa değişiklikler Date nesnesi tarafından otomatik olarak gerçekleştirilir.
setHours() yöntemi bir tarih nesnesinin saatlerini ayarlar (0-23):
const d = new Date();
d.setHours(22);Kendiniz Deneyin →
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript setHours()</h2>
<p>The setHours() method sets the hours of a date object:</p>
<p id="demo"></p>
<script>
const d = new Date();
d.setHours(22);
document.getElementById("demo").innerHTML = d;
</script>
</body>
</html>
setMinutes() yöntemi, bir tarih nesnesinin (0-59) dakikalarını ayarlar:
const d = new Date();
d.setMinutes(30);Kendiniz Deneyin →
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript setMinutes()</h2>
<p>The setMinutes() method sets the minutes of a date object (0-59):</p>
<p id="demo"></p>
<script>
const d = new Date();
d.setMinutes(30);
document.getElementById("demo").innerHTML = d;
</script>
</body>
</html>
setSeconds() yöntemi bir tarih nesnesinin saniyelerini ayarlar (0-59):
const d = new Date();
d.setSeconds(30);Kendiniz Deneyin →
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript setSeconds()</h2>
<p>The setSeconds() method sets the seconds of a date object (0-59):</p>
<p id="demo"></p>
<script>
const d = new Date();
d.setSeconds(30);
document.getElementById("demo").innerHTML = d;
</script>
</body>
</html>
Tarihler kolayca karşılaştırılabilir.
Aşağıdaki örnek, bugünün tarihini 14 Ocak 2100 ile karşılaştırmaktadır:
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.";
}Kendiniz Deneyin →
<!DOCTYPE html>
<html>
<body>
<p id="demo"></p>
<script>
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.";
}
document.getElementById("demo").innerHTML = text;
</script>
</body>
</html>
JavaScript 0'dan 11'e kadar olan ayları sayar. Ocak 0'dır. Aralık ise 11'dir.
Tam bir Tarih referansı için şu adrese gidin:
JavaScript Tarih Referansını Tamamlayın.
Referans, tüm Tarih özelliklerinin açıklamalarını ve örneklerini içerir ve yöntemler.