Go through different ways to display dates using javascript date object. It covers examples of date object usage to understand the main concepts of javascript date object.
Anuj Sharma
Last Updated Jan 9, 2025
Advertisement
Working with dates are very common use-case in web applications, and date-related operations can be achieved using Javascript Date Object. There are many methods supported by Date Object which makes it very easy to work with dates in javascript.
Date object supports 4 types of constructor functions to create a new object
💡 Number of milliseconds since midnight at the beginning of January 1, 1970, UTC — also known as the epoch
new Date()
This constructor doesn't take any parameter and provides a string representation of date time.
const date = new Date();
// "2025-01-05T11:29:53.357Z"
new Date(epoch value)
This constructor takes the integer epoch value (in milliseconds), this input is the same which returned by Date.now()
method. It returns the date-time in string format.
// Epoc value - 1736076951966 in millisecond
const date = new Date(1736076951966);
// 2025-01-05T11:35:51.966Z
new Date(DateString)
This constructor takes the date string and returns the date object representing the same date string.
const date = new Date("2025-01-07T11:45:51.966Z");
// 2025-01-07T11:45:51.966Z
new Date(year, month, <day>, <hour>, <minute>, <second>, <millisecond>)
This parameter can take the individual values of date time parameters. In this date, constructor year and month parameters are mandatory to pass and other parameters are optional.
const date = new Date(2025, 01, 04, 3, 24, 0);
// 2025-02-03T21:54:00.000Z
There are many Static and instance methods supported by date objects, lets check out the most common static and instance methods of Date Objects in javascript. These methods will help to handle any date-related use-case in Machine Coding and javascript interview rounds.
static method now()
returns the epoch time in milliseconds, this method is used generally to find the time difference in milliseconds between the 2 timestamps
const date = Date.now()
console.log('Epoc time in milliseconds: ', date);
// Epoc time in milliseconds: 1736080469441
parse() static method parses the input time and provides the corresponding epoch time, this is the same as using getTime()
method to get epoch time for a given input date.
Date.parse("2025-01-07T11:45:51.966Z");
// 1736250351966
const date = new Date("2025-01-07T11:45:51.966Z");
date.getTime()
// 1736250351966
const date = new Date("2025-01-07T11:45:51.966Z");
date.getDate() // 7
date.getTime() // 1736250351966
const date = new Date("2025-01-07T11:45:51.966Z");
date.getDay() // 2
date.getMonth() // 0
date.getFullYear() // 2025
const date = new Date("2025-01-07T11:45:51.966Z");
date.getHours(); // 17
date.getMinutes(); // 15
date.getSeconds(); // 51
date.getMilliseconds() // 966
static method Date.now()
can be used to find the epoch time in milliseconds, and can be used to find the difference between 2 timestamps
const epochTime1 = Date.now();
console.log('epochTime1 = ',epochTime1);
let sum = 0;
for(let i=0; i<1000000; i++){
// do nothing
sum = sum + i;
}
const epochTime2 = Date.now();
console.log('epochTime2 = ',epochTime2);
console.log('Time difference - ', epochTime2 - epochTime1);
// Output
epochTime1 = 1736081427953
epochTime2 = 1736081427962
Time difference - 9
Advertisement
Advertisement
Advertisement
Anuj Sharma
Last Updated Jan 29, 2025
Understand the difference between HTTP/2 vs HTTP/1.1 based on the various parameters, which helps to understand the improvement areas of HTTP/2 over HTTP 1.1
Anuj Sharma
Last Updated Dec 10, 2024
A brief explanation of Cross-Origin Resource Sharing (CORS) concept to enable client application accessing resources from cross domain and HTTP headers involved to enable resource access.
Anuj Sharma
Last Updated Jan 16, 2025
Deep dive into promise.all polyfill in javascript will help to understand the working of parallel promise calls using Promise.all and its implementation to handle parallel async API calls.
Anuj Sharma
Last Updated Jan 9, 2025
Learn the best & quickest way to format phone number in JavaScript with or without country codes. This will help websites to show the phone numbers in a more human-readable format.
Anuj Sharma
Last Updated Jan 17, 2025
Explore the 5 most efficient ways to Refresh or Reload page in JavaScript similar to location.reload(true), and identify the appropriate use cases to use one of these different approaches.
Anuj Sharma
Last Updated Feb 22, 2025
Easy to understand 5 rules, that cover the behaviour of the "this" keyword in different contexts and helps you to master this keyword for any javascript interview.
© 2024 FrontendGeek. All rights reserved