I have seen couple of questions on the same, but which I'm facing the issue is not addressed anywhere so that only asking again.
I am trying to convert current time to CET time so initially i had implemented like this
component.ts
public currentDate;
ngOnInit(){
this.currentDate = new Date();
}
component.html
TIME: {{currentDate | date:'h:mm a':'+0100'}}
With above snippet initially it was matching fine with CET time, but now getting one hour difference. And i found in docs like Central European Time CET alternates between UTC+1 (standard time) and UTC+2 (when daylight saving time (DST) is observed).
All countries in the CET time zone observe DST (UTC+2) from 02:00 am on the last Sunday of March until 03:00 am on the last Sunday of October.
So based on this from last Sunday of march to last Sunday of October supposed to add +2hrs..And tried the same and now matching the same with CET.
But how can we handle this dynamically, or any other approach to convert current time to CET, Instead of changing +1hr some time and +2hr some time period.
Thanks in advance.
Related
I'm building a calendar and I noticed that the date is incorrect on one day of the year.
I wanted to standardize dates to ISO 8601. Let's take 2022-03-27 for example:
const foo = new Date(2022, 2, 27).toISOString()
This gives 2022-03-27T00:00:00.000Z as one would expect.
However, the next day, 2022-03-28:
const bar = new Date(2022, 2, 28).toISOString()
This gives 2022-03-27T23:00:00.000Z - 27th March at 23:00.
Why does this happen?
What's happening is that British Summer Time (Daylight Saving Time, aka DST) is kicking in between your two examples (specifically, it starts at 01:00 on 2022-03-27). toISOString always gives you UTC time (GMT) as you can tell from the Z at the end of the string, but your local time is an hour ahead of UTC after 01:00 on 2022-03-27 until DST ends at 02:00 on 30 October 2022.
In your first example, you're creating a Date with the local time of midnight on 2022-03-27, when your local time is (apparently) GMT+00:00 (like mine here in the UK). So toISOString gives you back 00:00 because your local time is GMT/UTC (that date/time is an hour before DST kicks in).
But in your second example, you're creating a Date in local time at midnight on 2022-03-28, when you're on DST. So you're offset from UTC by an hour (UTC is one hour behind you). Midnight 2022-03-28 UK time is 23:00 2022-03-27 UTC.
I don't know what you want to do with this stuff, but if you want to create a Date for midnight UTC on 2022-03-28, use new Date(Date.UTC(2022, 2, 28)).
When the US Daylight Saving ends, we switch our clocks back 1 hour and this makes the 1 AM hour happen twice on that day. However, JavaScript behaves strangely for timestamps in the second 1 AM hour (after clock switch) and keeps reverting them to the daylight time of 1 hour before. This can be seen when trying to use setMinutes and setHours methods.
This code sets the minutes of a date to its current minutes value, and behaves as expected (browser has America/Los_Angeles time zone):
let date = new Date("2021-07-07T09:40:00.000Z");
console.log(date.toLocaleString("en-us", {timeZoneName: "short"}));
// "7/7/2021, 2:40:00 AM PDT"
date.setMinutes(date.getMinutes());
console.log(date.toLocaleString("en-us", {timeZoneName: "short"}));
// "7/7/2021, 2:40:00 AM PDT"
But this code doesn't:
let date = new Date("2021-11-07T09:50:00.000Z"); // DST ended on Nov 7 at 2 AM
console.log(date.toLocaleString("en-us", {timeZoneName: "short"}));
// "11/7/2021, 1:50:00 AM PST"
date.setMinutes(date.getMinutes());
console.log(date.toLocaleString("en-us", {timeZoneName: "short"}));
// "11/7/2021, 1:50:00 AM PDT" <-- Wrong time zone
So the Date object can correctly print timestamps in the second (standard time) hour, but it interprets the date as still in daylight time when using setMinutes().
Is this expected behavior? It seems odd that date.setMinutes(date.getMinutes()) should alter the date.
I want to get how far away is the next occurence of a particular PST time regardless of the client's timezone.
This would be trivial if the time were in UTC but I don't know how to do it in PST keeping in mind the observance of daylight savings time.
Eg. 4 PM PST would be 11 PM UTC since it is right now summer.
I would prefer not to have to manually input the dates of daylight saving time.
I am happy to use a library if this is not possible without one.
// returns the number of milliseconds from the current time until the specified time in PST.
function getTimeUntil (hour, minutes = 0, seconds = 0)
{
// implementation needed
}
The following is an explanation of why this is likely a duplicate of How to initialize a JavaScript Date to a particular time zone.
PST (presumably US Pacific Standard Time) is a timezone with a fixed offset, UTC -8. Places that observe PST and have daylight saving typically call that offset Pacific Daylight Time (PDT), which is UTC -7.
PST might also be Pitcairn Standard Time, which is also UTC -8 and observed all year round on Pitcairn Island. Converting PST to UTC is achieved by adding 8 hours.
However, likely you want to work with times and dates for a place that observes US PST in winter and US PDT in summer, e.g. Los Angeles. In that case you can use a library like Luxon or date.js that allows creating dates based on a timestamp and specified IANA representative location such as "America/Los_Angeles". If that is the case, then see the link above.
My implementation:
// returns the formatted time from the current time until the specified time in PST.
function getTimeUntil (hour, minutes = 0, seconds = 0)
{
let future = luxon.DateTime.now().setZone('America/Vancouver').set({
hours: hour,
minutes: minutes,
seconds: seconds
});
let now = luxon.DateTime.now().setZone('America/Vancouver');
if (future < now)
{
future = future.plus({ days:1 });
}
return future.diff(now, ["hours", "minutes", "seconds"]);
// implementation needed
}
console.log(getTimeUntil(13, 0, 0).toObject());
<script src="https://cdn.jsdelivr.net/npm/luxon#2.0.1/build/global/luxon.min.js"></script>
My server is sending my Javascript client a timestamp in UTC. I'm currently in Mountain Time which is currently in daylight savings (GMT-7), but any timezone adjustment I do is only applying -6 offset.
To confirm that javascript is even aware of my timezone, I did the following:
console.log(Date().toString()); which outputs the following: Mon Nov 19 2018 12:13:28 GMT-0700 (Mountain Standard Time). It's clear that JS knows I am currently in GMT-7.
Now, my server is sending 2018-08-24T17:00:00. So I parse it with moment.js, convert to local timezone and then format the result.
moment.utc(this.props.value).local().format('h:mm A')
The resulting value is 11:00 AM. 17:00:00 - 7 offset is 10:00 which is 10:00 AM. Why is javascript converting into 11:00? I get the same result if I try the spacetime library:
const s = spacetime(this.props.value,'UTC')
s.goto(spacetime().timezone().name);
console.log(s.format('h:mm a')); // Also spits out 11:00 AM
If I manually adjust the moment object with the offset, it works correctly:
var m = moment.utc(this.props.value);
m.add(-(new Date()).getTimezoneOffset(), 'minutes');
console.log(m.format('h:mm A')) // Outputs the correct time: 10:00 AM
Why is moment and spacetime both not adjusting my timezone correctly when Javascript is clearly aware of what timezone I'm in? The same problem occurs in both Chrome and Microsoft Edge. For now I will use the hacky workaround above, but I'd prefer to use the native methods so I'm curious as to why this isn't working. Any ideas?
Mountain Standard Time is UTC-7
Mountain Daylight Time is UTC-6
It is currently November 19th, and DST is not in effect, so you currently get UTC-7
For the date you gave, 2018-08-24T17:00:00, DST is in effect, so you get UTC-6
Everything is working correctly.
In other words, it doesn't matter whether it is currently in effect or not, only whether it is/was/will be in effect for the date in question.
Regarding this part:
m.add(-(new Date()).getTimezoneOffset(), 'minutes');
Don't do that. You aren't adjusting the time zone, you're picking a different moment in time.
I am using moment-timezone library to build a UI that needs to be relative to a variety of timezones.
I am taking an input of a timezone, i.e "America/Chicago" and need to get the start of day in GMT.
For instance, if today is March 27th at 9am Chicago time (2pm GMT), I need to get the date in epoch seconds for March 27th, 00:00 AM.
I'm using the moment.tz("America/Chicago").startOf('day') but I keep getting Tue Mar 27 2018 01:00:00 GMT-0400 (EDT) . Any idea how to do this?
Thanks
// This part you are already doing correctly.
// You get back a Moment object representing the start of the current day in Chicago:
var m = moment.tz("America/Chicago").startOf('day');
I need to get the date in epoch seconds
// Ok, so simply now get the associated Unix Time
var timestamp = m.unix();
Also note that the correct terminology is "Unix Time", not "epoch seconds".
See my blog post: "Please don't call it Epoch Time".
... but I keep getting Tue Mar 27 2018 01:00:00 GMT-0400 (EDT)
You are probably either looking at _d or a Date object, or rather the string representation of one. Don't. See this Moment.js documentation for details.
Per your comment:
I need to take the current time in a specific timezone. I then need to convert that time to the corresponding day in GMT. Finally I need to get the midnight epoch timestamp of that GMT day.
That's a little different then you originally asked, but it would be like this:
var timestamp = moment.utc().startOf('day').unix();
Note that there's no purpose in involving another time zone for this operation. Logically, when asking for "Now in time zone A converted to time zone B", it's the same as asking for "Now in time zone B". In other words, you would get the same value even when the time zone was present:
var timestamp = moment.tz('America/Chicago').utc().startOf('day').unix();
So you're better off just leaving the time zone out.