I am trying to compare two times:
Time A : A datetime which I know is in 'America/Chicago' timezone
Time B : A datetime for current time which I want to convert to 'America/Chicago' and compare to Time A.
I am doing this simple test and unable to understand why all the below give the same result:
moment('05/31/2018 03:25:00').tz('America/Chicago').diff(moment().tz('America/Phoenix'), 'minutes')
Result: 111
moment('05/31/2018 03:25:00').tz('America/Chicago').diff(moment().tz('America/Los_Angeles'), 'minutes')
Result: 110
moment('05/31/2018 03:25:00').tz('America/Chicago').diff(moment().tz('America/Chicago'), 'minutes')
Result: 110
moment('05/31/2018 03:25:00').diff(moment().tz('America/Chicago'), 'minutes')
Result: 110
Note: my system current time is 01:25 PST. So, I believe moment().tz('America/Los_Angeles') should mean 01:25 PST.
Please correct where I am going wrong. I am basically trying to compare a time in CST with current time in PST and find out what's the difference in minutes in real time.
Related
I have a time entry as follows:
FromTime:73400secs in Time format: 20:23
ToTime:73624secs in Time format: 20:27
When taking difference of the secs it is : 224secs in Time Format : 00:03:44
but when taking difference of the Time Format it is: 00:04
Why is this difference happening?
Which is the correct way to show the difference in the fromTime & toTime?
My timezone is GMT+2.
When local time '2020-05-21 01:00'
that should mean '2020-05-20 23:00' in UTC time zone (GMT+0)
So days difference should say: 1
This code deosnt give that result. Anyone have idea why ?
EDIT: Simply said: Saturday 1am in my place is Friday 11pm in UTC. so there SHOULD be 1 day difference. Here is live code sample
https://stackblitz.com/edit/moment-js-playground-vteexd?embed=1&file=index.ts
Internally they are the same date. To prove that, try comparing their timestamps now.format('X') === utc.format('X').
As a solution, I would propose using moment durations to measure the time difference, and using .days() to get the desired value.
This could be of help.
Here I'm going to give solution to my own question:
import moment from "moment-timezone";
//test for GMT+1
var now = moment().startOf("day").add(1, "hours");
console.log(now.format(), " now", now.format('dddd'));
let res= GetDayOffsetUTC(now);
console.log("Day diff: ", res);
// Get DAY difference between local and UTC time
// Responses:
// -1 : your local time is 1 day ahead of UTC
// 1 : your local time is 1 day after UTC
// 0 : your local time is same day as UTC
function GetDayOffsetUTC(date) {
const utcOffset = date.utcOffset();
const utc = date.clone().add(-utcOffset, "minutes");
const dayDiff = utc.startOf("day").diff(date.startOf("day"), "days");
return dayDiff;
}
I need to have a function where the user can insert a certain timeframe (e.g. 1 week or 5 days and 12 hours). Duration from Moment.js looked the most promising.
The code below returns 2 00:00, where 2 equals the numbers of days. This should be 1 because there are only 24 hours in there.
moment.utc(moment.duration(24, 'hours').as("milliseconds")).format("D HH:mm");
What am I doing wrong here?
You are setting 24 hours as a millisecond offset from 1970-01-01 (Unix epoch) by calling moment.utc(...). This means that your moment is holding the date 1970-01-02 00:00 and you are then printing the day part.
If the time frames are fixed and set by yourself you could always manually put in the amount of time in milliseconds to start with. e.g. 24 hours is 86400000 milliseconds.
You have to format the milliseconds in moment duration not in moment. I think the below line gives your expected value.
moment.duration(moment.duration(24, 'hours').as("milliseconds")).format("D HH:mm");
Answer : 1 00:00
I got it fixed with the following code and the plugin:
moment.duration(moment().diff(moment().subtract(1, 'days')));
I am using the following in moment.js to convert seconds to Days Hours Minutes Seconds format
moment().startOf('year').seconds(1209600).format('DD HH:mm:ss')
But instead of getting 14 00:00:00, I am getting 15 00:00:00
What am I missing here?
1209600 seconds is 14 days, so because the first day of the year is day 01 00:00:00, if you add 14 days you get 15 00:00:00.
You don't say exactly what you're trying to do, but what you're getting is the right answer for "what's the date/time for 1209600 seconds into the year."
You're attempting to work with the concept of duration, but you're using the calendar to do it. This isn't a good idea for several reasons. As others pointed out, the calendar starts on the 1st, which is throwing you off. But also, you could have local time zone discontinuities affect your results, such as if your duration went far enough into the year to be caught by the spring-forward daylight saving time transition.
If you want to use Moment to work with durations, there is a separate API for that:
var d = moment.duration(1209600, 'seconds');
var h = d.hours();
var m = d.minutes();
var s = d.seconds();
There is currently not a format method built-in for durations, so you'd have to assemble these into a string yourself, applying zero-padding where necessary. However, there is the moment-duration-format third-party plugin, which would let you do it like this:
moment.duration(1209600, 'seconds').format('DD HH:mm:ss')
moment().startOf('year'); // set to January 1st, 12:00 am this year
So, startOf('year') method set moment starting point to 1st January of current year from 12.00 AM
, which is start of the day. and you are adding 14 days on top of that. But as the initial date started at 12.00 AM, its still a whole day (ends at 11.50 PM) which adds one additional day in final result.
I am building a meeting calendar based on time zones around the world. My problem is how to add or subtract the time zone from the user selected date in JavaScript.
For example, on the select form, the user will select the date from a form: I would then get the results and convert to a date as below...
var ldSelectDate = new Date(document.form1.year.value,
document.form1.month.value,
document.form1.day.value);
I would like to add 12 midnight to this object.
I then read in an XML that gets the time zone difference in a string between two cities: such as "+0430" for Kabul or "-0400" for New York (on daylight savings time). This is based on GMT,.
I then calculate the time zone difference between the two cities: which will return the string of "830". (I assume I have to return this as a date object?). I got this part done returning a string. I'm working with strings.
I then want to loop through the 24 hours of the day, set Kabul at 12 midnight and then loop through. I can most likely figure this out - that is, set the date with the whole hours as I loop.
My problem is painlessly subtract the "830" from Kabul to see what the meeting time will be in New York.
It will be ideal if I can just subtract the hours and the minutes from the Kabul time. I noticed on here someone subtracting the hours in JavaScript, but not the minutes. BTW, that post didn't work for me.
I did this with strings without the minutes, but I mess up with the minutes. There has to be an easier way.
I would take a solution in either native JavaScript or jQuery.
Again, I need to subtract/add the time zone difference in hours and minutes to a certain date.
date.setMinutes(date.getMinutes()+minutesToAdd);
The above code will set your date object ahead by the amount of minutes in the minutesToAdd variable
Easiest would be to calculate the minutes for that time delta then do a minutes delta.
date.setMinutes(date.getMinutes() - (hours*60+minutes))