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

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.

Related

Moment js showing wrong time when i format

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.

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.

Format 24h hour in Local no GMT

moment("2013-12-31T19:51:57.000-0800").format("HH:mm") yields a 24hr hour format in GMT but I would like it in local time "11:51".
Is this possible with moment's format method?
That is incorrect. The code you gave will not show the the value in GMT. It will first adjust using the offset you gave it (-08:00), and then it will convert it to the correct offset as your own local time zone. So if you're time zone offset happens to be -08:00 for that particular time, you will see no conversion at all.
Also, you seem to be misunderstanding how the offset is applied. When you have an offset on an ISO8601 formatted string (like the one you supplied here), that means the time is already in the offset that is supplied. 19:51 is the local time that is in effect at the -8:00 offset.
To convert to UTC / GMT - you need to first invert the sign, then apply it. So the UTC time here is 3:51:57 AM on 2014-01-01.
Your requested output of 11:51 is not valid for this timestamp, no matter how you look at it.

Does this time format look familiar?

Can someone help me tell what datetime format is this, or what are it's parts?
e.g.
201106020539Z0000031552001001
201106020702Z0000000000001
201105140701Z0000000000001
201105170207A0000018560001001
I think I've got the first few parts (201106020539 is yyyymmddhhmm), but from the Z / A character onward, I have no clue. Is it possible that the rest isn't a datetime at all?
The Z implies Zulu or UTC time https://meta.stackexchange.com/questions/14684/so-html-formats-time-incorrectly, as for what comes after, I don't know since all other time data has already been indicated prior to the Timezone Character.
Also, the fact that your timestamps don't seem to end in the same number of characters suggests they might not be the same format/spec? Where did you obtain them?
In which context did you get these strings? I would guess that the digits are the seconds fractions.
Maybe this helps you:
For a UTC time (a DateTime.Kind
property value of DateTimeKind.Utc),
the result string includes a "Z"
character to represent a UTC date.

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