Use the utc() method of moment to remove the timezone and display everything in universal time.

moment.utc('2015-01-22T16:11:36.36-07:00').format('l LT')

That will display the time as it is in UTC without any timezone offset. If you want to display the time as it was recorded in the user/server timezone you can parse the zone information when you construct a moment instance and have it use the timezone recorded in the parsed string.

moment.parseZone('2015-01-22T16:11:36.36-07:00').format('l LT');

With either of these approaches you should consider labeling the time in some way to reflect the timezone the time corresponds to. Failing to do this could lead to a lot of confusion for the end users.

Answer from musicfuel on Stack Overflow
🌐
Reddit
reddit.com › r/programminghelp › using moment.js without having it transform the time to local time
r/programminghelp on Reddit: Using moment.js without having it transform the time to local time
October 15, 2020 -

Hi everyone,

I'm not familiar with javascript at all, only .Net and C++.

Here's the relevant piece of code that I'm working with:

this.timeStamp = moment('08:00', 'HHmm');

Of course, the time I put in isn't hardcoded but that's the exact format that the object I'm putting there contains. Every time.

Now, when I run this code, I receive an object containing the current date, information about my timezone (UTC+0300) and the time 11:00.

Judging from the documentation, this is seemingly normal behavior:

With Moment, the date is always interpreted as local time, unless you specify otherwise.

However, I can't seem to figure out how to specify otherwise. All I want is to create a moment-object that contains the exact time that I entered, not the time I entered + the offset from my local timezone.

It's probably a stupid question but I'm losing my mind over it.

I tried using a different format string, but that (obviously) didn't work. Using moment.utc() also just had the same result as moment().

Is anyone familiar with the moment library and could help me out here?

Discussions

Remove timezone from a moment.js object
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-0... More on github.com
🌐 github.com
4
December 4, 2015
Ignoring timezones when creating a date in javascript/momentjs - Stack Overflow
You could fake it by just not using a timezone. So if the date needs to be at 5AM, just do moment.utc([year, month, date, 5]) and then format and display that. If you are trying to display a date in a specific timezone, it gets trickier, as you need to figure out the offset based on when DST ... More on stackoverflow.com
🌐 stackoverflow.com
Format date in a specific timezone - javascript
NOTE: The format parameter is really important. If omitted moment might fall back to the Date class which can unpredictable behaviors · Assuming the timezone is specified in the date passed in: ... Even though now I understand why it works that way, I thought this was a pretty confusing feature and worth explaining. ... is there a way to get the parsed and converted moment back without ... More on stackoverflow.com
🌐 stackoverflow.com
Any way to specify format to moment.js but keep local timezone adjustment? — DataTables forums
Test case below. My real-life example is working server-side but I don't think that affects it. More on datatables.net
🌐 datatables.net
September 21, 2022
🌐
Readthedocs
momentjscom.readthedocs.io › en › latest › moment › 04-displaying › 01-format
Format - momentjs.com
Out of the box, moment.defaultFormat is the ISO8601 format YYYY-MM-DDTHH:mm:ssZ. As of version 2.13.0, when in UTC mode, the default format is governed by moment.defaultFormatUtc which is in the format YYYY-MM-DDTHH:mm:ss[Z]. This returns Z as the offset, instead of +00:00.
🌐
GitHub
github.com › moment › moment › issues › 2788
Remove timezone from a moment.js object · Issue #2788 · moment/moment
December 4, 2015 - 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 ·
Author   alvarotrigo
Top answer
1 of 9
342

As pointed out in Manto's answer, .utcOffset() is the preferred method as of Moment 2.9.0. This function uses the real offset from UTC, not the reverse offset (e.g., -240 for New York during DST). Offset strings like "+0400" work the same as before:

// always "2013-05-23 00:55"
moment(1369266934311).utcOffset(60).format('YYYY-MM-DD HH:mm')
moment(1369266934311).utcOffset('+0100').format('YYYY-MM-DD HH:mm')

The older .zone() as a setter was deprecated in Moment.js 2.9.0. It accepted a string containing a timezone identifier (e.g., "-0400" or "-04:00" for -4 hours) or a number representing minutes behind UTC (e.g., 240 for New York during DST).

