Date: Work with Moments in Time

Date: Work with Moments in Time

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.
JavaScript makes working with dates easy.

Creating a Date

Creating a date is extremely easy. Simply create a new Date object:

	
    var date = new Date();
    console.log(date);
	
	
    Fri Sep 01 2017 00:00:00 GMT-0400 (Eastern Daylight Time)
	

Because no parameters were passed in, JavaScript will just return the current date and time.

However, you can optionally pass in a string or a number to Date and it will give you back a different date and time.

Datestring

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:

	
    var date = new Date("March 4, 2017");
    console.log(date);
	
	
    Sat 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 abitrarily 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:

	
    var date = new Date(1000000000000);
    console.log(date);
	
	
    Sat 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).

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. The last 4 parameters are optional.

	
    var date = new Date(2000, 5, 8, 12, 0, 0, 0);
    console.log(date);
	
	
    Sat Sep 08 2001 21:46:40 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 millseconds
  • getTime(): Returns the milliseconds elapsed since Unix time

If you would like to easily test all of these out using the current time, simply paste this below:

	
    var 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());
	

Setters

Here are all the methods for setting information in your Date object:

  • setDate(): Sets the day in the month
  • setMonth(): Sets the month
  • setFullYear(): Sets the year
  • setHours(): Sets the hour
  • setMinutes(): Sets the minutes
  • setSeconds(): Sets the seconds
  • setMilliseconds(): Sets the millseconds
  • setTime(): Sets the milliseconds elapsed since Unix time