4

How to Convert UTC Date Time to Local Date Time in JavaScript

 1 year ago
source link: https://www.laravelcode.com/post/how-to-convert-utc-date-time-to-local-date-time-in-javascript
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

How to Convert UTC Date Time to Local Date Time in JavaScript

  8576 views

  2 years ago

Javascript

Use the toString() Method

You can simply use the toString() method to convert UTC or GMT date time to local date time in JavaScript. Greenwich Mean Time (GMT) is a time zone and Coordinated Universal Time (UTC) is a time standard. Although GMT and UTC share the same current time in practice.

Let's try out the following example to understand how this method actually works:

<script>
    // Create date object from datetime string
    var date = new Date('Tue, 21 Apr 2020 09:20:30 GMT');
    
    // Coverting to local datetime 
    console.log(date.toString());
    
    // Coverting local datetime back to UTC
    console.log(date.toUTCString()); // Tue, 21 Apr 2020 09:20:30 GMT
</script>
Author : Harsukh Makwana
Harsukh Makwana

Hi, My name is Harsukh Makwana. i have been work with many programming language like php, python, javascript, node, react, anguler, etc.. since last 5 year. if you have any issue or want me hire then contact me on [email protected]


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK