Incorrect timezone adjustment in moment.js and spacetime
My server is sending my Javascript client a timestamp in UTC. I'm currently in Mountain Time which is currently in daylight savings (GMT-7), but any timezone adjustment I do is only applying -6 offset.
To confirm that javascript is even aware of my timezone, I did the following:
console.log(Date().toString());
which outputs the following: Mon Nov 19 2018 12:13:28 GMT-0700 (Mountain Standard Time)
. It's clear that JS knows I am currently in GMT-7.
Now, my server is sending 2018-08-24T17:00:00
. So I parse it with moment.js, convert to local timezone and then format the result.
moment.utc(this.props.value).local().format('h:mm A')
The resulting value is 11:00 AM
. 17:00:00 - 7 offset is 10:00 which is 10:00 AM. Why is javascript converting into 11:00? I get the same result if I try the spacetime library:
const s = spacetime(this.props.value,'UTC')
s.goto(spacetime().timezone().name);
console.log(s.format('h:mm a')); // Also spits out 11:00 AM
If I manually adjust the moment object with the offset, it works correctly:
var m = moment.utc(this.props.value);
m.add(-(new Date()).getTimezoneOffset(), 'minutes');
console.log(m.format('h:mm A')) // Outputs the correct time: 10:00 AM
Why is moment
and spacetime
both not adjusting my timezone correctly when Javascript is clearly aware of what timezone I'm in? The same problem occurs in both Chrome and Microsoft Edge. For now I will use the hacky workaround above, but I'd prefer to use the native methods so I'm curious as to why this isn't working. Any ideas?
javascript datetime timezone momentjs
add a comment |
My server is sending my Javascript client a timestamp in UTC. I'm currently in Mountain Time which is currently in daylight savings (GMT-7), but any timezone adjustment I do is only applying -6 offset.
To confirm that javascript is even aware of my timezone, I did the following:
console.log(Date().toString());
which outputs the following: Mon Nov 19 2018 12:13:28 GMT-0700 (Mountain Standard Time)
. It's clear that JS knows I am currently in GMT-7.
Now, my server is sending 2018-08-24T17:00:00
. So I parse it with moment.js, convert to local timezone and then format the result.
moment.utc(this.props.value).local().format('h:mm A')
The resulting value is 11:00 AM
. 17:00:00 - 7 offset is 10:00 which is 10:00 AM. Why is javascript converting into 11:00? I get the same result if I try the spacetime library:
const s = spacetime(this.props.value,'UTC')
s.goto(spacetime().timezone().name);
console.log(s.format('h:mm a')); // Also spits out 11:00 AM
If I manually adjust the moment object with the offset, it works correctly:
var m = moment.utc(this.props.value);
m.add(-(new Date()).getTimezoneOffset(), 'minutes');
console.log(m.format('h:mm A')) // Outputs the correct time: 10:00 AM
Why is moment
and spacetime
both not adjusting my timezone correctly when Javascript is clearly aware of what timezone I'm in? The same problem occurs in both Chrome and Microsoft Edge. For now I will use the hacky workaround above, but I'd prefer to use the native methods so I'm curious as to why this isn't working. Any ideas?
javascript datetime timezone momentjs
add a comment |
My server is sending my Javascript client a timestamp in UTC. I'm currently in Mountain Time which is currently in daylight savings (GMT-7), but any timezone adjustment I do is only applying -6 offset.
To confirm that javascript is even aware of my timezone, I did the following:
console.log(Date().toString());
which outputs the following: Mon Nov 19 2018 12:13:28 GMT-0700 (Mountain Standard Time)
. It's clear that JS knows I am currently in GMT-7.
Now, my server is sending 2018-08-24T17:00:00
. So I parse it with moment.js, convert to local timezone and then format the result.
moment.utc(this.props.value).local().format('h:mm A')
The resulting value is 11:00 AM
. 17:00:00 - 7 offset is 10:00 which is 10:00 AM. Why is javascript converting into 11:00? I get the same result if I try the spacetime library:
const s = spacetime(this.props.value,'UTC')
s.goto(spacetime().timezone().name);
console.log(s.format('h:mm a')); // Also spits out 11:00 AM
If I manually adjust the moment object with the offset, it works correctly:
var m = moment.utc(this.props.value);
m.add(-(new Date()).getTimezoneOffset(), 'minutes');
console.log(m.format('h:mm A')) // Outputs the correct time: 10:00 AM
Why is moment
and spacetime
both not adjusting my timezone correctly when Javascript is clearly aware of what timezone I'm in? The same problem occurs in both Chrome and Microsoft Edge. For now I will use the hacky workaround above, but I'd prefer to use the native methods so I'm curious as to why this isn't working. Any ideas?
javascript datetime timezone momentjs
My server is sending my Javascript client a timestamp in UTC. I'm currently in Mountain Time which is currently in daylight savings (GMT-7), but any timezone adjustment I do is only applying -6 offset.
To confirm that javascript is even aware of my timezone, I did the following:
console.log(Date().toString());
which outputs the following: Mon Nov 19 2018 12:13:28 GMT-0700 (Mountain Standard Time)
. It's clear that JS knows I am currently in GMT-7.
Now, my server is sending 2018-08-24T17:00:00
. So I parse it with moment.js, convert to local timezone and then format the result.
moment.utc(this.props.value).local().format('h:mm A')
The resulting value is 11:00 AM
. 17:00:00 - 7 offset is 10:00 which is 10:00 AM. Why is javascript converting into 11:00? I get the same result if I try the spacetime library:
const s = spacetime(this.props.value,'UTC')
s.goto(spacetime().timezone().name);
console.log(s.format('h:mm a')); // Also spits out 11:00 AM
If I manually adjust the moment object with the offset, it works correctly:
var m = moment.utc(this.props.value);
m.add(-(new Date()).getTimezoneOffset(), 'minutes');
console.log(m.format('h:mm A')) // Outputs the correct time: 10:00 AM
Why is moment
and spacetime
both not adjusting my timezone correctly when Javascript is clearly aware of what timezone I'm in? The same problem occurs in both Chrome and Microsoft Edge. For now I will use the hacky workaround above, but I'd prefer to use the native methods so I'm curious as to why this isn't working. Any ideas?
javascript datetime timezone momentjs
javascript datetime timezone momentjs
asked Nov 19 '18 at 19:28
BradBrad
3,123103852
3,123103852
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
- Mountain Standard Time is UTC-7
- Mountain Daylight Time is UTC-6
- It is currently November 19th, and DST is not in effect, so you currently get UTC-7
- For the date you gave,
2018-08-24T17:00:00
, DST is in effect, so you get UTC-6
Everything is working correctly.
In other words, it doesn't matter whether it is currently in effect or not, only whether it is/was/will be in effect for the date in question.
Regarding this part:
m.add(-(new Date()).getTimezoneOffset(), 'minutes');
Don't do that. You aren't adjusting the time zone, you're picking a different moment in time.
Can you explain how you can tell that 2018-08-24T17:00:00 is DST? There is no timezone information in that timestamp.
– Brad
Nov 19 '18 at 22:01
I see the problem now. Thanks for your input!
– Brad
Nov 19 '18 at 22:09
1
From the time timestamp alone, you can't tell. However, you said you were in Mountain time zone (Calgary, from your profile). Refer to this page. You can see that August in Calgary is on MDT (UTC-6). It's not necessarily DST everywhere - but it is for you, at that date and time.
– Matt Johnson
Nov 19 '18 at 22:51
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53381381%2fincorrect-timezone-adjustment-in-moment-js-and-spacetime%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
- Mountain Standard Time is UTC-7
- Mountain Daylight Time is UTC-6
- It is currently November 19th, and DST is not in effect, so you currently get UTC-7
- For the date you gave,
2018-08-24T17:00:00
, DST is in effect, so you get UTC-6
Everything is working correctly.
In other words, it doesn't matter whether it is currently in effect or not, only whether it is/was/will be in effect for the date in question.
Regarding this part:
m.add(-(new Date()).getTimezoneOffset(), 'minutes');
Don't do that. You aren't adjusting the time zone, you're picking a different moment in time.
Can you explain how you can tell that 2018-08-24T17:00:00 is DST? There is no timezone information in that timestamp.
– Brad
Nov 19 '18 at 22:01
I see the problem now. Thanks for your input!
– Brad
Nov 19 '18 at 22:09
1
From the time timestamp alone, you can't tell. However, you said you were in Mountain time zone (Calgary, from your profile). Refer to this page. You can see that August in Calgary is on MDT (UTC-6). It's not necessarily DST everywhere - but it is for you, at that date and time.
– Matt Johnson
Nov 19 '18 at 22:51
add a comment |
- Mountain Standard Time is UTC-7
- Mountain Daylight Time is UTC-6
- It is currently November 19th, and DST is not in effect, so you currently get UTC-7
- For the date you gave,
2018-08-24T17:00:00
, DST is in effect, so you get UTC-6
Everything is working correctly.
In other words, it doesn't matter whether it is currently in effect or not, only whether it is/was/will be in effect for the date in question.
Regarding this part:
m.add(-(new Date()).getTimezoneOffset(), 'minutes');
Don't do that. You aren't adjusting the time zone, you're picking a different moment in time.
Can you explain how you can tell that 2018-08-24T17:00:00 is DST? There is no timezone information in that timestamp.
– Brad
Nov 19 '18 at 22:01
I see the problem now. Thanks for your input!
– Brad
Nov 19 '18 at 22:09
1
From the time timestamp alone, you can't tell. However, you said you were in Mountain time zone (Calgary, from your profile). Refer to this page. You can see that August in Calgary is on MDT (UTC-6). It's not necessarily DST everywhere - but it is for you, at that date and time.
– Matt Johnson
Nov 19 '18 at 22:51
add a comment |
- Mountain Standard Time is UTC-7
- Mountain Daylight Time is UTC-6
- It is currently November 19th, and DST is not in effect, so you currently get UTC-7
- For the date you gave,
2018-08-24T17:00:00
, DST is in effect, so you get UTC-6
Everything is working correctly.
In other words, it doesn't matter whether it is currently in effect or not, only whether it is/was/will be in effect for the date in question.
Regarding this part:
m.add(-(new Date()).getTimezoneOffset(), 'minutes');
Don't do that. You aren't adjusting the time zone, you're picking a different moment in time.
- Mountain Standard Time is UTC-7
- Mountain Daylight Time is UTC-6
- It is currently November 19th, and DST is not in effect, so you currently get UTC-7
- For the date you gave,
2018-08-24T17:00:00
, DST is in effect, so you get UTC-6
Everything is working correctly.
In other words, it doesn't matter whether it is currently in effect or not, only whether it is/was/will be in effect for the date in question.
Regarding this part:
m.add(-(new Date()).getTimezoneOffset(), 'minutes');
Don't do that. You aren't adjusting the time zone, you're picking a different moment in time.
answered Nov 19 '18 at 20:46
Matt JohnsonMatt Johnson
136k41275398
136k41275398
Can you explain how you can tell that 2018-08-24T17:00:00 is DST? There is no timezone information in that timestamp.
– Brad
Nov 19 '18 at 22:01
I see the problem now. Thanks for your input!
– Brad
Nov 19 '18 at 22:09
1
From the time timestamp alone, you can't tell. However, you said you were in Mountain time zone (Calgary, from your profile). Refer to this page. You can see that August in Calgary is on MDT (UTC-6). It's not necessarily DST everywhere - but it is for you, at that date and time.
– Matt Johnson
Nov 19 '18 at 22:51
add a comment |
Can you explain how you can tell that 2018-08-24T17:00:00 is DST? There is no timezone information in that timestamp.
– Brad
Nov 19 '18 at 22:01
I see the problem now. Thanks for your input!
– Brad
Nov 19 '18 at 22:09
1
From the time timestamp alone, you can't tell. However, you said you were in Mountain time zone (Calgary, from your profile). Refer to this page. You can see that August in Calgary is on MDT (UTC-6). It's not necessarily DST everywhere - but it is for you, at that date and time.
– Matt Johnson
Nov 19 '18 at 22:51
Can you explain how you can tell that 2018-08-24T17:00:00 is DST? There is no timezone information in that timestamp.
– Brad
Nov 19 '18 at 22:01
Can you explain how you can tell that 2018-08-24T17:00:00 is DST? There is no timezone information in that timestamp.
– Brad
Nov 19 '18 at 22:01
I see the problem now. Thanks for your input!
– Brad
Nov 19 '18 at 22:09
I see the problem now. Thanks for your input!
– Brad
Nov 19 '18 at 22:09
1
1
From the time timestamp alone, you can't tell. However, you said you were in Mountain time zone (Calgary, from your profile). Refer to this page. You can see that August in Calgary is on MDT (UTC-6). It's not necessarily DST everywhere - but it is for you, at that date and time.
– Matt Johnson
Nov 19 '18 at 22:51
From the time timestamp alone, you can't tell. However, you said you were in Mountain time zone (Calgary, from your profile). Refer to this page. You can see that August in Calgary is on MDT (UTC-6). It's not necessarily DST everywhere - but it is for you, at that date and time.
– Matt Johnson
Nov 19 '18 at 22:51
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53381381%2fincorrect-timezone-adjustment-in-moment-js-and-spacetime%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown