Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove timezone from a moment.js object #2788

Closed
alvarotrigo opened this issue Dec 4, 2015 · 4 comments
Closed

Remove timezone from a moment.js object #2788

alvarotrigo opened this issue Dec 4, 2015 · 4 comments

Comments

@alvarotrigo
Copy link

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
@mattjohnsonpint
Copy link
Contributor

@tangshuang
Copy link

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.

@Pawelh133
Copy link

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

@f1am3d
Copy link

f1am3d commented Nov 16, 2022

@Pawelh133 it doesn't. moment(valueAsDate).utc(true) will shift the date to amount of timezone offset.
for example:

moment('2022-08-02T11:30:00-09:00').utc(true)

will give
2022-08-02T22:30:00Z

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants