MOCKSTACKS
EN
Questions And Answers

More Tutorials









Javascript Date


ParameterDetails
valueThe number of milliseconds since 1 January 1970 00:00:00.000 UTC (Unix epoch)
dateAsStringA date formatted as a string (see examples for more information)
yearThe year value of the date. Note that month must also be provided, or the value will be interpreted as a number of milliseconds. Also note that values between 0 and 99 have special meaning. See the examples.
monthThe month, in the range 0-11. Note that using values outside the specified range for this and the following parameters will not result in an error, but rather cause the resulting date to "roll over" to the next value. See the examples.
dayThe date, in the range 1-31.
hourThe hour, in the range 0-23.
minuteThe minute, in the range 0-59
secondThe second, in the range 0-59.
millisecondThe millisecond, in the range 0-999.

Create a new Date object

To create a new Date object use the Date() constructor:


  • with no arguments
  • Date() creates a Date instance containing the current time (up to milliseconds) and date.


  • with one integer argument
  • Date(m) creates a Date instance containing the time and date corresponding to the Epoch time (1 January, 1970 UTC) plus m milliseconds. Example: new Date(749019369738) gives the date Sun, 26 Sep 1993 04:56:09 GMT.


  • with a string argument
  • Date(dateString) returns the Date object that results after parsing dateString with Date.parse.


  • with two or more integer arguments
  • Date(i1, i2, i3, i4, i5, i6) reads the arguments as year, month, day, hours, minutes, seconds, milliseconds and instantiates the corresponding Dateobject. Note that the month is 0-indexed in JavaScript,so 0 means January and 11 means December. Example: new Date(2017, 5, 1) gives June 1st, 2017.


    Note that these examples were generated on a browser in the Central Time Zone of the US, during Daylight Time, as evidenced by the code. Where comparison with UTC was instructive, Date.prototype.toISOString() was used to show the date and time in UTC (the Z in the
    formatted string denotes UTC).

    Creates a Date object with the current date and time from the user's browser


    var now = new Date();
    now.toString() === 'Mon Apr 11 2016 16:10:41 GMT-0500 (Central Daylight Time)'

    Output

    'Mon Apr 11 2016 16:10:41 GMT-0500 (Central Daylight Time)'

    setFullYear() method:


    special1.setFullYear(12);
    special1.toString() === 'Sun Jan 01 12 00:00:00 GMT-0600 (Central Standard Time)`

    Output

    'Sun Jan 01 12 00:00:00 GMT-0600 (Central Standard Time)'


    Conclusion

    In this page (written and validated by ) you learned about Javascript Date . What's Next? If you are interested in completing Javascript tutorial, your next topic will be learning about: Javascript Convert to a string format.



    Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue: mockstacks@gmail.com.


    Share On:


    Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using Mockstacks.com, you agree to have read and accepted our terms of use, cookies and privacy policy.