Moment: Remove timezone from a moment.js object

Created on 4 Dec 2015  ·  3Comments  ·  Source: moment/moment

I'm using datetimepicker.js and its date function returns a moment.js object. It does so with the local UTC offset in it and my original date has a different offset.

My original date:

2015-10-01T15:00:00.000Z

What I display on the date time picker (DD-MM HH:mm):

01-10 15:00

What I get:

2015-10-01T15:40:00+01:00

What I want:

2015-10-01T15:40:00+00:00

Note how I removed the +01 offset at the end.

How can I do this applying it for any local UTC ? This is, without having to manually remove the 01 (as it can be a any other local offset depending on the user location).

This is what I have so far to get the date, but I don't know how to remove the offset by keeping the same time.

    var momentDate = timePicker.data("DateTimePicker").date();
    console.log(momentDate.format());
    //this prints  2015-10-01T15:40:00+01:00

Most helpful comment

const tmp = moment(valueAsDate).utc(true) - thats what you need thats ignores timezone
then you can use for example as tmp.toISOString().

All 3 comments

As moment will format original date to be a local timezone date. i.e. 2017-08-02 10:00:00+02:00 will be formatted to be 2017-08-02 16:00:00+08:00 if the machine system using +08:00 timezone.

I wrote a function to format original date ignoring timezone:

/**
 * @desc use moment to format a datetime
 * @param string|date|timestamp datetime: the datetime to format
   the given datetime can have timezone, and the timezone will be kept in the process of output datetime
   i.e. formate('2017-09-08 11:24:00+08:00', 'D/M/YYYY HH:mm:ss Z') =>  8/9/2017 11:24:00 +08:00, as you see, +08:00 is kept, which is different from moment default behavior
   if timezone has not been given, local timezone will be used.
 * @param string formatter: the target format you want to get
 * @param string givenFormatter: moment allow developer to define your own formatter, i.e. DD/MM/YYYY HH,mm,ss which is not a statndard time format
 * @return string new formatted datetime string
 * @example:
   1. format(new Date(), 'YYYY-MM-DD HH:mm:ss')
   2. format(your_date, 'YYYY-MM-DD HH:mm:ss', 'DD/MM/YYYY HH,mm,ss')
 */
export function format(datetime, formatter, givenFormatter) {
  let localTimezoneOffset = timezoneOffset()
  let givenTimezoneOffset = moment.parseZone(datetime).utcOffset()

  if (givenTimezoneOffset !== 0) {
    return moment(datetime, givenFormatter).utcOffset(givenTimezoneOffset).format(formatter)
  }
  else {
    // big problem: we do not know whether timezone has not been given or it is +00:00
    // if its utc time is the same with itself, it means timezone has been given
    let u = utc(datetime, 'YYYY-MM-DD HH:mm:ss')
    let t = moment(datetime).parseZone(datetime).format('YYYY-MM-DD HH:mm:ss')
    if (u === t) {
      return moment(datetime, givenFormatter).utc().format(formatter)
    }
    else {
      return moment(datetime).format(formatter)
    }
  }
}

Source code: https://github.com/tangshuang/moment-format

Use this function to format date ignore moment default format logic. 2017-08-02 10:00:00+02:00 will be formatted like 2017/08/02 10:00:00+02:00.

const tmp = moment(valueAsDate).utc(true) - thats what you need thats ignores timezone
then you can use for example as tmp.toISOString().

Was this page helpful?
0 / 5 - 0 ratings

Related issues

IbraheemAlSaady picture IbraheemAlSaady  ·  3Comments

vbullinger picture vbullinger  ·  3Comments

danieljsinclair picture danieljsinclair  ·  3Comments

BCup picture BCup  ·  3Comments

Delgan picture Delgan  ·  3Comments