Open In App

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

Last Updated : 30 Dec, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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('DATE_IN_UTC'))
theDate.toLocaleString()

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

html




<body>
    <h1 style="color:green;">
        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>


Output:

 

Example 2: This example converts today’s UTC date time into local date time using JavaScript. 

HTML




<body>
    <h1 style="color:green;">
        GeekforGeeks
    </h1>
  
    <p>
        Click the button to convert
        UTC date and time to local
        date and time
    </p>
  
    <p id="UTC_DATE">
        UTC date and time:
        06/14/2020 4:41:48 PM
    </p>
  
    <button onclick="myGeeks()">
        Try it
    </button>
  
    <p id="demo"></p>
  
    <script>
        var theDate = new Date().toUTCString();
        document.getElementById("UTC_DATE").innerHTML = "UTC date and time: "
                                                        + theDate
        function myGeeks() {
              
            var theDate = new Date().toLocaleString();
          
            document.getElementById("demo")
                .innerHTML = "Local date Time: "
                + theDate.toLocaleString();
        }
    </script>
</body>


Output:

 



Previous Article
Next Article

Similar Reads

How to convert a JavaScript date to UTC?
Javascript date can be converted to UTC by using functions present in the Javascript Date object. The toUTCString() method is used to convert a Date object into a string, according to universal time. The toGMTString() returns a string that represents the Date based on the GMT (UT) time zone. By default, JavaScript uses the browser’s time zone and d
1 min read
JavaScript Date UTC() Method
In JavaScript, the Date.UTC() method is used to create a date object representing a specified date and time in UTC (Coordinated Universal Time). It accepts the year, month, day, hour, minute, second, and millisecond components of the date and returns the number of milliseconds since January 1, 1970, 00:00:00 UTC. Date UTC() Method Syntax:Date.UTC(y
4 min read
JavaScript Get the start and end of the day in UTC
Given a date, and the task is to determine the start and end of the day of the date using JavaScript. We're going to discuss a few methods. These are: JavaScript setHours() Method: This method sets the hour of a date object. This method can be used to set the minutes, seconds, and milliseconds. Syntax: Date.setHours(hour, min, sec, millisec) Parame
3 min read
How to validate if input date (end date) in input field must be after a given date (start date) using express-validator ?
In HTML forms, we often required validation of different types. Validate existing email, validate password length, validate confirm password, validate to allow only integer inputs, these are some examples of validation. In certain cases, we want the user to type a date that must come after some given date(Ex. 'end date' must be after 'start date')
5 min read
How to validate if input date (start date) in input field must be before a given date (end date) using express-validator ?
In HTML forms, we often required validation of different types. Validate existing email, validate password length, validate confirm password, validate to allow only integer inputs, these are some examples of validation. In certain cases, we want the user to type a date that must come before some given date (Ex: 'start date' must come before 'end da
5 min read
Moment.js Parsing UTC
Moment.js is a JavaScript date library for parsing, validating, manipulating, and formatting dates. UTC stands for Universal Time Coordinated which is maintained by the Bureau International des Poids et Measures (BIPM). Moment.js uses the moment() function by default to parse and display in local time. Thus, we need to use moment.utc() function whi
2 min read
Moment.js moment().utc() Method
The moment().utc() method is used to specify that the given Moment object's timezone would be displayed as UTC. An optional parameter can be passed that preserves the current time value and only changes the timezone to UTC. Syntax: moment().utc( Boolean );Parameters: Boolean: It is a boolean value that specifies whether the timezone would be change
2 min read
How to convert JSON results into a date using JavaScript ?
The task is to convert a JSON result to JavaScript Date with the help of JavaScript. There are two methods which are discussed below: Approach 1: Use substr() method to get the integer part of the string.Use the parseInt() method followed by Date() to get the JavaScript date. Example: This example implements the above approach. C/C++ Code &lt;h1 st
2 min read
Convert string into date using JavaScript
In this article, we will convert a string into a date using JavaScript. A string must follow the pattern so that it can be converted into a date. A string can be converted into a date in JavaScript through the following ways: Table of Content Using JavaScript Date() constructorUsing JavaScript toDateString() methodUsing Date.parse() methodUsing Int
3 min read
How to convert a Date into Timestamp using PHP ?
The Task is to convert a Date to a timestamp using PHP. The task can be done by using the strtotime() function in PHP. It is used to convert English textual date-time description to a UNIX timestamp. The UNIX timestamp represents the number of seconds between a particular date and the Unix Epoch. Syntax: strtotime ($EnglishDateTime, $time_now) Exam
1 min read