How to get timezone without moment-timezone.js - javascript

I have this string "Mon Oct 21 2013 21:00:00 GMT-0300 (ART)" and I need to convert it to the timezone (GMT-0300) using just moment.js (not moment-timezone.js)
I'm doing this but it's returning the same hour.
var startDateTime = moment(date).format('YYYY-MM-DD HH:mm Z'),
startMoment = moment.parseZone(startDateTime).zone();
console.log(moment(startDateTime).zone(startMoment).format("YYYY-MM-DD HH:mm"));
Any help ?

To convert a moment object with timezone A to timezone B, you can do the following:
var startDateTime = "Mon Oct 21 2013 21:00:00 GMT-0300 (ART)";
var newDateTime = moment(startDateTime).zone('-0400').format('YYYY-MM-DD HH:mm')
Also note that in your example, the initial time "Mon Oct 21 2013 21:00:00 GMT-0300 (ART)" is already GMT-0300.

Related

Convert date & time string to moment.js with timezone

I have user inputs of meeting_time which is javascript Date (calendar select) and meeting_time string (select dropdown option)
var userTime = meeting_time ? meeting_time.label.split(' -')[0] : null
var userDate = moment(meeting_date).format("YYYY-MM-DD");
How Do I converting those variable to moment.js object with specific timezone
For example,
meeting_date is Thu Jul 12 2018 12:00:00 GMT+0300 (MSK)
meeting_time is 15:00 - 15:30
I want to get moment _d Thu Jul 12 2018 20:00:00 GMT+0800 (UTC +3 to UTC +8)
You can set the hour and minute in the date.
const [hour, minute] = userTime.split(':');
moment(meeting_date).hour(hour).minute(minute).toDate();

Compare Date 1 and Date 2 with 10 minutes

This is how I calculate the duration:
var duration = new Date().getTime() - customerForgotPassword[0].createdTime.getTime();
This is how I compare:
var TEN_MINUTES = 10*60*1000;
if(duration > TEN_MINUTES){
//do smtg
}
new Date().getTime() returns 1528291351108 which is Wed Jun 06 2018 13:22:31 in UTC
customerForgotPassword[0].createdTime returns Wed Jun 06 2018 13:20:04 GMT+0800 (Malay Peninsula Standard Time) in my code.
customerForgotPassword[0].createdTime.getTime() returns 1528262404000 which is Wed Jun 06 2018 05:20:04 in UTC
In database, customerForgotPassword[0].createdTime is in UTC format but when I retrieve it, it shows the local timezone. How can I set this to UTC format too when I retrieve it in node.js using Sequelize ?
I had to add 8 hours to get time in UTC
var timeInUTC = customerForgotPassword[0].createdTime.getTime() - (customerForgotPassword[0].createdTime.getTimezoneOffset() * 60 * 1000)

Wrong result when converting a date

I am just a beginner. My original date is: Friday, September 16th 2016, 09:00
And I need to convert it to this format: Fri Sep 16 2016 09:00:00 GMT+0000 (GMT)
But my code shows wrong date: Tue Sep 06 2016 09:00:00 GMT+0100 (BST)
Please see my code here:
var startdate = "Friday, September 16th 2016, 09:00";
var sdate = new Date(startdate.replace(/(\d)+(st|nd|th)/g, '$1'));
alert(new Date(sdate));
Can anybody help me with this issue? The example is here: https://jsfiddle.net/5hzwbbku/3/
By using momentjs I receive the strange results: https://jsfiddle.net/5hzwbbku/13/
Your regex is incorrect. /(\d)+(st|nd|th)/g matches the 16th but the captured group only contains 6. In order to return 16 your regex needs to be /(\d+)(st|nd|th)/g, like so:
var sdate = new Date(startdate.replace(/(\d+)(st|nd|th)/g, '$1'));
If you need the time to be in UTC, you'll have to append a timezone such as +0, +0000, or simply Z (for Zulu time).
var startdate = "Friday, September 16th 2016, 09:00";
startdate = startdate + ' +0000';
var sdate = new Date(startdate.replace(/(\d+)(st|nd|th)/g, '$1'));
alert(new Date(sdate));
On my system that returns Fri Sep 16 2016 11:00:00 GMT+0200 (CEST) which is 09:00 in UTC.
Other regexes that match correctly are /(\d)(st|nd|th)/g (note the complete absence of the +) and the one given by pastine in the comments /(.\d)+(st|nd|th)/g.

Easy way to convert a timestamp to a human readable date

I have the following timestamp: 2016-03-29T14:14:43.000Z. Is there any easy way to use JavaScript to make it look something like the following: Mar 29, 2016 2:14p? I tried using Date.parse() but it didn't seem to do anything.
{{yourValue| date:"MMM d, yyyy h:ma"}}
var ts = "2016-03-29T14:14:43.000Z";
var date = new Date(ts);
console.log(date); // Displays Tue Mar 29 2016 16:14:43 GMT+0200 (Romance Summer Time)
Is that what you need ?
var ds = date.toUTCString();
console.log(ds.substr(0,24)); // Displays Tue Mar 29 2016 16:14:43

MomentJS Date.now() format

What is the correct way to format this when using MomentJS's fromNow() functionality?
The date was generated using Javascripts Date.now() function.
Thu Nov 19 2015 19:58:03 GMT+0000 (GMT)
My first attempt was this.
var date = Thu Nov 19 2015 19:58:03 GMT+0000 (GMT);
var parsed = moment(date, "dd MMM d YYYY h:mm:ss").fromNow();
But I'm quite sure that dd MMM d YYYY h:mm:ss isn't the correct way to format this.
You can simply use:
var date = "Thu Nov 19 2015 19:58:03 GMT+0000 (GMT)";
moment(date).fromNow(); // 1 hour ago
See: http://momentjs.com/docs/#/parsing/string/

Categories