// always "2013-05-23 00:55"
moment(1369266934311).zone(-60).format('YYYY-MM-DD HH:mm')
moment(1369266934311).zone('+0100').format('YYYY-MM-DD HH:mm')

To work with named timezones instead of numeric offsets, include Moment Timezone and use .tz() instead:

// determines the correct offset for America/Phoenix at the given moment
// always "2013-05-22 16:55"
moment(1369266934311).tz('America/Phoenix').format('YYYY-MM-DD HH:mm')
2 of 9
114

A couple of answers already mention that moment-timezone is the way to go with named timezone. I just want to clarify something about this library that was pretty confusing to me. There is a difference between these two statements:

moment.tz(date, format, timezone)

moment(date, format).tz(timezone)

Assuming that a timezone is not specified in the date passed in:

The first code takes in the date and assumes the timezone is the one passed in. The second one will take date, assume the timezone from the browser and then change the time and timezone according to the timezone passed in.

Example:

moment.tz('2018-07-17 19:00:00', 'YYYY-MM-DD HH:mm:ss', 'UTC').format() // "2018-07-17T19:00:00Z"

moment('2018-07-17 19:00:00', 'YYYY-MM-DD HH:mm:ss').tz('UTC').format() // "2018-07-18T00:00:00Z"

My timezone is -5 from utc. So in the first case it does not change and it sets the date and time to have utc timezone.

In the second case, it assumes the date passed in is in -5, then turns it into UTC, and that's why it spits out the date "2018-07-18T00:00:00Z"

NOTE: The format parameter is really important. If omitted moment might fall back to the Date class which can unpredictable behaviors


Assuming the timezone is specified in the date passed in:

In this case they both behave equally


Even though now I understand why it works that way, I thought this was a pretty confusing feature and worth explaining.

