Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

moment.js change time to gmt

I have a datetime string: 2016-03-04 14:35:53 this is in `GMT. I have a countdown on my site which counts down time till in seconds.

Now if the user is not gmt they may already be past the time.

How do I use moment.js and moment time zone to adjust this datetime string to the users timezone?

like image 519
Ricky Barnett Avatar asked Mar 04 '16 16:03

Ricky Barnett


People also ask

How to change time with moment in JavaScript?

To change time with moment.js and JavaScript, we can use the set method. to create a moment object from a date string with moment. Then we call set on the returned moment object with an object that sets the hour h and minute m. Therefore, the new value of the moment object is Jan 01 2022 11:11:00.

How do I get the current date and time in moment?

Get the current date and time with Moment.js const now = moment(); This returns an object with the date and time based on your browser's location, along with other locale information. It's similar to native JavaScript's new Date(). Get a date and time from a timestamp with Moment.js

How to convert local time to UTC time in moment?

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.

What is moment JS and how to use it?

Moment.js is a Swiss Army knife for working with dates in JavaScript. It allows you to parse, validate, manipulate, and display dates and times using a clean and concise API. In this article I’ll show you how to get up and running with Moment.js, as well as demonstrate several of its common use cases.


2 Answers

Please us to get GMT TimeStamp using moment.js

var moment = require('moment');
dt = moment();
dt.subtract(dt.parseZone().utcOffset(), 'minutes')

console.log(dt.unix());
like image 144
Ashish Gupta Avatar answered Oct 05 '22 10:10

Ashish Gupta


Code that's being run client side is by default in local time, and then you can convert that into UTC by calling the utc method on the moment constructor. So client side, you can run moment.utc() to get their current time in UTC (which is the same as GMT) and then perform your comparisons on that object.

like image 23
svangordon Avatar answered Oct 05 '22 11:10

svangordon