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?
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.
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
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.
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.
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());
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With