JavaScript Date and Time Functions — PBA Institute Tutorial
Chapter 09 · JavaScript Programming Series
12 min read Beginner

JavaScript Date & Time Functions

JavaScript provides a powerful built-in Date object to work with dates, times, and timestamps. You can create, read, format, and compute differences between dates easily.

What is the Date Object?

The Date object represents a moment in time. It stores the number of milliseconds since 1 January 1970 (UTC) and provides methods to read or change individual parts like year, month, hours and minutes.

Ways to Create a Date

Current Date

new Date() – right now.

📅

From String

new Date("2025-08-15") – ISO format.

🔢

From Numbers

new Date(2025, 7, 15, 10, 30)

From Timestamp

new Date(1700000000000)

🛠️

Date.now()

Returns current timestamp in ms.

🌍

UTC

new Date(Date.UTC(2025,7,15))

Basic Features

  • Months are 0-indexed: January = 0, December = 11.
  • Days of week from getDay(): Sunday = 0, Saturday = 6.
  • getTime() returns milliseconds since 1 Jan 1970 UTC.
  • toLocaleDateString() formats by locale automatically.

Common Date Methods

MethodReturnsExample
getFullYear()Year (4 digits)2025
getMonth()Month (0–11)7 (August)
getDate()Day of month (1–31)15
getDay()Day of week (0–6)5 (Friday)
getHours()Hour (0–23)10
getMinutes()Minute (0–59)30
getSeconds()Seconds (0–59)45
getTime()Timestamp ms1755250200000
toLocaleDateString()Locale date15/8/2025
toLocaleTimeString()Locale time10:30:45 AM

Exploring Date & Time Functions

In JavaScript, Date and Time functions are used to create, manage, manipulate, and format dates and times in web applications. They help developers build clocks, calendars, countdowns, schedulers, timers, and many real-time systems.

Importance of Date & Time Functions

  • Handle time-sensitive data.
  • Create clocks and countdowns.
  • Manage appointments and schedules.
  • Support localization and timezones.
  • Useful in dashboards and analytics.

Important Date Functions

Function Description
new Date() Creates current date object.
getDate() Returns day of month.
getDay() Returns weekday number.
getMonth() Returns month value.
getFullYear() Returns full year.
getHours() Returns current hour.
getMinutes() Returns current minutes.
getSeconds() Returns current seconds.
Date.now() Returns timestamp.

Your First Date Program

Show Current Date & Time
<script>

let d = new Date();

document.write(

"Today is: " + d

);

</script>
Output Today is: Fri Aug 15 2025 10:30:45 GMT+0530

Example 1 : Format DD/MM/YYYY

Date Formatting
<script>

let d = new Date();

let day =
d.getDate();

let mon =
d.getMonth() + 1;

let yr =
d.getFullYear();

document.write(

day + "/" +

mon + "/" +

yr

);

</script>
Output 15/8/2025

Example 2 : Day Name

Display Day Name
<script>

let days = [

"Sun","Mon",
"Tue","Wed",
"Thu","Fri",
"Sat"

];

let d =
new Date();

document.write(

"Today is " +

days[d.getDay()]

);

</script>
Output Today is Fri

Example 3 : Current Time

HH : MM : SS
<script>

let d =
new Date();

document.write(

d.getHours()

+ ":" +

d.getMinutes()

+ ":" +

d.getSeconds()

);

</script>
Output 10:30:45

Example 4 : Today's Date

getDate() Example
<button onclick="F()">
CLICK
</button>

<p id="d"></p>

<script>

function F(){

  var d =
  new Date();

  var n =
  d.getDate();

  document
  .getElementById("d")
  .innerHTML = n;

}

</script>

Example 5 : getMinutes()

Display Current Minutes
<button onclick="F()">
CHECK
</button>

<p id="d"></p>

<script>

function F(){

  var d =
  new Date();

  var n =
  d.getMinutes();

  document
  .getElementById("d")
  .innerHTML = n;

}

</script>

Example 6 : UTC Hours

getUTCHours()
<button onclick="F()">
CHECK
</button>

<p id="d"></p>

<script>

function F(){

  var d =
  new Date();

  var n =
  d.getUTCHours();

  document
  .getElementById("d")
  .innerHTML = n;

}

</script>

Example 7 : Live Clock

Real-Time Clock
<p id="clk"></p>

<script>

setInterval(()=>{

  let d =
  new Date();

  document
  .getElementById("clk")
  .innerText =

  d.toLocaleTimeString();

},1000);

</script>
Output 10:30:45 AM

Important Notes

  • getMonth() starts from 0.
  • Use Date.now() for timestamps.
  • padStart() helps formatting dates.
  • Dates internally use UTC timezone.

Real-Life Use Cases

Clocks

Live dashboards and timers.

📅

Calendars

Appointment and scheduling apps.

Countdowns

Exam and sale countdown timers.

Frequently Asked Questions

Question Answer
Why is getMonth() zero-based? Months start from 0 in JavaScript.
Difference between Date.now() and new Date() ? Date.now() returns timestamp only.
Is Date timezone aware? Yes, dates use UTC internally.

Conclusion

Date and Time functions are essential in JavaScript for building clocks, calendars, schedulers, countdowns, and dynamic web applications.

JavaScript All Chapters

Continue Learning

Previous

Go to Switch Case Chapter

Next

Go to String Function Chapter