Very odd issue using momentjs:
moment().startOf('month').toDate() produces:
Sun Nov 01 2020 00:00:00 GMT-0400 (Eastern Daylight Time)
moment().endOf('month').toDate() produces:
Mon Nov 30 2020 23:59:59 GMT-0500 (Eastern Standard Time)
Why is there a change of timezone? Why does start choose EDT and the endOf chose EST?
startOf('month') and endOf('month') returns the first and last date of the month in the browser's timezone.
On November 1st, 2020, daylight savings time ended in the USA, so up to November 1st, your browser's timezone will be EDT (if you're in the Eastern Timezone area), and after November 1st, it will be EST.
If you want to get a specific timezone-based time, you can use moment-timezone.
Related
I have what seems like a very common use case: I want to have a recurring event that occurs at the same time each day in a specific time zone (in the example below, 6:00 AM in the America/Denver time zone). I want this to recur at the same time of day after a change in Daylight Savings as before. Right now, it is changing by one hour after Daylight Savings, which seems to indicate that Daylight Savings is not being accounted for when the recurring datetimes are generated.
I have tried various configurations for the rrule as indicated in the documentation here and here. It says the time of day should be the same across Daylight Savings, but that is not what I am seeing.
Code sample
const rrule = new RRule({
freq: RRule.DAILY,
dtstart: new Date(Date.UTC(2022, 7, 18, 12, 0, 0)),
// tzid: 'America/Denver', // output is the same whether this is included or not
})
const datetimes = rrule.between(
new Date('2022-10-31'),
new Date('2022-11-10')
)
Try out the CodeSandbox. Should get similar results as long as you are in a time zone that has Daylight Savings and the between range includes a change in Daylight Savings.
Expected output
The time of day in America/Denver time zone should not change after Daylight Savings (i.e. recurrence should account for Daylight Savings):
Mon Oct 31 2022 06:00:00 GMT-0600 (Mountain Daylight Time)
Tue Nov 01 2022 06:00:00 GMT-0600 (Mountain Daylight Time)
Wed Nov 02 2022 06:00:00 GMT-0600 (Mountain Daylight Time)
Thu Nov 03 2022 06:00:00 GMT-0600 (Mountain Daylight Time)
Fri Nov 04 2022 06:00:00 GMT-0600 (Mountain Daylight Time)
Sat Nov 05 2022 06:00:00 GMT-0600 (Mountain Daylight Time)
Sun Nov 06 2022 06:00:00 GMT-0700 (Mountain Standard Time) <-- Daylight savings change
Mon Nov 07 2022 06:00:00 GMT-0700 (Mountain Standard Time)
Tue Nov 08 2022 06:00:00 GMT-0700 (Mountain Standard Time)
Wed Nov 09 2022 06:00:00 GMT-0700 (Mountain Standard Time)
^^
Actual output
The time of day in America/Denver time zone is changing from 6:00 to 5:00:
Mon Oct 31 2022 06:00:00 GMT-0600 (Mountain Daylight Time)
Tue Nov 01 2022 06:00:00 GMT-0600 (Mountain Daylight Time)
Wed Nov 02 2022 06:00:00 GMT-0600 (Mountain Daylight Time)
Thu Nov 03 2022 06:00:00 GMT-0600 (Mountain Daylight Time)
Fri Nov 04 2022 06:00:00 GMT-0600 (Mountain Daylight Time)
Sat Nov 05 2022 06:00:00 GMT-0600 (Mountain Daylight Time)
Sun Nov 06 2022 05:00:00 GMT-0700 (Mountain Standard Time) <-- Daylight savings change
Mon Nov 07 2022 05:00:00 GMT-0700 (Mountain Standard Time)
Tue Nov 08 2022 05:00:00 GMT-0700 (Mountain Standard Time)
Wed Nov 09 2022 05:00:00 GMT-0700 (Mountain Standard Time)
^^
I've opened an issue for this on GitHub, but I'm wondering if I'm just missing something. It seems like a common use case, so I would think I'd be able to find something out there about it. I did find a couple of SO questions about it here and here, but I'm already applying the solutions suggested.
Is this an actual bug in rrule or am I just missing something?
I had to dig very deep and surprisingly it seems like there is no bug. If you change your CodeSandbox to the following then it should work:
import { RRule } from "rrule";
import "./styles.css";
const rrule = new RRule({
freq: RRule.DAILY,
dtstart: new Date(Date.UTC(2022, 7 - 1, 18, 12, 0)), // See note 1
tzid: 'America/Denver',
})
const datetimes = rrule.between(
new Date('2022-10-31'),
new Date('2022-11-10')
)
const output = datetimes.map((d) => new Date( // See note 2
d.getUTCFullYear(),
d.getUTCMonth(),
d.getUTCDate(),
d.getUTCHours(),
d.getUTCMinutes(),
)).join('<br/>')
document.getElementById("app").innerHTML = output;
Notes:
The docs at https://github.com/jakubroztocil/rrule#timezone-support state that using new Date(2022, 7, ...) will produce unexpected timezone offsets. Instead use rrule's datetime function. Since this function currently is not working (see https://github.com/jakubroztocil/rrule/issues/551) we have to use new Date(Date.UTC(2022, 7 - 1, ...)) (which is excatly what the datetime function is doing).
The result will be a date in UTC which you have to interpret as a date in the timezone you specified in tzid. It seems very weird but it is stated in the docs at https://github.com/jakubroztocil/rrule#important-use-utc-dates that: Even though the given offset is Z (UTC), these are local times, not UTC times. Just to emphasize this my code example transforms the UTC dates to America/Denver dates by creating a new instance of Date using the UTC values (only works if your operating's system timezone is America/Denver).
I am in London (GMT time zone). We are currently in DST.
In the UK, Daylight Saving Time starts in March and ends in October, so Jan 1 1970 would have been outside DST.
However,
epochDate = new Date(0);
console.log(epochDate);
returns
Thu Jan 01 1970 01:00:00 GMT+0100 (Greenwich Mean Time)
I expected
Thu Jan 01 1970 00:00:00 GMT+0000 (Greenwich Mean Time)
Does Date(); return results in the current timezone?!
JavaScript Date(0) returns GMT+1 because, according to Wikipedia (and timeanddate.com), "the British Standard Time experiment, with Britain remaining on GMT+1 throughout the year… took place between 27 October 1968 and 31 October 1971"
I am getting date as "Thu Jun 11 00:49:35 IST 2015" from REST API.
When I try to convert it using new Date("Thu Jun 11 00:49:35 IST 2015"), it says "Invalid Date". Is there any other ways to convert it to Date object?
You can remove "IST" from the source and it will work :
var d = new Date("Thu Jun 11 00:49:35 2015");
result will be a jscript date with value of :
Thu Jun 11 2015 00:49:35 GMT+0200 (Central European Daylight Time)
JavaScript accepts these time zones:
Time Zone Description
UTC Coordinated Universal Time
GMT Greenwich Mean Time
EDT (US) Eastern Daylight Time
CDT (US) Central Daylight Time
MDT (US) Mountain Daylight Time
PDT (US) Pacific Daylight Time
EST (US) Eastern Standard Time
CST (US) Central Standard Time
MST (US) Mountain Standard Time
PST (US) Pacific Standard Time
Parse it to milliseconds and then back to date in the format that you want.
http://www.w3schools.com/js/tryit.asp?filename=tryjs_date_convert
you can try the following code
<p id="demo"></p>
<script>
var s="Thu Jun 11 00:49:35 IST 2015";
s=s.replace("IST","");
document.getElementById("demo").innerHTML =new Date(s);
</script>
</body>
</html>
I need to convert a String to a Date object.
The date string is delivered in the following format:
"2015-01-28T00:00:00"
When I create a new Date, I get the previous date:
Entered: new Date("2015-01-28T00:00:00")
Result: Tue Jan 27 2015 17:00:00 GMT-0700 (Mountain Standard Time)
Does anyone know why this is occurring?
When you enter the following:
new Date("2015-01-28T00:00:00");
// Result: Tue Jan 27 2015 17:00:00 GMT-0700 (Mountain Standard Time)
the browser assumes that you are giving a date in GMT Time zone. So it will automatically convert the given date to your local date.
It's always a good idea to inform the browser of the timezone you are working on in order to prevent future problems:
new Date("2015-01-28T00:00:00-07:00");
// Result: Tue Jan 28 2015 00:00:00 GMT-0700 (Mountain Standard Time)
Actually, you aren't getting the previous date . . . you are getting that date, offset by the timezone difference.
Tue Jan 27 2015 17:00:00(Mountain Time) + 7 hours (time zone difference) = 2015-01-28T00:00:00 (GMT)
Or, in English, when it is 12:00 Midnight in Greenwich, England, it is 5:00 PM on the previous day in Denver, Colorado. ;)
It's the right date/time, just in a different timezone.
I'm trying to return a date function, from
Mon Jul 30 2012 10:57:56 GMT 0100 (GMT Daylight Time)
to
30 July 2012 10:57:56
Tried out most of the date functions, but still no answer/format.
Do not invent a bicycle, use library for this: http://blog.stevenlevithan.com/archives/date-time-format