Awesome Club

Date Blog

Exploring the Date Object

A date object within JavaScript allows you to create a variable that represents a date. By creating an instance of the date object, you can then specify the name of the object holding the date and the actual date it is holding.


Date()



        var todayDate = new Date();
        var todayDate1 = new Date(2022, 1, 25);
        var todayDate2 = new Date(2022, 1, 25, 22, 26, 45);
            //Format(YYYY, MM, DD, HH, MM, SS)
        

The three code samples shown above are all examples of using the date constructor to successfully create a date object. Weither you wanted to create an empty object, object with just the date, or a object with the date and time. It's up to you! Just add as many of the fields in the order shown above from left to right as you want.

The data type of a date object is actually pretty cool. All entrys are essentially numbers that get calculated into the number of milliseconds since midnight on January 1, 1970. And then that number is what is actually stored. Pretty rad.


get/set()


            
        

The code sample above shows how to retrieve and set parts of a date object.

The get/set Day method will return and set a 2 digit number that is more than 0 and less than or equal to 31.

The get/set Month method will return and set a 2 digit number less than or equal too 11.

The get/set Full Year method will return and set a 4 digit number.

These getter and setter methods can be very useful for getting a handle on pieces of these objects which may otherwise be difficult if the date were one string. It is also useful for setting the dates because you do not have to enter all of the elements of a date in order seperated by commas.


toString()


            
        

The toString methods shown above can be very useful for displaying date objects. As an example, the toDateString method declared above would return "Tue Jan 26 2022". Additionally, the toTimeString would return "11:10:10 GMT-0600" which shows the time zone along with the time. No parameters can be passed into these methods, but just like the previous methods they are used with the dot operator.