understanding Date and time

understanding Date and time

JavaScript Date & Time - Key Concepts

Every date and time is stored as number representing the millisecond since january 1970,UTC

  • Javascript does not store dates as separate as number day,month or year values.

  • it count millisecond (1 second = 1000 millisecond) from jan 1 1970. This number can be positive dates after 1970 and number can be negative before 1970

console.log(new Date(0)); //1970-01-01T00:00:00.000Z

console.log(new Date(1000)); //1970-01-01T00:00:01.000Z

console.log(new Date(-1000)); //1969-12-31T23:59:59.000Z
  1. getting and setting time:

    Use .getTime() to get timestamp(in millisecond)

    Use .setTime() to set a date using timestamp

let date = new Date()
console.log(date.getTime()); // get the currrent timestamp 1740450274947
console.log(date); // current time 2025-02-25T02:20:36.486Z
date.setTime(10000000)
console.log(date);  //set time start from january,1970 - 1970-01-01T02:46:40.000Z
  1. Date object
    when use date object in math act like their timestamps

     let date = new Date();
     console.log(date + 1000); // Adds milliseconds here and give current time with + millisecond  
     //Tue Feb 25 2025 08:00:57 GMT+0530 (India Standard Time)
    
  2. static method return timestamps

     console.log(Date.now()); // this is current timestamp 1740450959731
     console.log(Date.parse("2025-02-25"));  //  1740441600000
     console.log(Date.UTC(2025,2,25));  // 1742860800000
    
  3. creating Date with timestamp

     console.log(new Date(1740451166337)); // Converts timestamp to current date  
     //2025-02-25T02:39:26.337Z
    

    The MDN documentation on JavaScript Date is very detailed and can take days to read fully. But instead of focusing on everything, let's understand only the most important parts that are useful in real-world coding.

Let understand some specific part of Date with code snippet

Get Methods (Retrieve Date/Time)

 let now = new Date();
  console.log(now.getFullYear()); //2025
  console.log(now.getMonth()); // 1 (here month start from 0)
  console.log(now.getDate()); // 25 
  console.log(now.getDay()); //  2     (here also start with 0 like sunday = 0 monday = 1)
  console.log(now.getHours()); // 8
  console.log(now.getMinutes()); // 22
  console.log(now.getSeconds()); // 51
console.log(now.getMilliseconds()) // 384  (Get milliseconds (0-999))

Set Methods (Modify Date/Time)

 let now = new Date();
 now.setFullYear(2026);  // Set year to 2026
 now.setMonth(5);        // Set month to June (5 because January = 0)
 now.setDate(10);        // Set day to 10th
 console.log(now);
//2026-06-10T03:01:47.721Z

Convert Date into human readable

let now = new Date();
console.log(now.toLocaleString()); // 25/2/2025, 8:34:49 am

What is meaning of UTC

UTC(coordinated universal time) is the standard time which use worldwide. Its does not change with time zones or daylight time and all time zones are defined as offsets from UTC(like UTC+5:30 for india)

Understand with example

Different places in the world have different local times. These times are based on UTC (Coordinated Universal Time) with a plus (+) or minus (-) offset.

  • In india, indian standard time(IST) - UTC + 5:30 (5 hours 30 minutes ahead of UTC)
    if UTC time is 12:00 pm then IST is 5:30 pm

  • In new york, Eastern standard time(EST)- UTC - 5 (5 hours behind UTC)
    if UTC time is 12:00 pm then EST is 7:00 pm

let now = new Date();
console.log(now.toUTCString());  // Tue, 25 Feb 2025 03:39:29 GMT
console.log(now.getUTCHours());   // 3
console.log(now.getUTCMinutes());  // 40
console.log(now.getUTCSeconds);  // 28

Why we use UTC?

  • When we need to manage date/time for international users, we use UTC.

  • If working only on a local system, normal Date() is enough.

  • If showing time globally, we store UTC and convert it based on the user's time zone.