Trouble subtracting time from a moment - timezone

I am working on a project that uses a different timezone than the one I currently live in:
moment.tz.add("Asia/Seoul|LMT KST JST KST KDT KDT|-8r.Q -8u -90 -90 -9u -a0|0123141414141414135353|-2um8r.Q 97XV.Q 1m1zu kKo0 2I0u OL0 1FB0 Rb0 1qN0 TX0 1tB0 TX0 1tB0 TX0 1tB0 TX0 2ap0 12FBu 11A0 1o00 11A0|23e6")
Then formatted it as such:
const kST = moment().tz('Asia/Seoul').format("HH:mm");
Now I want to be able to subtract a year from that. Looking at examples of how to do that I found something like var foo = moment(blah, "HH:mm).subtract(1, "years")
I assumed that since I already defined the moment with the 'const kST' I could simply substitute "kST" for "moment" as such:
const firstTimeConverted = kST(firstBus, "HH:mm").subtract(1, "years");
Unfortunately that doesn't work. Any thoughts how I might do this would be much appreciated.

In your code, kST isn't a function - it's a string. The format returns a string formatted as specified.
You probably want:
const firstTimeConverted = moment.tz(firstBus, "HH:mm", `Asia/Seoul`).subtract(1, "years");
This will parse the string in your firstBus variable in HH:mm format (such as "23:45"), and interpret as belonging to the Asia/Seoul time zone on the current date there. Then it will subtract a year and return the result as a moment object. If you want a string, you would then need to call the format function.

Related

Moment Display Date according to ISO-8601 Timezone Offset

I am getting a ISO-8601 date string from an API response as follows :
var x1 = 2022-06-22T05:30:00+05:30
or it could be
var x2 = 2022-06-22T08:30:00-05:00
Irrespective of browser timezone I should display the dates as
X1 - 2022-06-22 05:30:30 IST
X2 - 2022-06-22 08:30:00 EST
How can i parse timezone from the offset like -05:00 or +05:30 using moment or luxon?
I tried moment.tz(timestamp) but it defaults to UTC since it needs the second argument.
So i did a bit more digging.
Logically what i want is not possible.
Many timezones shares UTC offset. Hence, there could be ambiguity, if we try to convert an offset to a TimeZone without any other additional info
Hence, I am opting for a solution, where my API response sends a timezone metadata for each date/time field. (I have lat/long info to convert in Tz in my backend)
In front End, i will simply use the timezone info to format my moment object into a desired date-time String.
For example
const timestring = '2022-06-22T00:00:00+00:00';
const timezoneName = "America/Chicago" ;
moment.tz(timestring, timezoneName).format('YYYY-DD-MM hh:mm:ss z');
// Output: 2022-06-21 07:00:00 CDT
Source : https://stackoverflow.com/tags/timezone/info

Transform Epoch UTC to String EST using momentjs

I have two input variables: an epoch time in UTC time zone and the name of the actual time zone. How do I get a formatted day/time using moment.js that would take in account the DST changes. I tried this code but it doesn't do the trick. What am I doing wrong, please?
var abs_time = 1611188219.277; // this is UTC coresponding to 1/21/2021 18:31:37 UTC
var timezone = "America/New_York"; // this the actual time zone
var mom = moment(abs_time * 1000).format();
var date_time = moment.tz(mom, timezone).format('ddd, MMM DD YYYY - HH:mm');
console.log(date_time);
//actual result: Thu, Jan 21 2021 - 18:31
//desired result: Thu, Jan 21 2021 - 13:31 - in the summer this should only be 4 hour difference
First, 1611188219.277 actually corresponds to 2021-01-21T00:16:59.277Z, not the time you gave in your question (assuming it is a Unix timestamp with seconds precision). This can be seen with the following code:
const d = new Date(1611188219.277 * 1000);
const s = d.toISOString();
console.log(s);
You can get the equivalent local time in a specific time zone without any libraries, as long as you're satisfied with the output produced by the toLocaleString function.
const d = new Date(1611188219.277 * 1000);
const s = d.toLocaleString(undefined, { timeZone: 'America/New_York' });
console.log(s);
Note that undefined in the above code will use the browser's current language. If you want a specific language, you could pass its language code there instead (such as en or en-US, etc.)
In general, due to its project status, you should avoid using Moment unless it's already being used in an existing project. If however, you must use Moment and Moment-TimeZone for this, you can do the following to get the same result:
const m = moment.tz(1611188219.277 * 1000, 'America/New_York');
const s = m.format('ddd, MMM DD YYYY - HH:mm');
console.log(s);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.32/moment-timezone-with-data-10-year-range.min.js"></script>
I used the same format from your question, but of course you could change this as desired.
You might also consider using Luxon (the successor to Moment), or Date-fns, or several other libraries.
And yes, all of the above will correctly take daylight saving time into account.

Dart: DateTime.parse() not including microseconds

See the following code:
void main()
{
String test = '2017-10-11T12:03:46.351363-04:00';
DateTime testDate = DateTime.parse(test);
print(testDate.microsecond);
print(testDate.toString());
}
When running this code I lose the microseconds in the string that I parse. Why is this and is there anyway to solve this?
The Dart docs say that the parse method supports microseconds.
'2017-10-11T12:03:46.351363-04:00' is not a format Dart's DateTime can handle.
DateTime only supports Z for GMT or no Z for local time.
Just print the value from a created DateTime to see what format it can parse.
print(DateTime.now());
print(DateTime.now().toUtc())
DateTime has also 2 constructors fromMicrosecondsSinceEpoch and fromMillisecondsSinceEpoch to create an instance from an integer value.
There's an issue here
https://github.com/dart-lang/co19/issues/17
As the test show it only 6 decimal places
https://github.com/dart-lang/co19/commit/8465825f60c9580d82ae01ffc040f3b589aaf667#diff-02c526d1dcb5aa2dcdab3500c14ede87R40
You can parse format 2018-12-11T12:00:32.304272Z
but cannot parse 2018-12-11T12:00:32.304272001Z
I found an issue for dart-lang/sdk.
https://github.com/dart-lang/sdk/issues/44876
The web Date object doesn't support microseconds. It's implemented
using the JavaScript Date object which only supports millisecond
precision. So, working as well as possible.

Jodatime get milliseconds with offset

New to JodaTime library, i would like to get a DateTime's milliseconds field with the specified TimeZone's offset.
So far my attempt is:
private DateTimeZone timeZone = DateTimeZone.forID("Europe/Amsterdam");
private long now=new DateTime().withZone(timeZone).getMillis();
But i always get the UTC millis, the timezone offset isnt applied,
Is there any way to apply the timezone's offset to the DateTime object?
Thx!
First: What do you intend to do with these "local" millis? What do you really try to achieve? Normally only UTC-millis are needed.
Anyway, remember the general timezone offset definition which is:
UTC + Offset = Local Time
Then the solution is simple:
DateTimeZone tz = DateTimeZone.forID("Europe/Amsterdam");
long nowUTC = new DateTime().withZone(tz).getMillis();
long nowLocal = nowUTC + tz.getOffset(nowUTC);
But once again: What is your use-case for "local" millis? They are not even related to UNIX epoch any longer because the UTC-link is cut off.
About your last question ("Is there any way to apply the timezone's offset to the DateTime object?"):
Your DateTime-object already has got a timezone, namely "Europe/Amsterdam". It is internally used to compute the field tuple representation once you have a global UTC-timestamp expressed as millis since UNIX epoch. No need to apply an extra offset on DateTime. It is already there.
JodaTime is using machine time inside. So to find miliseconds, you can use a constant storing LocalDateTime referring to Jan 1, 1970(Because of UNIX Time).
Unix time, or POSIX time, is a system for describing points in time,
defined as the number of seconds elapsed since midnight proleptic
Coordinated Universal Time (UTC) of January 1, 1970, not counting leap
seconds.
Then calculate the difference between your DateTime.
I tried like this;
public static void main(String[] args) {
final LocalDateTime JAN_1_1970 = new LocalDateTime(1970, 1, 1, 0, 0);
DateTime local = new DateTime().withZone(DateTimeZone.forID("Europe/Amsterdam"));
DateTime utc = new DateTime(DateTimeZone.UTC);
System.out.println("Europe/Amsterdam milis :" + new Duration(JAN_1_1970.toDateTime(DateTimeZone.forID("Europe/Amsterdam")), local).getMillis());
System.out.println("UTC milis :" + new Duration(JAN_1_1970.toDateTime(DateTimeZone.UTC), utc).getMillis());
}
And the result is;
Europe/Amsterdam milis :1429695646528
UTC milis :1429692046534
And #leonbloy write here a good comment.
Your local and utc represent the same instants of time, (only with
different timezones attached). Hence, getMillis() (which gives the
"physical" time interval elapsed from the "instant" corresponding to
the unix epoch), must return the same value.
I will also look for better solution with no constant.

Lua ISO 8601 datetime parsing pattern

I'm trying to parse a full ISO8601 datetime from JSON data in Lua.
I'm having trouble with the match pattern.
So far, this is what I have:
-- Example datetime string 2011-10-25T00:29:55.503-04:00
local datetime = "2011-10-25T00:29:55.503-04:00"
local pattern = "(%d+)%-(%d+)%-(%d+)T(%d+):(%d+):(%d+)%.(%d+)"
local xyear, xmonth, xday, xhour, xminute,
xseconds, xmillies, xoffset = datetime:match(pattern)
local convertedTimestamp = os.time({year = xyear, month = xmonth,
day = xday, hour = xhour, min = xminute, sec = xseconds})
I'm stuck at how to deal with the timezone on the pattern because there is no logical or that will handle the - or + or none.
Although I know lua doesn't support the timezone in the os.time function, at least I would know how it needed to be adjusted.
I've considered stripping off everything after the "." (milliseconds and timezone), but then i really wouldn't have a valid datetime. Milliseconds is not all that important and i wouldn't mind losing it, but the timezone changes things.
Note: Somebody may have some much better code for doing this and I'm not married to it, I just need to get something useful out of the datetime string :)
The full ISO 8601 format can't be done with a single pattern match. There is too much variation.
Some examples from the wikipedia page:
There is a "compressed" format that doesn't separate numbers: YYYYMMDD vs YYYY-MM-DD
The day can be omited: YYYY-MM-DD and YYYY-MM are both valid dates
The ordinal date is also valid: YYYY-DDD, where DDD is the day of the year (1-365/6)
When representing the time, the minutes and seconds can be ommited: hh:mm:ss, hh:mm and hh are all valid times
Moreover, time also has a compressed version: hhmmss, hhmm
And on top of that, time accepts fractions, using both the dot or the comma to denote fractions of the lower time element in the time section. 14:30,5, 1430,5, 14:30.5, or 1430.5 all represent 14 hours, 30 seconds and a half.
Finally, the timezone section is optional. When present, it can be either the letter Z, ±hh:mm, ±hh or ±hhmm.
So, there are lots of possible exceptions to take into account, if you are going to parse according to the full spec. In that case, your initial code might look like this:
function parseDateTime(str)
local Y,M,D = parseDate(str)
local h,m,s = parseTime(str)
local oh,om = parseOffset(str)
return os.time({year=Y, month=M, day=D, hour=(h+oh), min=(m+om), sec=s})
end
And then you would have to create parseDate, parseTime and parseOffset. The later should return the time offsets from UTC, while the first two would have to take into account things like compressed formats, time fractions, comma or dot separators, and the like.
parseDate will likely use the "^" character at the beginning of its pattern matches, since the date has to be at the beginning of the string. parseTime's patterns will likely start with "T". And parseOffset's will end with "$", since the time offsets, when they exist, are at the end.
A "full ISO" parseOffset function might look similar to this:
function parseOffset(str)
if str:sub(-1)=="Z" then return 0,0 end -- ends with Z, Zulu time
-- matches ±hh:mm, ±hhmm or ±hh; else returns nils
local sign, oh, om = str:match("([-+])(%d%d):?(%d?%d?)$")
sign, oh, om = sign or "+", oh or "00", om or "00"
return tonumber(sign .. oh), tonumber(sign .. om)
end
By the way, I'm assuming that your computer is working in UTC time. If that's not the case, you will have to include an additional offset on your hours/minutes to account for that.
function parseDateTime(str)
local Y,M,D = parseDate(str)
local h,m,s = parseTime(str)
local oh,om = parseOffset(str)
local loh,lom = getLocalUTCOffset()
return os.time({year=Y, month=M, day=D, hour=(h+oh-loh), min=(m+om-lom), sec=s})
end
To get your local offset you might want to look at http://lua-users.org/wiki/TimeZone .
I hope this helps. Regards!
There is also the luadate package, which supports iso8601. (You probably want the patched version)
Here is a simple parseDate function for ISO dates. Note that I'm using "now" as a fallback. This may or may not work for you. YMMV 😉.
--[[
Parse date given in any of supported forms.
Note! For unrecognised format will return now.
#param str ISO date. Formats:
Y-m-d
Y-m -- this will assume January
Y -- this will assume 1st January
]]
function parseDate(str)
local y, m, d = str:match("(%d%d%d%d)-?(%d?%d?)-?(%d?%d?)$")
-- fallback to now
if y == nil then
return os.time()
end
-- defaults
if m == '' then
m = 1
end
if d == '' then
d = 1
end
-- create time
return os.time{year=y, month=m, day=d, hour=0}
end
--[[
--Tests:
print( os.date( "%Y-%m-%d", parseDate("2019-12-28") ) )
print( os.date( "%Y-%m-%d", parseDate("2019-12") ) )
print( os.date( "%Y-%m-%d", parseDate("2019") ) )
]]

Resources