Table of Contents
JavaScript provides a useful Date object that abstracts away a lot of the nitty gritty of working with dates directly. This is great because it allows us to represent moments in time and manipulate them with just a few lines of code.
JavaScript makes working with dates easy.
Creating a Date
Creating a date is extremely easy. Simply create a new Date object:
JAVASCRIPTconst date = new Date();
console.log(date);
HTMLFri Sep 01 2017 00:00:00 GMT-0400 (Eastern Daylight Time)
- JavaScript
Because no parameters were passed in, JavaScript will just return the current date and time. This is a useful default, but it's also possible to create a date with a specific date and time.
Datestring
You can optionally pass in a string or a number to Date
and it will give you back a different date and time. If you pass in a string, or a Datestring, it will be parsed with the Date.parse()
method to give you back the Date object.
Let's say instead of today you wanted March 4, 2017, just pass it in:
JAVASCRIPTconst date = new Date("March 4, 2017");
console.log(date);
HTMLSat Mar 04 2017 00:00:00 GMT-0500 (Eastern Standard Time)
Unix Time
In the field of computer science, there is a concept of Unix time. This arbitrarily decided moment in time is set to midnight on January 1st, 1970.
With that frame of reference in mind, if you pass in a number to a Date object, that number represents the number of milliseconds that have elapsed since then.
Here is one trillion milliseconds since then:
JAVASCRIPTconst date = new Date(1000000000000);
console.log(date);
HTMLSat Sep 08 2001 21:46:40 GMT-0400 (Eastern Daylight Time)
The beauty of this format is that you can represent exact moments in time with a single number, and not have to worry about dates and strings and timezones, as you can derive all the information you need whenever you want. This is a great format for storing data in databases and cookies because it is just a number.
Multiple Parameters
Another way of creating new Date objects is by passing in 7 numbers. These numbers represent, in order, the year, month, day, hour, minute, second, and millisecond.
JAVASCRIPTconst date = new Date(2000, 5, 8, 12, 0, 0, 0);
console.log(date);
HTMLThu Jun 08 2000 12:00:00 GMT-0400 (Eastern Daylight Time)
Manipulating Dates
After you have created your dates, you can manipulate them by either retrieving data from them or setting data in them.
Getters
Here are all the methods for getting information from your Date object:
- getDate(): Returns the day in the month
- getDay(): Returns the day in the week
- getMonth(): Returns the month
- getFullYear(): Returns the year
- getHours(): Returns the hour
- getMinutes(): Returns the minutes
- getSeconds(): Returns the seconds
- getMilliseconds(): Returns the milliseconds
- getTime(): Returns the milliseconds elapsed since Unix time
If you would like to easily test all of these out using the current time, try this:
JAVASCRIPTconst date = new Date();
console.log("Date: " + date);
console.log("getDate(): " + date.getDate());
console.log("getDay(): " + date.getDay());
console.log("getMonth(): " + date.getMonth());
console.log("getFullYear(): " + date.getFullYear());
console.log("getHours(): " + date.getHours());
console.log("getMinutes(): " + date.getMinutes());
console.log("getSeconds(): " + date.getSeconds());
console.log("getMilliseconds(): " + date.getMilliseconds());
console.log("getTime(): " + date.getTime());
- JavaScript
Setters
Here are all the methods for setting information in your Date object:
- setMonth(): Sets the month
- setDate(): Sets the day in the month
- setFullYear(): Sets the year
- setHours(): Sets the hour
- setMinutes(): Sets the minutes
- setSeconds(): Sets the seconds
- setMilliseconds(): Sets the milliseconds
- setTime(): Sets the milliseconds elapsed since Unix time
Time between two dates
You can calculate the time between two dates by using the Date.getTime()
method. Here's an example of getting the days between two dates:
JAVASCRIPTconst date1 = new Date("09/01/2017");
const date2 = new Date();
const differenceInDays = (date2.getTime() - date1.getTime()) / (1000 * 3600 * 24);
console.log(Math.floor(differenceInDays) + " days");
Resources
- Getting Started with Svelte
- Getting Started with Express
- Create an RSS Reader in Node
- Git Tutorial: Learn how to use Version Control
- How to deploy a Node app using Docker
- Learn how to use v-model with a custom Vue component
- Getting Started with Handlebars.js
- Getting User Location using JavaScript's Geolocation API
- Getting Started with Moment.js
- Learn how to build a Slack Bot using Node.js
- Building a Real-Time Note-Taking App with Vue and Firebase
- Getting Started with React