Learn about JavaScript Dates

Many times, we find it hard to work with JavaScript Date, but it is still widely used. By default, JavaScript will use the browser’s time zone and displays that date.

We can create Date object using the new Date() constructor. We can use this constructor by passing different values to it to generate the date object. Lets see all four of them one by one.

1. new Date() – it creates the new date object with current date and time.

let today_date = new Date(); //sets the current date and time

2. new Date(milliseconds) – it creates the new date object with provided milliseconds. JavaScript stores dates as number of milliseconds from 1st January, 1970, 00:00:00 UTC

let date = new Date(1598932800000); //sets as Tue Sep 01 2020 00:00:00 GMT-0400

3. new Date(year, month, day, hours, minutes, seconds, milliseconds) – it creates the new date object with provided date and time. If you supply only one parameter, it will consider that value as milliseconds. In JavaScript, month starts from 0 to 11 so January is 0 and so on.

let date = new Date(2017,05,09,09,55,30); //Fri Jun 09 2017 09:55:30 GMT-0400 (Eastern Daylight Time)

let date1 = new Date(2017,10); //Wed Nov 01 2017 00:00:00 GMT-0400 (Eastern Daylight Time)

let date2 = new Date(2017,07,3); //Thu Aug 03 2017 00:00:00 GMT-0400 (Eastern Daylight Time)

4. new Date(dateString) – it creates the new date object with provided date and time as string

let date = new Date("November 13, 2018 13:13:00"); //Tue Nov 13 2018 13:13:00 GMT-0500 (Eastern Standard Time)

I hope this post was helpful to you. If you like my post, follow me @twitter/andramazo.

Begin typing your search term above and press enter to search. Press ESC to cancel.

Back To Top