Docs/JavaScript/Date & Time

Date & Time

Membuat Date

  • new Date() — sekarang
  • new Date(year, month, day) — month mulai dari 0!
  • new Date("2024-01-15") — dari string

Methods

  • .getFullYear(), .getMonth(), .getDate()
  • .getHours(), .getMinutes(), .getSeconds()
  • .toLocaleDateString(), .toLocaleTimeString()
  • Date.now() — timestamp ms
script.js
Try It →
const now = new Date();
const o = document.getElementById('output');
o.innerHTML = 
  'new Date() → '+now.toString()+'

'+
  '.toLocaleDateString("id-ID")
→ '+now.toLocaleDateString("id-ID",{weekday:"long",year:"numeric",month:"long",day:"numeric"})+'

'+
  '.toLocaleTimeString()
→ '+now.toLocaleTimeString()+'

'+
  'Year: '+now.getFullYear()+'
'+
  'Month: '+now.getMonth()+' (0-indexed!)
'+
  'Date: '+now.getDate()+'
'+
  'Day: '+now.getDay()+' (0=Sunday)

'+
  'Date.now() → '+Date.now()+' ms';