How to convert UTC date time into local date time using JavaScript ?

Given an UTC date and the task is to convert UTC date time into local date-time using JavaScript toLocaleString() function.

Syntax:

var theDate = new Date(Date.parse('06/14/2020 4:41:48 PM UTC'))
theDate.toLocaleString()

JavaScript code:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Funcion to convert UTC date-time
// to Local date-time
function myFunction() {
    var theDate = new Date(Date.parse(
            '06/14/2020 4:41:48 PM UTC'));
  
    document.write("Local date Time: "
            theDate.toLocaleString());
}

chevron_right


Example: This example converting UTC date time into local date time using JavaScript.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
  
<head>
    <title>
        How to convert UTC date time
        into local date time?
    </title>
  
    <style>
        h1 {
            color: green;
        }
  
        body {
            text-align: center;
        }
    </style>
</head>
  
<body>
    <h1>GeekforGeeks</h1>
  
    <p>
        Click the button to convert
        UTC date and time to local
        date and time
    </p>
  
    <p>
        UTC date and time:
        06/14/2020 4:41:48 PM
    </p>
  
    <button onclick="myGeeks()">
        Try it
    </button>
  
    <p id="demo"></p>
  
    <script>
        function myGeeks() {
            var theDate = new Date(Date.parse(
                '06/14/2020 4:41:48 PM UTC'));
  
            document.getElementById("demo")
                .innerHTML = "Local date Time: "
                + theDate.toLocaleString();
        }
    </script>
</body>
  
</html>

chevron_right


Output:

  • Before Clicking the Button:
  • After Clicking the Button:

full-stack-img




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 :
Practice Tags :


1


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