Set date() to midnight in users timezone with moment.js

20,845

I'm not sure that I fully understand your question, but I'll give you some general advice and tips.

  1. 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 a Date object.

  2. To get a moment in UTC, just use moment.utc(...), passing the appropriate arguments, such as moment.utc([2016,3,30]) or moment.utc('2016-04-30') for midnight April 30th UTC.

  3. 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.

  4. If you want a moment in the user's local time, then that would be moment(...), such as moment([2016,3,30]) or moment('2016-04-30') for midnight April 30th local time.

  5. You can difference two moments using the diff function, which can give the answer in specific units, such as m1.diff(m2, 'seconds') where m1 and m2 are moment objects.

  6. 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')

  7. 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.

  8. 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 moment m 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');
    
Share:
20,845
Snowball
Author by

Snowball

Updated on March 20, 2020

Comments

  • Snowball
    Snowball about 4 years

    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.

  • Snowball
    Snowball about 8 years
    This is absolutely awesome Matt, thanks a lot. I wasn't aware of all these details, as I first just used moment to convert a UTC time into the users local time and show it beneath the counter. I will update my code, thanks!