admin管理员组文章数量:1178545
I use moment.js to display a UTC date in the users local timezone:
var date = new Date(Date.UTC(2016,03,30,0,0,0));
var now = new Date();
var diff = (date.getTime()/1000) - (now.getTime()/1000);
var textnode = document.createTextNode(moment(date).format('dddd, DD.MM.YYYY') + ' a las ' + moment(date).format('HH:mm A'));
document.getElementsByClassName("date")[0].appendChild(textnode.cloneNode(true));
I later use the diff
variable to show a countdown timer.
I would like to show a different countdown timer to everyone in their local time zone. (Using the difference till its midnight in their time zone, not in UTC)
But I am struggeling to get it work. Instead of using var date = new Date(Date.UTC(2016,03,30,0,0,0));
I probably need to use some function of moment.js that gives me till midnight in the users time zone.
The best example would be new years eve. If I use UTC everyone would have the same counter (9 hours left) but in different parts of the world this wouldn't make sense. For someone in australia it should be 2 hours left, and for someone in the US 14 hours.
I use moment.js to display a UTC date in the users local timezone:
var date = new Date(Date.UTC(2016,03,30,0,0,0));
var now = new Date();
var diff = (date.getTime()/1000) - (now.getTime()/1000);
var textnode = document.createTextNode(moment(date).format('dddd, DD.MM.YYYY') + ' a las ' + moment(date).format('HH:mm A'));
document.getElementsByClassName("date")[0].appendChild(textnode.cloneNode(true));
I later use the diff
variable to show a countdown timer.
I would like to show a different countdown timer to everyone in their local time zone. (Using the difference till its midnight in their time zone, not in UTC)
But I am struggeling to get it work. Instead of using var date = new Date(Date.UTC(2016,03,30,0,0,0));
I probably need to use some function of moment.js that gives me till midnight in the users time zone.
The best example would be new years eve. If I use UTC everyone would have the same counter (9 hours left) but in different parts of the world this wouldn't make sense. For someone in australia it should be 2 hours left, and for someone in the US 14 hours.
Share Improve this question asked Mar 19, 2016 at 4:28 SnowballSnowball 1,5123 gold badges17 silver badges31 bronze badges 2 |1 Answer
Reset to default 44I'm not sure that I fully understand your question, but I'll give you some general advice and tips.
When using moment.js, there is very little need to ever use the
Date
object. Only use it for interacting with other APIs that expect aDate
object.To get a moment in UTC, just use
moment.utc(...)
, passing the appropriate arguments, such asmoment.utc([2016,3,30])
ormoment.utc('2016-04-30')
for midnight April 30th UTC.If you want to convert that back to the user's local time, use the
.local()
function. For example,moment.utc('2016-04-30').local()
will create a moment with the equivalent local time to the UTC time provided.If you want a moment in the user's local time, then that would be
moment(...)
, such asmoment([2016,3,30])
ormoment('2016-04-30')
for midnight April 30th local time.You can difference two moments using the
diff
function, which can give the answer in specific units, such asm1.diff(m2, 'seconds')
wherem1
andm2
are moment objects.You don't need to call
format
twice. Just encapsulate any text you want outputed with square brackets..format('dddd, DD.MM.YYYY [a las] HH:mm A')
You might look into moment's locale support. If I'm not mistaken, "a las" indicates Spanish, however it's not always "a las", but sometimes "a la", if the hour is
1
. Also, moment only uses those words in its.calendar()
function, such as when producing a phrase like"mañana a las 13:17"
. A regular date formatted with.format('LLLL')
in the Spanish locale would be something like:"sábado, 19 de marzo de 2016 13:17"
. So, you might want to verify that "a las" is exactly what you want in every case.The title to this question was how to set a date to midnight. For that, I recommend using moment's
startOf
function.m.startOf('day')
will give set the momentm
to the start of the day, which is usually midnight. Keep in mind that not every local day actually starts at midnight in every time zone. Due to anomalies like daylight saving time, some days might start at 1:00. For example, this occurs in Brazil on October 16th this year.Also, if you created the moment in UTC mode, you may wish to convert it back to local mode first before setting it to the start of the day. If you don't want to change the original moment object, be sure to clone it first.
Putting this all together:
var m1 = moment.utc([2016,3,30]); var m2 = m1.clone().local().startOf('day'); var now = moment(); var diff = m1.diff(now, 'seconds');
本文标签: javascriptSet date() to midnight in users timezone with momentjsStack Overflow
版权声明:本文标题:javascript - Set date() to midnight in users timezone with moment.js - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738031857a2052279.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
date
variable, you explicitly callDate.UTC()
. All you need to do is construct a midnight date without usingDate.UTC()
right? – CodingGorilla Commented Mar 19, 2016 at 4:35