JavaScript Get Date Methods

    The JavaScript Date objects support some of the get methods, that can retrieve specific data from the Date. In this JavaScript tutorial we will learn some of those get methods and how can we use them.

    JS Date getTime() method

    The getTime() method is used to return the date in milliseconds since January 1, 1970.

    Example

    <script>
        let today = new Date();
        let total_millisec = today.getTime();
        console.log(total_millisec);
    </script>

    Output

    1616579349137

    JS Date getFullYear() method

    The getFullYear() method returns only the year data from the date in 4 digits YYYY format.

    Example

    <script>
        let today = new Date();
        let year = today.getFullYear();
        console.log(year);
    </script>

    Output

    2021

    JS Date getMonth() method

    The getMonth() method is used when we want to retrieve only month data from the Date. And this method returns a month number value between 0 to 11.

    Example

    <script>
        let today = new Date();
        let month = today.getMonth();
        console.log(month)
    </script>

    Output

    2

    The month number 0 represents January and 11 represents December.

    JS Date getDate() method

    The getDate() method returns the date number between 1 to 31.

    Example

    <script>
        let today = new Date();
        let date = today.getDate()
        console.log(date)
    </script>

    Output

    24

    JS Date getHours() method

    The getHours() method returns the hours value from the date object and it returns a number value between 0 to 23.

    Example

    <script>
        let today = new Date();
        let hour = today.getHours()
        console.log(hour)
    </script>

    Output

    15

    JS Date getMinutes() method

    If you want to retrieve only minutes data from a Date object, there you can use the getMinutes() method. The getMinutes() method returns the minutes number from the date object.

    Example

    <script>
        let today = new Date();
        let minutes = today.getMinutes();
        console.log(minutes)
    </script>

    Output

    19

    JS Date getSeconds() method

    As the method name suggests the getSeconds() method returns the seconds data from the Date object as a number between 0 to 59. Example

    <script>
        let today = new Date();
        let seconds =today.getSeconds();
        console.log(seconds)
    </script>

    Output

    9

    JS Date getMilliseconds() method

    The getMilliseconds() method returns the milliseconds data from the date object. It can return a numeric value between 0 to 999.

    Example

    <script>
        let today = new Date();
        let millisec = today.getMilliseconds()
        console.log(millisec)
    </script>

    Output

    137

    JS Date getDay() method

    The getDay() method returns the weekday number. It can return a numeric value between 0 to 6. Where 0 represents Sunday and 6 represent Saturday.

    Example

    <script>
        let today = new Date();
        let weekday = today.getDay()
        console.log(weekday)
    </script>

    Output

    3

    UTC Date Methods

    All the methods we discussed above will return the date and time data according to your browser-specific timezone. If you want to retrieve date data in the UTC timezone then you can use the following methods.

    Date UTC Methods Description
    getUTCDate() Get date according to UTC, similar to the getDate() method.
    getUTCDay() Get day according to UTC.
    getUTCFullYear() Get Full Year according to UTC.
    getUTCHours() Get hours according to UTC.
    getUTCMilliseconds() Get milliseconds according to UTC.
    getUTCMinutes() Get minutes according to UTC.
    getUTCMonth() Get Month according to UTC.
    getUTCSeconds() Get seconds according to UTC.

    Summary

    • The getFullYear() method returns numeric Year from the date object (yyyy).
    • The getMonth() method returns the numeric month data from the date object (0-11).
    • The getDate() method returns the numeric day data from the date object (0-31).
    • The getHours() method returns Hours data from the date object (0-23).
    • The getMinutes() method return the minutes data from the date object(0-59).
    • The getSeconds() method returns the seconds data from the date object(0-59).
    • The getTime() method returns the time in milliseconds since 1 Jan 1970.
    • The getDay() method returns the weekday number from the Date object (0-6).
    • The getMilliseconds() method return the milliseconds from the date object (0-999)