Date and Time Function in JS

Date and Time Function in JS

Date object is used to stores the date and time and provides different methods for date/time management like creation, modification, measure time or just print current date.

To create a new Date object, call new Date () with one of the following arguments:

  1. new Date ()
  2. new Date (year, month, day, hours, minutes, seconds, milliseconds)
  3. new Date(milliseconds)
  4. new Date (date string)
  • new Date ()- Without arguments – create the current date and time:
        • let currentDate = new Date ();
        • alert (currentDate);
  • new Date(milliseconds)- Create a Date object with the time equal to number of milliseconds (1/1000 of a second)
        •  let date = new Date (0); // 0 means 01.01.1970 UTC+0
        • alert (date);
  • Now add 24 hours, get 02.01.1970 UTC+0
        • let date1 = new Date (24 * 60*60 * 1000);
        • alert(date1);
  • If there is a single string argument, then it is parsed automatically.
        •  let date = new Date (“2021-05-31”);
        • alert(date);
One thought on “Date and Time Function in JS

Post Your Comments & Reviews

Your email address will not be published. Required fields are marked *

*

*