Thursday, December 26, 2024
Google search engine
HomeLanguagesJavascriptJavaScript | Date Objects

JavaScript | Date Objects

The Date object is an inbuilt datatype of JavaScript language. It is used to work with dates and times. The Date object is created by using new keyword, i.e. new Date(). The Date object can be used date and time in terms of millisecond precision within 100 million days before or after 1/1/1970. But using another methods we can only get and set the year, month, day, hour, minute, second, and millisecond fields using local or UTC or GMT time. So we can represent date and time till the year 275755 using Date object.
There are four different way to declare a date, the basic things is that the date objects are created by the new Date() operator.
Syntax:

new Date()
new Date(milliseconds)
new Date(dataString)
new Date(year, month, date, hour, minute, second, millisecond)

Example:

  • new Date():
    Parameters: The Date() constructor creates a Date object which sets the current date and time depend on the browser’s time zone. It does not accepts any value.
    Example:




    <script> 
    // "neveropen" is Date object
    var neveropen = new Date(); 
      
    document.write(neveropen);
    // Prints todays date 
    </script>    

    
    

    Output: I will return the current Day Month Date Year Standard Time.

    Wed Jul 03 2019 19:01:35 GMT+0530 (India Standard Time)
    
  • new Date(milliseconds):
    Parameters: This method accepts single parameter milliseconds which indicates any numeric value. This argument is taken as the internal numeric representation of the date in milliseconds.
    Example:




    <script> 
    // "neveropen" is Date object
    var neveropen = new Date(4500); 
      
    document.write("Todays date : " + neveropen);
    </script>    

    
    

    Output:

    Todays date : Thu Jan 01 1970 05:30:04 GMT+0530 (India Standard Time)
    
  • new Date(datastring):
    Parameters: This method accepts single parameter dataString which indicates any string value. It is a string representation of a date and return the datastring with the day.
    Example:




    <script> 
    // "neveropen" is Date object
    var neveropen = new Date("October 13, 2013 11:13:00"); 
      
    document.write("Datastring with day : " + neveropen);
    </script>    

    
    

    Output:

    Datastring with day : Sun Oct 13 2013 11:13:00 GMT+0530 (India Standard Time)
    
  • l

  • new Date(year, month, date, hour, minute, second, millisecond):
    Parameters: This method accepts seven parameters as mentioned above and described below:

    • year: Integer value which represents the year. This should always specify the year in full, i.e. use 2018, instead of use 18.
    • month: Integer value which represents the month. The integer values are starts from 0 for January to 11 for December.
    • date: Integer value which represents the date.
    • hour: Integer value which represents the hour in 24-hours scale.
    • minute: Integer value which represents the minute.
    • second: Integer value which represents the second.
    • millisecond: Integer value which represents the millisecond.

    Example:




    <script> 
    // "neveropen" is Date object
    var neveropen = new Date(2014, 10, 24, 10, 33, 30, 0); 
      
    document.write(neveropen);
    </script>                    

    
    

    Output:

    Mon Nov 24 2014 10:33:30 GMT+0530 (India Standard Time)
    
  • Properties of Date object:

    • Prototype: Prototype allows us to add properties and methods to an object.
    • Date constructor: It defines the function that creates an Date object’s prototype.

    Some Methods of Date object: Here are some methods that define the usage of a Date object, These are non-static methods.

    Below methods are returns all the values according to local time:

    Method Description
    Date() It returns presents day’s date and time.
    getDate() It returns the day for the specified date.
    getDay() It returns the day of the week for the specified date.
    getFullYear() It returns the year of the specified date.
    getYear() This method returns the year in the specified date.
    getHours() It returns the hour in a specified date.
    getMilliseconds() It returns the milliseconds in the specified date.
    getMinutes() It returns the minutes in the specified date.
    getMonth() It returns the month in the specified date. This also find the month.
    getSeconds() This method returns the seconds in the specified date.
    getTime() This method returns the date in terms of numeric value as milliseconds.
    setDate() This method sets the day of the month for a specified date.
    setFullYear() This method sets the full year for a specified date.

    There are many more methods:

    Below methods are returns all the values according to universal time:

    Methods Description
    getUTCDate() It returns the day of a month for a specified date.
    getUTCDay() It returns the day of the week for a specified date.
    getUTCFullYear() This method returns the year for a specified date.
    getUTCHours() It returns the hours in a specified date.
    getUTCMilliseconds() This method returns the milliseconds form for a specified date.
    getUTCMinutes() This method returns the minutes in a specified date.
    getUTCMonth() This method returns the month for a specified date.

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments