JavaScript Operation on Date

JavaScript Operation on Date

In JavaScript we can fetch current date and time based on the browser’s time zone. This date is displayed as a full text string.

Example: Fri May 03 2019 22:22:06 GMT+0530 (India Standard Time)

We can create a new Date object in given four ways:

Date ObjectDescriptionExample
new Date()This method returns current date and time based on the browser’s time zone.console.log(new Date());

Output: The current date and time will be displayed

new Date(year, month, day, hours, minutes, seconds, milliseconds)This method creates a new Date object with a specified date and time.var date = new Date(2019, 06, 07, 10, 33, 30, 0);

console.log(data);

Output: Sun Jul 07 2019 10:33:30 GMT+0530 (India Standard Time)

new Date(milliseconds)This method adds/subtracts milliseconds mentioned here from the current date.console.log(new Date(1000));

Output: The current date and time + 1000 milliseconds will be dispalyed

new Date(date string)This method creates a new date from a date string. This date string should be of given formats only:

1. ISO Date: “2019-04-30”

2. Short Date: “04/30/2019”

3. Long Date: “Apr 30 2019” or “25 Apr 2015”

console.log(new Date(“Apr 30 2019”));

Output: Tue Apr 30 2019 00:00:00 GMT+0530 (India Standard Time)

 

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.