# Date Objects
Dates in JavaScript are a special type of Object. They are not just Strings.
They have a long list of methods that we can use to set or retrieve a date value or a portion of the date. To create a Date object we need to use the new constructor.
let today = new Date();
let dt1 = new Date("28/9/2016 14:30:00.000");
let dt2 = new Date(2016, 9, 28, 14, 30, 0);
let ds = new Date(1500000000000);
2
3
4
The first line above will create a new Date object and it will fill it with the current date and time from the computer that is running the script.
The second line creates a new Date object and sets a specific date and time. We pass in a String with a valid date and time. The values for day, month, year, hour, minutes, seconds, and milliseconds would be set inside our Date object. The String that is passed in must be a date string that would be recognized by the Date.parse( )
method. See here for examples
The third line accepts up to seven arguments for the parts of the Date. The last argument is the milliseconds. dt2 will contain the same date as dt1 from the example code.
The last line accepts a timestamp, which is the number of milliseconds since the start of the Unix Epoch -> Jan 1, 1970 00:00:00.000Z. In July of 2017 we passed the 1.5 Trillion mark for milliseconds in the current timestamp. You can pass any number to this method to set the time and date inside the Date object.
# Setting Date Values
If you want to update any part of the date or time then we use one of the following methods.
today.setHours(12); // from 0-23. OR setHours(hour, mins, seconds, milliseconds)
today.setMinutes(3); // from 0-59. OR setMinutes(mins, seconds, milliseconds)
today.setSeconds(50); // from 0 - 59 OR setSeconds(seconds, milliseconds)
today.setMilliseconds(123); // from 0 - 999
today.setFullYear(2044);
today.setMonth(0); //value from 0 - 11 (like an array) OR setMonth( month, day)
today.setDate(); //value from 1 - 31 invalid dates will move forward to a valid one
Date.UTC(y, m, d, hr, min, sec, ms); //creates a date object and returns the timestamp
//equivalent of the date
2
3
4
5
6
7
8
9
10
# Retrieving Date Values
If you want to retrieve any part of the date or time then you can use the matching "get" methods. Start with using your Date variable and then call the method on the variable
today.getHours();
today.getMinutes();
today.getSeconds();
today.getMilliseconds();
today.getFullYear();
today.getMonth(); // value from 0 - 11
today.getDate(); // value from 1 - 31
today.getDay(); //day of week Sunday (0) - Saturday (6)
Date.now(); //Using the Date object, call now( ) to get a timestamp
//which is the number of milliseconds since the start of the UNIX Epoch
//midnight January 1, 1970
2
3
4
5
6
7
8
9
10
11
12
# Outputting Dates
There are many methods for outputting the date object's value. Here is a list of the various methods.
today.toDateString(); //returns the date portion in human readable format
today.toTimeString(); //returns the time portion in human readable format
2
today.toISOString(); //returns a string in simplified extended ISO format
today.toJSON(); //converts the date to a string intended to be used in JSON
today.toLocaleDateString("en-CA"); //returns a string representation based on the
//computer's locale settings or provided locale
today.toLocaleString("en-GB"); //same thing effectively
today.toLocaleString("en-US");
2
3
4
today.toUTCString(); //returns the date string using UTC time zone
# Working With Months
In the JavaScript Date object, months are stored as a number between 0 and 11.
If you want to see or use the name of the month then you need to create your own array of the month names and use the Date object month as the index for that array.
let months = [
"Jan",
"Feb",
"Mar",
"Apr",
"May",
"Jun",
"Jul",
"Sep",
"Oct",
"Nov",
"Dec"
];
let today = new Date();
console.log("Current month is:", months[today.getMonths()]);
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# Converting to Timestamps (unary)
The unary +
operator is an easy way to convert a date into its numeric timestamp.
let today = new Date();
let timestamp = +today; //convert the value in the date to a timestamp
let timestamp = +new Date(); //convert the current computer time to a timestamp
let timestamp = Date.now(); //use the Prototype/Class/Static method
let timestamp = today.valueOf(); //get the timestamp of a specific date
2
3
4
5
# Moment.JS Library
If you are building something that works with a lot of dates or times then it can help to have a JavaScript library that you can use. MomentJS is a great JS library http://momentjs.com/ for formatting and validating dates.
# Luxon Time and Date Library
There is a new updated version of MomentJS, built by one of the team members from MomentJS, called Luxon. It incorporates the latest browser support for multilingual and timezone operations.