🌐
DataTables
datatables.net › forums › discussion › 74120 › any-way-to-specify-format-to-moment-js-but-keep-local-timezone-adjustment
Any way to specify format to moment.js but keep local timezone adjustment? — DataTables forums
September 21, 2022 - If I recall it correctly you will only have the automatic adjustment if you keep DataTable.render.datetime() without parameter. You will need a more complex renderer that allows you to use the output of DataTable.render.datetime() as an input parameter OR you adjust the input date format returned from the server to match your target format. https://datatables.net/examples/datetime/auto-locale-moment.html
🌐
Moment.js
momentjs.com › timezone › docs
Moment Timezone | Docs
Using Time zones Parsing in Zone ... to Zone Formatting Additions Default time zone Guessing user zone Getting all Zones Getting Zones for country ... Data Loading Adding a Zone Adding a Link Loading a Data Bundle Checking Zone Existence Getting Zone Names · Data Utilities Pack Unpack Pack Base 60 Unpack Base 60 Create Links Filter Years Filter Years, Create Links, and Pack · To use moment-timezone, you will ...
Find elsewhere
🌐
Dawntraoz
dawntraoz.com › home › blog › how to format dates without moment.js
How to format dates without Moment.js - Dawntraoz
February 2, 2021 - In order not to forget, I'll explain the problems and the solutions I found without using any library: ... // How it comes from my headlessCMS Storyblok const finishDate = '2021-01-31' // -- Before moment(finishDate).format('MMM YYYY') // -- ...
🌐
Theodo
blog.theodo.com › 2018 › 01 › tips-tricks-date-handling-moment-js
Tips and tricks for date handling with moment.js | Theodo
{ localDateTime: 'YYYY-MM-DD HH:mm', // Not in UTC timezone: 'Indian/Reunion', // Or 'Europe/Paris'' } I had to display and manipulate these dates. But before that, I needed to parse and store them.
🌐
Moment.js
momentjs.com › guides
Moment.js | Guides
moment('01/12/2016', 'DD/MM/YYYY', true).format() "2016-12-01T00:00:00-06:00" In addition, the ECMA Script 5 Specification makes an unusual assertion about the offset of ISO 8601 dates: ... Effectively what this means is that ISO 8601 dates without an offset are to be treated as UTC values, creating the following oddity:
🌐
GitHub
github.com › moment › moment › issues › 4799
Is it possible to keep backend timezone? · Issue #4799
October 4, 2018 - moment / moment Public · Notifications · You must be signed in to change notification settings · Fork 7k · Star 48k · New issueCopy link · New issueCopy link · Closed · Closed · Is it possible to keep backend timezone?#4799 · Copy link · shell362 · opened · on Oct 4, 2018 · Issue body actions · Description of the Issue and Steps to Reproduce: Say I received a date from backend in this format: "2018-10-10T13:00:00+11:00" What I expect to see in frontend after parsing and formatting: 10-10-2018 1.00 PM ·
Author   shell362
🌐
IQCode
iqcode.com › code › javascript › momentjs-display-timezone
momentjs display timezone Code Example
October 26, 2021 - mezone form momentjs momentjs to get current time in utc timezone moment convert date with timezone how to get current timezone in javascript moment moment-timezone zone abbr moment-timezone display full date with timezone abbreviation moment js timezone name momentjs print timezone code momentjs print timezone momentjs timezone src moment.js default timezone moment js get date without time with zone change timezone of a timestring using moment moment rest timezone moment set timezone ja moment display time without timezone moment create date with timezone moment to current timezone moment get
🌐
xjavascript
xjavascript.com › blog › display-datetime-with-momentjs-without-timezone-conversion
How to Display Datetime with Moment.js Without Timezone Conversion: Show Exact Time As-Is — xjavascript.com
Formatting depends on the timezone context: When you call .format(), Moment.js converts the internal timestamp to a string based on the current "timezone mode" (local, UTC, or a specific timezone). To illustrate the problem, let’s look at a common scenario where timezone conversion creeps in unintentionally. Suppose you have a naive datetime string "2024-05-20 14:30:00" (recorded without ...
🌐
GitHub
github.com › moment › moment › issues › 849
Switching timezone of moment without changing the values? · Issue #849 · moment/moment
June 16, 2013 - I want to change the timezone of the selected date without altering actual date and time selected by the user then of course I can convert it to UTC and send back to server. moment(selectedDate) //convert javascript date to moment moment.setTimezone('+0200') // this should change timezone keeping the date and time unchanged moment.utc().toJSON() //back to server ·
Author   amitava82
🌐
Moment.js
momentjs.com › docs
Moment.js | Docs
Some libraries still provide their own locale and time zone files like Moment and Moment-Timezone do.
Top answer
1 of 1
4

To meet the requirement you described (keeping the local time while changing the offset) you can do the following:

var result = moment.parseZone("2019-11-19T07:05:00+01:00").utcOffset(0, true).format();
console.log(result); //=> "2019-11-19T07:05:00Z"
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js"></script>

Step by step explained:

moment.parseZone("2019-11-19T07:05:00+01:00") // Parses the string, retaining the offset provided
      .utcOffset(0, true)  // sets the offset to zero while keeping the local time
      .format()            // formats the output, using Z to represent UTC

However - You should recognize that these are not the same moments in time. The output timestamp is one hour earlier than the input timestamp. This is explained in the documentation as follows (emphasis mine):

The utcOffset function has an optional second parameter which accepts a boolean value indicating whether to keep the existing time of day.

  • Passing false (the default) will keep the same instant in Universal Time, but the local time will change.

  • Passing true will keep the same local time, but at the expense of choosing a different point in Universal Time.

Thus, it is usually the wrong choice when you already have a specific point in time, either in UTC or as an offset from UTC (like your example input value).

Instead, you should expect that converting from local time to UTC will indeed change the date and time portion of the timestamp. You can use .utcOffset(0, false), or .utcOffset(0), or simply .utc() to do the conversion correctly.

🌐
GitHub
github.com › moment › moment › issues › 6223
Time Format without AM and PM · Issue #6223 · moment/moment
March 27, 2024 - using moment().format('LT') , I can get result like 6:02 PM I would like to get th result as 18:02 without setting am/pm
Author   gradaom