new date time js code example

Example 1: new date javascript

// Several ways to create a Date object
let today = new Date();
let birthday = new Date("December 17, 1995 03:24:00");
let birthday = new Date("1995-12-17T03:24:00");
let birthday = new Date(1995, 11, 17); // the month is 0-indexed
let birthday = new Date(1995, 11, 17, 3, 24, 0);
let birthday = new Date(628021800000); // passing epoch timestamp

// To get Date, Month and Year or Time
let [month, date, year] = new Date().toLocaleDateString("en-US").split("/");
let [hour, minute, second] = new Date()
   .toLocaleTimeString("en-US")
   .split(/:| /);

// Two digit years map to 1900 – 1999
let date = new Date(98, 1); // Sun Feb 01 1998 00:00:00 GMT+0000 (GMT)
// Deprecated method; 98 maps to 1998 here as well
date.setYear(98); // Sun Feb 01 1998 00:00:00 GMT+0000 (GMT)
date.setFullYear(98); // Sat Feb 01 0098 00:00:00 GMT+0000 (BST)

Example 2: javascript date

var currentDate = new Date();
var dateFromTimeStamp= new Date(1560807962);
var dateFromString = new Date('December 17, 1995 03:24:00');
//new Date(year, month, day, hours, minutes, seconds, milliseconds)
//0= Jan, 11=Dec
var dateFromYearMonthDay = new Date(2018, 11, 26, 0, 0, 0, 0);

Example 3: js time

Pulling Date and Time Values
getDate()
Get the day of the month as a number (1-31)
getDay()
The weekday as a number (0-6)
getFullYear()
Year as a four digit number (yyyy)
getHours()
Get the hour (0-23)
getMilliseconds()
The millisecond (0-999)
getMinutes()
Get the minute (0-59)
getMonth()
Month as a number (0-11)
getSeconds()
Get the second (0-59)
getTime()
Get the milliseconds since January 1, 1970
getUTCDate()
The day (date) of the month in the specified date according to universal time (also available for
day, month, fullyear, hours, minutes etc.)
parse
Parses a string representation of a date, and returns the number of milliseconds since January

Example 4: dates in javascript

new Date();
// Sat Jan 16 2021 22:08:57 GMT-0500 (Eastern Standard Time)
Date.now();
// 1610852869057  -> Seconds passed since Jan 1,1970

// Set the date
new Date(year, month, day hours, minutes, seconds, milliseconds)

Example 5: new date() javascript

var date = new Date(); //Will use computers date by default.
//parameters will specify date you put to input
var date = new Date(year, month, day, hours, minutes, seconds, milliseconds);