MomentJS set timezone without changing time
Asked Answered
C

5

28

I'm trying to set the timezone for a date in moment.js without changing the time value

I get a date in utc:

date.toString() // Sun Sep 27 2015 00:00:00 GMT+0000

and I need to set the time zone without changing the time.

Sun Sep 27 2015 00:00:00 GMT-0500

if I use date.utcOffset(moment().utcOffset()) it adds the offset:

date.toString() // Sat Sep 26 2015 19:00:00 GMT-0500

I could do

date = moment(date.format("YYYYMMDDHHmmssSSSS"), "YYYYMMDDHHmmssSSSS")

but that seems like an inefficient way to do it.

Is there any method that will just change the timezone without changing the time?

Cloven answered 29/10, 2015 at 18:46 Comment(0)
B
17

At the time of writing this (Moment 2.22), you could go from local to UTC with someLocalMomentVariable.utc(true) then back from UTC to local with someUtcMomentVariable.local(true).

Bronchiectasis answered 7/5, 2018 at 12:44 Comment(3)
date.local(true) seems to work but I can't find any documentation on what parameters .local() acceptsCloven
looks like .local(true) was added in v2.8.0Cloven
@TonyBrix yeah, not sure why its not documented.Bronchiectasis
C
18

If you are using moment.js use utcOffset with true as second parameter.

date2 = moment(date1).utcOffset('+0400', true)

Or If you are using moment-timezone, you can use tz function on moment object with second parameter as true.

date1 = moment(date1).tz('Asia/Kolkata', true)
Condorcet answered 19/11, 2018 at 12:45 Comment(2)
This works great, but is there any documentation about the boolean parameter to the tz method on an existing moment instance?Tungus
momentjs.com/timezone/docs/#/using-timezones/converting-to-zoneSlicker
B
17

At the time of writing this (Moment 2.22), you could go from local to UTC with someLocalMomentVariable.utc(true) then back from UTC to local with someUtcMomentVariable.local(true).

Bronchiectasis answered 7/5, 2018 at 12:44 Comment(3)
date.local(true) seems to work but I can't find any documentation on what parameters .local() acceptsCloven
looks like .local(true) was added in v2.8.0Cloven
@TonyBrix yeah, not sure why its not documented.Bronchiectasis
L
8

Using moment timezone, if you can identify the timezone you can also set it without adjusting time with by passing the true flag

const tz = "America/New_York"; // GMT-0500
moment(date).tz(tz, true); // true flag identifies not changing time when updating time zone

Documentation (though it does not explain the boolean flag) Converting to Zone

Logbook answered 20/9, 2021 at 15:42 Comment(0)
U
3

moment().utcOffset(0).add(moment().utcOffset(), 'minutes').format()

You'll have to do some sort of math, and without profiling there's no way to say which is the most efficient.

A javascript date object will always have the local timezone, so you'll need to work in moment objects or compensate.

Undersurface answered 29/10, 2015 at 18:58 Comment(0)
S
0

If you need to set different type of timezones ('Europe/Berlin' and '+05:00') with the moment-timezone, then you can do it with a following steps:

import moment from 'moment-timezone';

// get timezone offset. From 'Europe/Berlin' to '+02:00'

function getOffsetValue(zoneName) {
  const offset = moment.tz(zoneName).format('Z');

  return offset;
}

 // check if it's '+02:00' format or text format 'Europe/Berlin'

const formatZone = zone => {
  if (zone[0] === '+' || zone[0] === '-') {
    return zone;
  }

  return getOffsetValue(zone);
};

export const addCustomTimezone = (zone) => { // pass your zones and periods here
  const shortDate = moment('2022.05.05 00:00:00 +01:00').format('2022.05.05 00:00:00'); // cutting off previous timezone

  return `${shortDate} ${formatZone(zone)}`;
};
Stenger answered 27/6, 2022 at 6:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.