I'm trying to calculate the time until the US/Central midnight. The problem is that my calculation returns 3 hours 30 minutes (as for me in Europe it's 20:30).
moment('2020-12-19 23:59:59', "YYYY-MM-DD HH:mm:ss", 'US/Central') - moment.tz('US/Central')
I think this should work. I'm calculating a difference between US/Central midnight and US/Central now, but it returns the wrong amount of milliseconds.
Do you know how to make it work?
EDIT:
Tried it with diff and still the same result:
moment('2020-12-19 23:59:59', "YYYY-MM-DD HH:mm:ss", 'US/Central').diff(moment.tz('US/Central'),'hours')
> 3
EDIT2:
Tried moment().tz instead of moment.tz and it didn't help at all...
moment('2020-12-19 23:59:59', "YYYY-MM-DD HH:mm:ss", 'US/Central').diff(moment().tz('US/Central'),'hours')
> 3
Maybe try getting the 2 times separately then find another way to check the difference between them. For example
var now = new Date();
var midnight = new Date();
midnight.setHours(0,0,0,0); // this set's the midnight var to exactly midnight at your systems timezone, you can then offset that time after this
Then use either moment or some other package to check the difference between the two times.
Related
I need some help with javascript dates. I have found a bug when I was working. I think that it has been solved but I don't know why.
We have a custom calendar with seven days each pages (monday-sunday).
When you pick next (>) it add 7 days. The trouble was that in october 2015 (19-25) when you pressed next, it becomes a new week with days between 25-31 instead of 26-1 week.
This was the code that sum one week:
date = new Date( date.getTime() + num * 86400000 );`
And this is how I "fix" it:
date = new Date(date.getFullYear(), date.getMonth(), date.getDate() + num);`
Now the picker is working, I suppose 86400000 are the milliseconds in a day but why it doesn't work for some days?
Thanks
Late October in your locale is when Daylight Savings or "Summer" time ends. One of the days in that week is slightly shorter than other days.
The internals of the JavaScript runtime know about that, so adding days via the setDate() API gets the right answer.
If I may make a recommendation: check out Moment.js. While it doesn't directly answer your question as to why you're encountering your issue (#Pointy's answer is right), it will make such calculations such as yours much simpler.
Instead of this:
date = new Date( date.getTime() + num * 86400000 );
You can do this:
date = moment().add(1, 'w').toDate()
...and I believe it will account for daylight savings time.
So I'm trying to use a javascript date object to deal with automatically rolling over the days. However getting the information seems to be difficult.
date = new Date();
console.log(date.toISOString());
date.setTime(date.getTime() + 600000); // 10 minutes
console.log(date.toISOString());
console.log(date.getDay());
console.log(date.getUTCDay());
This returns
"2014-10-23T22:55:34.962Z"
"2014-10-23T23:05:34.962Z"
4
4
I have no idea why it keeps returning a day that is not even close to what the day actually is.
My current solution just takes sections of the toISOString and assigns things bassed off of that, but I do want to know why this is doing this.
Day returns the day of the week, try date instead.
This question already has answers here:
What is the best way to initialize a JavaScript Date to midnight?
(10 answers)
Closed 8 years ago.
I am using Javascript for Parse.com Cloud Code.
According to Parse.com they return UTC time. I am in EDT (GMT -4)
I'm trying to get today's date at midnight to no avail.
Here is my code:
var date = new Date();
var startDay = Math.floor((date.setUTCHours(4,0,0,0) / 1000));
So before 8pm on each day, the code returns today's date at midnight which is what I want. However, after 8pm it returns tomorrow's date at midnight. I believe the reason is due to the UTC date changing to Today+1 at midnight. But I cant figure out how to resolve this issue in a way that I get my local date at midgnight.
PS:
I also tried setHours(4,0,0,0) in vain.
If I use setUTCHours(0,0,0,0) it returns today's date at 8pm
Thank you for your help.
It's doing everything correct.
Think about it from UTC's point of view. setUTCHours(0,0,0,0) will always set it to midnight that day.
From your point of view though (GMT-4), setting the date to midnight (UTC) at 8pm (GMT-4) appears to go a day forward.
This is because when you call date.setUTCHours(), it first converts that time to UTC, then sets the hours. If you create the date on May 18th, 8pm GMT-4, then call setUTCHours(), it will first convert that date to May 19th, 12am UTC. Then it will perform the hour change.
Here's what you can do to make sure it returns midnight on the same day as the user's timezone:
const date = new Date();
// Convert to midnight in your timezone first
date.setHours(0,0,0,0);
// Convert to midnight UTC
date.setUTCHours(0,0,0,0);
I have a web application that runs on Chrome without any problems on a Android Device but when running it on Firefox it converts the "newvalue" to BST time zone instead of GMT Standard Time.
var now = new Date();
var start = new Date();
var newvalue = new Date(now - start);
The newvalue timezone output is GMT+0100(BST) but should actually be GMT+0000(GMT Standard Time)
Firefox is adding an an extra hour.
I have tried to convert to UTC and GMT but doesn't seem to work.
Any ideas?
OK so ... Ermmm ... I'm not clear on what you're doing with the line:
var newvalue = new Date(now - start);
Are you trying to time something?
Regardless, the first thing this line does is to subtract start from now, and give the difference between the 2 dates in milliseconds. Assuming this happens basically instantly, the result will be approximately (if not precisely) 0 milliseconds.
When you create a date and pass in a single parameter like this, you are asking for the date as it was, X milliseconds after epoch. Epoch is defined as Thursday Jan 1st 1970 (for reasons I won't go into). So by creating a new Date with a parameter of 0, you're just asking the browser to give you epoch.
Why Firefox decides to give you epoch in BST instead of GMT, I'll grant is actually pretty odd (since Jan 1st is clearly not in British Summer Time). But this fact is probably irrelevant, given that this is almost certainly NOT what you are trying to achieve here. If you're trying to time something, I'd suggest you probably just want to do:
var newvalue = start - now;
Where newvalue is now the difference in time. Note: I have swapped now and start around, since in your example start is defined after now, and hence this will give you the positive time difference.
EDIT IN RESPONSE TO COMMENT
To be clear, I'm suggesting that you DON'T create a new Date object with the result of the subtraction.
If you want to get the number of milliseconds between the two times using dates, just subtract them:
var start = new Date();
// do time consuming stuff
var end = new Date();
var difference = end - start; // NOT: var difference = new Date(end - start)
Resolved the issue by turning the two dates in to a UTC date and then finding the difference between the two dates using milliseconds.
This then works on Android.
Thanks for your assistance
Well, you might think that this question has already been asked, but I think it has not. The solutions I've read about all had this "jigsaw puzzle" technique (like getUTCMonth() + getUTCMinutes + ...).
But as I only want to compare the elapsed seconds between two UTC (!) dates, this does not apply.
As everybody knows, you can get the current (non-UTC) date by:
var d = new Date();
var t_millis = d.getTime();
But this is NOT what I want. I'd like to have the current system date in UTC and in milliseconds, so not mess about with strings at all. AFAIK the variable t_millis will contain the millisecond value of the current timestamp in GMT, not UTC.
(Since d is in GMT as well. Unless getTime() does a sort of implicit time zone conversion, i. e. adding the offset BEFORE giving out the milliseconds, but I've never read about that anywhere)
So is there really no other way than adding the offset to the time value?
I'm desperately missing a function like getUTCTimeMillis() known from other languages.
This is an old question but for the sake of the new visitors here is THE CORRECT ANSWER:
Date.now();
It returns the number of milliseconds elapsed since 1 January 1970 00:00:00 UTC
The millisecond value of the time-of-day is going to be the same regardless of your time zone. That is, there are no time zones on planet Earth that differ from one another by a number of milliseconds greater than zero. (They may differ by an integer number of hours or even minutes, but not seconds or milliseconds.)
That said, the value you get back from getTime() is a UTC-relative timestamp. If two web browsers at widely different spots on the globe create a Date object at the same time, they'll both get the same value from .getTime() (assuming the clocks are synchronized, which is of course highly unlikely).
Here: 1338585185539 That's a timestamp I just got from my browser. I'm in Austin, TX, and now it's 4:13 in the afternoon (so that timestamp will be from slightly before that). Plug it into a Date instance on your machine and see what it says.
(edit — for posterity's sake, that timestamp is from 1 June 2012.)
how about:
var now = new Date();
var utc_now = new Date(now.getUTCFullYear(), now.getUTCMonth(), now.getUTCDate(), now.getUTCHours(), now.getUTCMinutes(), now.getUTCSeconds(), now.getUTCMilliseconds());
console.log('UTC: ' + utc_now) // correct UTC time but wrong timezone!
console.log('UTC (in ms): ' + utc_now.getTime())
I have used this function to solve the problem.
function getUTCNow()
{
var now = new Date();
var time = now.getTime();
var offset = now.getTimezoneOffset();
offset = offset * 60000;
return time - offset;
}
The getTime function returns the number of milliseconds elapsed since
1 January 1970 00:00:00 in the client timezone.
getTimezoneOffset return offset in minutes between Client timezone and UTC.
offset = offset * 60000; this operation transform minutes in miliseconds.
subtracting the offset get the number of milliseconds elapsed since 1
January 1970 00:00:00 UTC.
To get the timestamp from a date in UTC, you need to take in consideration the timezone and daylight savings for that date. For example, a date in January or in July could mean 1 hour difference.
The option below does just that.
Date.prototype.getUTCTime = function () {
return this.getTime() - (this.getTimezoneOffset() * 60000);
};
It can be used as in:
var date = new Date();
var timestamp = date.getUTCTime();