Skip to content
Related Articles

Related Articles

JavaScript Date toLocaleString() Method

Improve Article
Save Article
Like Article
  • Last Updated : 21 Oct, 2021

Below is the example of Date toLocaleString() method.

  • Example:

        var d = new Date(Date.UTC(2020, 9, 26, 7, 0, 0));
        var result = d.toLocaleString();
        document.write("Date and Time of apocalypse: "
        + result);
  • Output:
    Date and Time of apocalypse: 26/10/2020, 12:30:00

    The date.toLocaleString() method is used to convert a date and time to a string.

    dateObj.toLocaleString(locales, options)

    Parameters: This methods accepts two parameters as mentioned above and described below:

    • 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 a 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 date and time as a string value in the specific format that is specified by locale.

    Note: The dateObj should be a valid Date object.

    Program 1: This code prints the current date and time. Also, In this code toLocaleString() method does not use any parameter so it uses the operating system’s locale’s conventions and return the result which is machine-specific.

      var d = new Date();
      var result = d.toLocaleString();
      document.write("date and time as a string =  " + result);


    date and time as a string = 6/26/2018, 10:28:17 

    Program 2: This code prints the date and time in string format specified by locale parameter.

      var date = new Date(Date.UTC(2018, 5, 26, 7, 0, 0));  
      var options = { hour12: false };  
      document.write(date.toLocaleString("en-US", options));


    6/26/2018, 12:30:00 PM
    6/26/2018, 12:30:00

    Note: The toLocaleString() method is different from toLocaleDateString() as toLocaleDateString() converts only the date of a Date object into a string but toLocaleString() converts date and time to a string..

    Supported Browsers: The browsers supported by JavaScript Date toLocaleString() Method are listed below:

    • Google Chrome 1 and above
    • Edge 12 and above
    • Firefox 1 and above
    • Internet Explorer 3 and above
    • Opera 3 and above
    • Safari 1 and above

    My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!