Given a moment time, how do you change the timezone without changing the actual time?
For example if you have 2020-06-19 16:30:00+00 but you want this in America/New_York timezone.
The output would be 2020-06-19 16:30:00-04:00 (currently Daylight Savings time).
You can do this by using the .tz(.., true) function.
From the Moment Timezone docs (https://momentjs.com/timezone/docs/#/using-timezones/converting-to-zone/):
On passing a second parameter as true, only the timezone (and offset) is updated, keeping the local time same. Consequently, it will now point to a different point in time if the offset has changed.
m.format(); // 2013-11-18T11:55:00-05:00
m.tz('Europe/Berlin', true).format() // 2013-11-18T11:55:00+01:00
Related
Lets say I have 2 files, each with a date column, e.g. 2022-11-14 and a timestamp, e.g. 18:36 column. No prior information is given on timezone this information was taken from. In my code, I create a new column, date_time_X corresponding to the file # where I concatenate date and time paste(as.character(date), timestamp).
I've discovered that base R's POSIXct default timezone is system-specific while lubridate's ymd_hds timezone defaults to UTC, so now I'm defaulting to applying ymd_hds(paste(as.character(date), timestamp)) across both files to keep consistent timezones, since I will be subtracting date_time_1 from date_time_2 to get a time difference diff_time = date_time_2 - date_time_1 .
My question is, what is the best approach to handle datetime variables from these different files when timezone is unknown? Should data be read in as UTC? Is there a way to completely remove the timezone component? I don't want to assume the data was collected at my local time, but I'm also not sure if defaulting to UTC is an acceptable approach. Ideally, removing the timezone would be the best option, but I'm not sure if this is possible without leaving the variables as character columns.
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.
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?
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.
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.