JavaScript | Date

The Date object in JavaScript is used to represent a moment of time. This time value is since 1 January 1970 UTC (Coordinated Universal Time). We can create date using the Date object by calling the new Date() constructor as shown in the below syntax.

Syntax:

new Date();
new Date(value);
new Date(dateString);
new Date(year, month, day, hours, minutes, seconds, milliseconds);

Parameters: All of the parameters as shown in the above syntax are described below:

  • value : This value is the number of milliseconds since January 1, 1970, 00:00:00 UTC.
  • dateString : This represents a date format.
  • year : This is represented by integer values which ranging from years 1900 to 1999.
  • month : This is represented by integer values which ranging from 0 for January to 11 for December.
  • day : This is an optional parameter. This is represented by integer value for the day of the month.
  • hours : This is optional. This is represented by integer value for the hour of the day.
  • minutes : This is optional. This is represented by integer value for the minute of a time.
  • seconds : This is optional. This is represented by integer value for the second of a time.
  • milliseconds : This is optional. This is represented by integer value for the millisecond of a time.

Return Values: It returns the present date and time if nothing as the parameter is given otherwise it return the date format and time in which parameter is given.

Let’s see JavaScript programs on Date object.



  • Program 1 : If nothing as the parameter is given, it returns present date and time.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // If nothing as parameter is given, 
    // it represent the present date and time.
    var A = new Date();
      
    // Printing present date and time.
    console.log(A);

    chevron_right

    
    

    Output:

    Wed Mar 21 2018 20:44:40 GMT+0530 (India Standard Time)
  • Program 2 : When any integer value is taken as the parameter then it given the number of milliseconds since January 1, 1970, 00:00:00 UTC.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Parameter as integer value give the number of 
    // milliseconds since January 1, 1970, 00:00:00 UTC.
    var A = new Date(32549);
      
    console.log(A);

    chevron_right

    
    

    Output:

    Thu Jan 01 1970 05:30:32 GMT+0530 (India Standard Time)
  • Program 3 : When any dataString is given as the parameter then it return the same as the parameter including day name.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // When any dataString is given as the parameter 
    // then it return the same as the parameter
    // including day name.
    var A = new Date('June 22, 1985 07:19:35');
      
    console.log(A);

    chevron_right

    
    

    Output:

    Sat Jun 22 1985 07:19:35 GMT+0530 (India Standard Time)
  • Program 4 : When some numbers are taken as the parameter then they are considered as year, month, day, hours, minutes, seconds, milliseconds respectively.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // When some numbers are taken as the parameter 
    // then they are considered as year, month, day, 
    // hours, minutes, seconds, milliseconds 
    // respectively.
    var A = new Date(1996, 10, 13, 5, 30, 22);
      
    console.log(A);

    chevron_right

    
    

    Output:

    Wed Nov 13 1996 05:30:22 GMT+0530 (India Standard Time)

Errors and Exceptions:

  • Program 1 : Any integer number should be taken as the parameter not any name otherwise it gives error.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Any integer number should be taken 
    // as the parameter not any name.
    var A = new Date(gfg);
      
    console.log(A);

    chevron_right

    
    

    Output:

    Error: gfg is not defined
  • Program 2 : Any integer number should be take as the parameter not any other number e.g- complex number.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Any integer number should be take as 
    // the parameter not any other number
    // e.g- complex number.
    var A = new Date(1 + 5i);
      
    console.log(A);

    chevron_right

    
    

    Output:

    Error: Invalid or unexpected token
  • Program 3 : Any integer number should be take as the parameter not any other number e.g- complex number.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Any integer number should be taken
    //  as the dateString not word.
    var A = new Date("geeksforgeeks");
      
    console.log(A);

    chevron_right

    
    

    Output:

    Invalid Date

Application: It has many application such as for getting the exact current date and time. Below program prints the current date and time using the Date() object.

filter_none

edit
close

play_arrow

link
brightness_4
code

// If nothing as parameter is given, it 
// represent the present date and time.
var A = new Date();
  
// Printing present date and time.
console.log(A);

chevron_right


Output:

> Wed Mar 21 2018 20:44:40 GMT+0530 (India Standard Time)


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.