Methods to create Date object in Javascript 05th June 2020

In this blog we will see how we can creates "Dates" using Javascript's Date function.

What we will achieve at the end of this blog

At the end of this tutorial, we will see various ways to create Dates in Javascript. Idea is to get more familiar with the Date() and its overloaded options provided to us by Javascript.

Source code of the functionality Github Link

Below is a quick demo to wrap your head around to what we are trying to achieve

So what we are trying to achieve is quite clear from the demo/video above.Basically, we are trying to explore various ways with which we can create dates in Javascript.I used to find it tricky when dealing with dates in Javascript and thought of putting all together in one place.I assume this will help you guys as well. There may be other ways to creating dates so feel free to add them below in comments and I will accomodate

PS You will see that i have used callback functions to make code reusable

Like we saw in the demo, we have 4 approaches to accomplish this.I have added below a quick snapshot of the createDate() function i wrote which is a wrapper over Javascripts Date function. In the createDate() function you will see all the 4 approaches(if else statements)

  • First,we are creating Dates by calling new Date() and passing no arguments

  • Second, we take inputs of Year, Month and Day from the User.If you wish you can also take input of hours,seconds and milliseconds.
    The inputs entered by User are captured and passed as argument to Javascript's Date function. Javascript's date function takes

    • Year
    • Month
    • Day
    • Hours
    • Minutes
    • Seconds
    • Milliseconds

    as arguments, in the order they are passed. Incase you skip the argument, then it defaults to 0. In our eg, we only pass the first 3 arguments, i.e Year,Month and Day therefore rest all becomes 0. One interesting thing I learnt is that month is 0 based in Javascript. i.e 0 refers to January and 11 means December.This is little tough to comprehend but yeah that is how the Date Javascript function works.

  • Third option is to pass the Date a String as a paramenter. Some Eg which you can try entering are:

    • Ocotber 3
    • Ocotber 3,2019
    • October 13, 2019 11:13:00
    • October 13, 2019 11:13:00
  • Last option we see is just passing the milliseconds.One thing to understand is that JavaScript stores dates as number of milliseconds since January 01, 1970, 00:00:00 UTC (Universal Time Coordinated).You can try passing values like

    • 0
    • 10000
function createDate(year,month,day,dateStr,milliseconds){
            if(year==undefined && month==undefined && day==undefined
                && dateStr==undefined && milliseconds==undefined)
                    return new Date(); //First approach- No Arguments
            else if (year !=undefined && month !=undefined && day !=undefined 
                 && dateStr==undefined && milliseconds==undefined)
                    return new Date(year,month,day);//Second approach- Year,Month and Day passed in
            else if (dateStr !=undefined && year==undefined && month==undefined 
                 && day==undefined && milliseconds==undefined)
                    return new Date(dateStr);//Third approach- Date string passed in as argument
            else
                    return new Date(milliseconds);//Fourth approach- Argument pass in as milliseconds

                    }
                                

So this ends this tutorial where we saw various options to create Dates in Javascript.You can download the code I shared from Github and extend it if you wish.Please feel free to leave comments or questions below. Or you can drop me an email at "techspacedeck@gmail.com"

Did you like the blog or have questions, please add your comments