Table of Contents
The new Date() constructor creates a date object with the current date and time:
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Date Methods</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>
<h4>Get the Current Time</h4>
<p id="demo"></p>
<script>
const d = new Date();
document.getElementById("demo").innerHTML = "Current date and time: " + d;
</script>
</body>
</html>

Result View Example
Date get methods return parts of a date. The most common methods:
getFullYear() - year (4 digits)getMonth() - month (0-11)getDate() - day of month (1-31)getDay() - day of week (0=Sunday)getHours() - hours (0-23)getMinutes() - minutes (0-59)getSeconds() - seconds (0-59)<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Date Methods</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>
<h4>Date Get Methods</h4>
<p id="demo"></p>
<script>
const d = new Date(2024, 5, 15, 14, 30, 45);
document.getElementById("demo").innerHTML =
"getFullYear(): " + d.getFullYear() + "<br>" +
"getMonth(): " + d.getMonth() + " (June = 5)<br>" +
"getDate(): " + d.getDate() + "<br>" +
"getDay(): " + d.getDay() + " (Saturday = 6)<br>" +
"getHours(): " + d.getHours() + "<br>" +
"getMinutes(): " + d.getMinutes() + "<br>" +
"getSeconds(): " + d.getSeconds();
</script>
</body>
</html>

Result View Example
Date.now() returns the number of milliseconds since January 1, 1970:
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Date Methods</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>
<h4>The Date.now() Method</h4>
<p id="demo"></p>
<script>
// Date.now() returns milliseconds since Jan 1, 1970
const ms = Date.now();
const date = new Date(ms);
// Also getTime() returns the same
const d = new Date();
const time = d.getTime();
document.getElementById("demo").innerHTML =
"Date.now(): " + ms + " ms<br>" +
"new Date(Date.now()): " + date + "<br><br>" +
"d.getTime(): " + time + " ms<br>" +
"Date.now() === d.getTime(): " + (ms === time);
</script>
</body>
</html>

Result View Example
UTC methods return date parts according to UTC (Coordinated Universal Time):
getUTCFullYear() - year in UTCgetUTCMonth() - month in UTCgetUTCDate() - day in UTCgetUTCDay() - weekday in UTCgetUTCHours() - hours in UTC<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Date Methods</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>
<h4>UTC Date Get Methods</h4>
<p id="demo"></p>
<script>
const d = new Date();
document.getElementById("demo").innerHTML =
"Local vs UTC:<br><br>" +
"getHours(): " + d.getHours() + " (local)<br>" +
"getUTCHours(): " + d.getUTCHours() + " (UTC)<br><br>" +
"getFullYear(): " + d.getFullYear() + " (local)<br>" +
"getUTCFullYear(): " + d.getUTCFullYear() + " (UTC)<br><br>" +
"getMonth(): " + d.getMonth() + " (local)<br>" +
"getUTCMonth(): " + d.getUTCMonth() + " (UTC)<br><br>" +
"UTC methods use the universal time standard";
</script>
</body>
</html>

Result View Example
getTimezoneOffset() returns the difference (in minutes) between local time and UTC time:
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Date Methods</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>
<h4>The getTimezoneOffset() Method</h4>
<p id="demo"></p>
<script>
const d = new Date();
// Returns the timezone offset in minutes
const offset = d.getTimezoneOffset();
// Convert to hours
const offsetHours = offset / 60;
document.getElementById("demo").innerHTML =
"getTimezoneOffset(): " + offset + " minutes<br>" +
"Offset in hours: " + offsetHours + " hours<br><br>" +
"This is the difference between your local time and UTC. " +
"For example, Bangkok (UTC+7) has an offset of -420 minutes.";
</script>
</body>
</html>

Result View Example
Document in project
You can Download PDF file.