Moment js showing wrong time when i format - momentjs

I'm trying to convert java datetime instant to hh:mm format using moment js
moment("2020-03-21T17:34:00Z").utcOffset(-0500).format("hh:mm")
it should give me 12:34, but somehow it is giving "12:14" which is the wrong time.

The moment js .utcOffset() method takes the offset in minutes.
so if you want to get 12:34 you need to use -300 instead of -0500
moment("2020-03-21T17:34:00Z").utcOffset(-300).format("hh:mm")

A few things:
The utcOffset function behaves differently whether you pass a string or a number. As a number, it's expected to be in terms of minutes. Since you have hours and minutes in your offset, you should pass it as a string: .utcOffset("-0500")
Format strings are case sensitive. You should use HH:mm (24-hour time), or hh:mm a (12-hour time). Since you used hh:mm without an a, anything after 12 pm will be misrepresented.
You are supplying a fixed offset. If that is your intent, then ok. But do recognize that there is a difference between an offset and a time zone. (See "Time Zone != Offset" in the timezone tag wiki.) For example, if you wanted to convert to US Eastern Time, you should use .tz('America/New_York) instead of .utcOffset("-0500"). (US Eastern time is at -4 for the date given, not -5.) You would need the moment-timezone addon to make this work.
Lastly, recognize that Moment is in maintenance mode. The moment team generally recommends Luxon for new development.

Related

Create moment js object from date-time string and an offset

I'm using Moment.js and have been all over the docs trying to find the best way, if any, to instantiate a moment object from a local date string and an offset, when I need that object to be in UTC and reflect said offset in its hours. For example:
if I have that somewhere it's January 1st, 2019 midnight, and that somewhere's offset is -300, that would be 2019:01:01T00:00:00.000-05:00, right?, meaning it's 5 hours before UTC, so I need to be able to represent that in UTC like
moment.utc("2019-01-01T05:00:00.000+00:00")
What functions/transformations should I use for this purpose?
Thanks
I hope you are after this
moment.utc("2019-01-01 00:00:00").utcOffset(-300).format('YYYYMMDD HHmmss ZZ')
Note that the offset should be a number and not a string.

Moment js diff with different timezone

I want to calculate the difference of time in hours between 2 different time zones using the moment.js library.
I tried using diff function provided by the library but it is not providing correct answers, I tried:
moment('2016-12-18 6:00:00', 'YYYY-MM-DD HH:mm:ss')
.diff(moment('2016-12-18 6:00:00', 'YYYY-MM-DD HH:mm:ss').utcOffset('+09:30'), 'hours')
I am in UTC+5:30 and the moment(string, string) constructor returns the time in my current timezone. And I was expecting the answer to be 4 instead got 0. Is there any other function that moment library provides to get difference amongst timezones ?
As long as you are just using fixed time zone offsets, your above code will work if you make one adjustment:
.utcOffset('+09:30', true)
The second parameter tells the function to retain the date and time value the moment already has when the offset is set. The default (false) will shift the date and time from its current offset (your local time) to the offset provided.
If you are not working with fixed offsets, then you will indeed need to use the moment-timezone add-on.

How does timezones works in javascript?

How does timezones works in javascript?
I'm trying to use moment.js but have some strange results.
In my zone moment().format() returns 2014-08-05T18:56:08+02:00.
But this one moment(0).format() returns 1970-01-01T01:00:00+01:00.
Why do the timezones differ?
You see the timezone differences due to the Daylight Saving Time (or Summer Time).
On moment=0 you timezone was not in DST so the offset by that time was +1 (I believe this is the normal offset in your region).
Right now, in the current moment you use moment(), your region is in DST so your current offset is +2. That's why you see the different offsets.
I've never used Javascript, but using google I found this link.
It looks like passing 0 as an argument constructs a time with a 0 millisecond offset from the start of Unix Time.
If we go 0 milliseconds from the start of unix time, we end up at the start of unix time. Which is Thursday, 1 January 1970.

function in peoplecode which takes in timezoneoffset of UTC and calculate timezone?

I am calling a service which standardizes a given address and also gives timezone of the result in UTC offset (ex: -5:00 etc).
Is there a function in peoplecode which takes in timezoneoffset of UTC and calculate timezone ?
Have you tried this:
DateTimeToTimeZone
DateTimeToTimeZone
Syntax
DateTimeToTimeZone(OldDateTime, SourceTimeZone, DestinationTimeZone)
Description
Use the DateTimeToTimeZone function to convert DateTime values from the DateTime specified by SourceTimeZone to the DateTime specified by DestinationTimeZone.
Considerations Using this Function
Typically, this function is used in PeopleCode, not for displaying time. If you take a DateTime value, convert it from base time to client time, then try to display this time, depending on the user settings, when the time is displayed the system might try to do a second conversion on an already converted DateTime. This function could be used as follows: suppose a user wanted to check to make sure a time was in a range of times on a certain day, in a certain timezone. If the times were between 12 AM and 12 PM in EST, these resolve to 9 PM and 9 AM PST, respectively. The start value is after the end value, which makes it difficult to make a comparison. This function could be used to do the conversion for the comparison, in temporary fields, and not displayed at all.
Example
The following example. TESTDTTM, is a DateTime field with a value 01/01/99 10:00:00. This example converts TESTDTTM from Pacific standard time (PST) to eastern standard time (EST).
&NEWDATETIME = DateTimeToTimeZone(TESTDTTM, "PST", "EST");
&NEWDATETIME will have the value 01/01/99 13:00:00 because EST is three hours ahead of PST on 01/01/99, so three hours are added to the DateTime value.

What date format is this?

I exported my Firefox bookmarks, and the 'dateAdded' fields look like this:
1260492675000000
1260492675000000
1266542833000000
They're too big to be a Unix timestamp, and I can't make sense of them. What are they? (I want to convert it into something usable/readable.)
It is PRTime.
This type is a 64-bit integer representing the number of microseconds since the NSPR epoch, midnight (00:00:00) 1 January 1970 Coordinated Universal Time (UTC). A time after the epoch has a positive value, and a time before the epoch has a negative value.
PRTime as described on this page.
You can extract the time using the f3e tool if you can find a link to it.

Resources