Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ignoring timezones when creating a date in javascript/momentjs

I am creating a schedule for something on the east coast, and I need to create a date that always returns a date object/timestamp for this time.

So, if I open the page in Paris at 18:00, the "new myDate()" should return 12:00.

Any suggestions on how I can do this with wither momentJS or just Javascript? The end result should be a javascript Date object.

like image 283
kimpettersen Avatar asked Nov 09 '12 20:11

kimpettersen


People also ask

How do I change timezone in MomentJS?

To change the default time zone, use moment. tz. setDefault with a valid time zone.

Why you should not use MomentJS?

However, Moment. js has too many drawbacks compared to modern date and time libraries. Its API is not immutable, it is large and it doesn't support tree shaking. Even the Moment team discourages to use their library in new projects.

How do I display timezone in MomentJS?

var result = moment(someDate). format("MM/DD/YYYY HH:mm A Z");

Should you still use MomentJS?

Moment. js is a fantastic time & date library with lots of great features and utilities. However, if you are working on a performance sensitive web application, it might cause a huge performance overhead because of its complex APIs and large bundle size. Problems with Moment.


2 Answers

You can do this with moment.js using moment.utc().

http://momentjs.com/docs/#/parsing/utc/

moment([2011, 10, 8, 5]).format(); // different output based on timezone
moment.utc([2011, 10, 8, 5]).format(); // same output for all timezones

The way moment.utc works is by setting a flag internally to use getUTCMinutes instead of getMinutes, thus the output is the same in all timezones.

like image 104
timrwood Avatar answered Oct 14 '22 04:10

timrwood


If you want your timezone completely ignored, you can use the following approach:

  var firstDayStr = '29 January 2014';
  var startAtTime = '10:01:02 AM';
  var localFormat = 'YYYY-MM-DD[T]HH:mm:ss';

  var m = moment(firstDayStr + ' ' + startAtTime).format(localFormat);

  console.log(m);

Output:

2014-01-29T10:01:02 
like image 16
Roman Pushkin Avatar answered Oct 14 '22 05:10

Roman Pushkin