JavaScript | date.toLocaleDateString()

The date.toLocaleDateString() is an inbuilt function in JavaScript which is used to convert a date to a string.
Syntax:

dateObj.toLocaleDateString( [locales][, options])

dateObj should be a valid Date object.
Parameters:

  • locales –This parameter is an array of locale strings that contain one or more language or locale tags.Note that it is an optional parameter.If you want to use specific format of the language in your application then specify that language in the locales argument.
  • Options –It is also an optional parameter and contains properties that specify comparison options.Some properties are localeMatcher, timeZone, weekday, year, month, day, hour, minute, second etc.
  • Return values: It returns a date as a string value in specific format that is specified by locale.

    JavaScript code to show the working of date.toLocaleDateString() function:

    Code #1:
    This program prints the current date.

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <html>
    <head>
    <script>
    var dateObj = new Date();  
    var options = { weekday: "long", year: "numeric", month: "short",  
        day: "numeric" }; 
    document.write(dateObj.toLocaleDateString("en-US"));
    document.write("<br>");
    document.write(dateObj.toLocaleDateString("en-US", options));
    </script>
    </head>
    <body></body>
    </html>

    chevron_right

    
    

    Output:

    6/24/2018
    Sunday, Jun 24, 2018
    

    Code #2:
    Without parameters return value of this method cannot be relied upon in scripting.It uses the operating system’s locale’s conventions.

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <html>
    <head>
    <script>
    var dateObj = new Date(1993, 6, 28, 14, 39, 7);
    document.write(dateObj.toLocaleDateString());
    </script>
    </head>
    <body></body>
    </html>

    chevron_right

    
    

    Output:

    7/28/1993 
    

    Note :
    The locales and options arguments are not supported in all browsers.To check whether it is supported or not we can use following function :

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    function toLocaleDateStringSupportsLocales()
    {
        try {
            new Date().toLocaleDateString('i');
        }
        catch (e) {
            return e.name === 'RangeError';
        }
        return false;
    }

    chevron_right

    
    



    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.