Format 24h hour in Local no GMT - momentjs

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.

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.

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.

Convert any local time to GMT time using provided offset

Using XSLT 1.0 I need to convert a date-time value into GMT.
Input is an XML file which contains a date-time field with value of the format "yyyy-mm-ddThh:mm:ss+mm:ss".
I am not sure of the timezone this time is taken from.
So currently I add/subtract the offset with this time to deduce the GMT time.
Example:
Time in input XML = 2015-06-16T15:00:31+02:00
Offset = +02:00
Therefore, GMT Time = [Time in input XML] - [Offset] = 15:00:31 - 02:00 = 13:00:31
Will the offset present in the XML time change according to daylight savings?
Will the offset present in the XML time change according to daylight
savings?
Yes. Or at least it should, if it follows the standard.
Will the offset present in the XML time change according to daylight savings?
It depends what you mean. If the offset is +02:00, that means 2 hours after UTC. UTC isn't affected by daylight savings, and so a dateTime with an offset of +02:00 isn't affected by daylight savings either. In winter, it will correspond to the time shown by clocks in Kiev, in summer, it won't.

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.

Resources