JavaScript Date Methods

Back to JS page

Table of Contents

Get the Current Time

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>

Example 1

Result View Example

Date Get Methods

Date get methods return parts of a date. The most common methods:

<!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>

Example 2

Result View Example

The Date.now() Method

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>

Example 3

Result View Example

UTC Date Get Methods

UTC methods return date parts according to UTC (Coordinated Universal Time):

<!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>

Example 4

Result View Example

The getTimezoneOffset() Method

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>

Example 5

Result View Example

Document

Document in project

You can Download PDF file.

Reference