Javascript moment to utc

Convert local time to another timezone using Moment JS

Today we’ll show you how to convert local time to another timezone using Moment JS. Additionally, we’ll show you more conversion functions such as convert local time to UTC time, UTC time to desire timezone, etc.

Previously, we have covered the article where you can get the local time to another timezone but it’s in JavaScript. Here we will use the Moment.js.

Use moment and moment-timezone script

We have to add the following moment scripts to convert time.

Convert time using Moment.js

1. Get current local date and time

The below function will give you the local date and time using moment.js.

Читайте также:  Javascript условие if несколько условий

You can change the format of the return date. Check out the format of the date.

2. Get current UTC date and time

If you want to get an UTC datetime then use the following function.

3. Convert local time to UTC time

Moment is providing the .utc() function to get UTC datetime that function will be used to return UTC time. To convert local time to UTC time, run the below function.

convertLocalToUTC ( ’20-05-2020 10:12:44 PM’ , ‘DD-MM-YYYY hh:mm:ss A’ ) ; // Output: 2020-05-20 04:42:44 PM

In the above function, we consider two different parameters, one is date and second one is format that is used to pass the format of the given date.

4. Convert UTC time to local time

Now, let’s convert an UTC time to local time for that use the following function.

In the above code, first we have converted the UTC datetime to the native date object then return the formatted date.

5. Convert UTC time to another timezone

Let’s create one more function to convert an UTC time to another timezone.

convertUTCToTimezone ( ‘2020-05-20 04:42:44 PM’ , null , ‘America/Los_Angeles’ ) ; // Output: 2020-05-20 09:42:44 AM

We need to pass the UTC datetime along with the timezone. We have taken the America/Los_Angeles timezone for an example.

6. Convert local time to another timezone

At the last, we will convert local time to another timezone.

convertLocalToTimezone ( ‘2020-05-20 10:12:44 PM’ , null , ‘America/Los_Angeles’ ) ; // Output: 2020-05-20 09:42:44 AM

Same as the previous function, we used the .tz() method of the moment.

That’s it for today.
Thank you for reading. Happy Coding.

You may also like.

Generate a random password using JavaScript - Clue Mediator

Generate a random password using JavaScript

How to get the current date time of other countries in JavaScript - Clue Mediator

How to get the current date time of other countries in JavaScript

Get the number of days between two dates using jQuery - Clue Mediator

Get the number of days between two dates using jQuery

Splice and Slice array methods in JavaScript - Clue Mediator

Splice and Slice array methods in JavaScript

JavaScript Interview Questions and Answers - Part 1 - Clue Mediator

JavaScript Interview Questions and Answers – Part 1

How to combine two strings in JavaScript - Clue Mediator

How to combine two strings in JavaScript

2 Responses

The last two seem to have their function calls flipped but when I correct the names, they still don’t seem to work. The ones further up the page do though.

Hi Christian,
I forgot to update the code in the article. I have updated the new code now.
Thank you for drawing our attention. Let us know if you are facing any difficulties.

Leave a Reply Cancel reply

Search your query

Recent Posts

  • How to Copy a File from/to a Remote Server using Command Line July 23, 2023
  • Create a MySQL Database on Linux via Command Line July 22, 2023
  • Connect to a MySQL Database Using the MySQL Command: A Comprehensive Guide July 16, 2023
  • Connecting to SSH using a PEM File July 15, 2023
  • How to Add the Body to the Mailto Link July 14, 2023

Tags

Join us

Top Posts

Explore the article

We are not simply proficient at writing blog post, we’re excellent at explaining the way of learning which response to developers.

For any inquiries, contact us at [email protected] .

  • We provide the best solution to your problem.
  • We give you an example of each article.
  • Provide an example source code for you to download.
  • We offer live demos where you can play with them.
  • Quick answers to your questions via email or comment.

Clue Mediator © 2023. All Rights Reserved.

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these cookies, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may have an effect on your browsing experience.

Necessary cookies are absolutely essential for the website to function properly. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information.

Any cookies that may not be particularly necessary for the website to function and is used specifically to collect user personal data via analytics, ads, other embedded contents are termed as non-necessary cookies. It is mandatory to procure user consent prior to running these cookies on your website.

Источник

Javascript moment to utc

moment.utc(); moment.utc(Number); moment.utc(Number[]); moment.utc(String); moment.utc(String, String); moment.utc(String, String[]); moment.utc(String, String, String); moment.utc(String, String, String[]); moment.utc(String, String, Boolean); moment.utc(String, String, String, Boolean); moment.utc(Moment); moment.utc(Date);

By default, moment parses and displays in local time.

If you want to parse or display a moment in UTC, you can use moment.utc() instead of moment() .

This brings us to an interesting feature of Moment.js. UTC mode.

While in UTC mode, all display methods will display in UTC time instead of local time.

moment().format(); // 2013-02-04T10:35:24-08:00 moment.utc().format(); // 2013-02-04T18:35:24+00:00 

Additionally, while in UTC mode, all getters and setters will internally use the Date#getUTC* and Date#setUTC* methods instead of the Date#get* and Date#set* methods.

moment.utc().seconds(30).valueOf() === new Date().setUTCSeconds(30); moment.utc().seconds() === new Date().getUTCSeconds(); 

It is important to note that though the displays differ above, they are both the same moment in time.

var a = moment(); var b = moment.utc(); a.format(); // 2013-02-04T10:35:24-08:00 b.format(); // 2013-02-04T18:35:24+00:00 a.valueOf(); // 1360002924000 b.valueOf(); // 1360002924000 

Any moment created with moment.utc() will be in UTC mode, and any moment created with moment() will not.

To switch from UTC to local time, you can use moment#utc or moment#local.

var a = moment.utc([2011, 0, 1, 8]); a.hours(); // 8 UTC a.local(); a.hours(); // 0 PST 

From here you can search these documents. Enter your search terms below.

Источник

Оцените статью