Date fail
#1 : 04/07-25 22:30 Chris
Posts: 38
|
This script fails:
const e = new Date("14 Jun 2017 00:00:00 PDT"); return e.getTime(); // Expected output: "1497423600000" // Observed output: "-56423865600000" Expected output is from Google Chrome JS Console https://i.imgur.com/dyF5NqH.png and runjs: https://runjs.app/play/#Y29uc3QgZSA9IG5ldyBEYXRlKCIxNCBKdW4g MjAxNyAwMDowMDowMCBQRFQiKTsKY29uc29sZS5sb2coZS5nZXRUaW1lKCkp OwovLyBFeHBlY3RlZCBvdXRwdXQ6ICIxNDk3NDIzNjAwMDAwIgovLyBPYnNl cnZlZCBvdXRwdXQ6ICItNTY0MjM4NjU2MDAwMDAiCiAgICAgICAgICAgICAg ICAgICAgICAgICAgICAgICAgICAgICAgICAg Any ideas? PS This is on Windows 7. Though AR does not object, the web site does say Windows 10 is required. However the fact Chrome on this OS doesn't suffer the problem suggests to me the OS is not to blame. Also, I have UK regional settings. PPS This fail is from Date() rather than getTime(): return new Date("14 Jun 2017 00:00:00 PDT"); // Expected output: "Wed Jun 14 2017 08:00:00 GMT+0100 (British Summer Time)" (from Windows 7 Google Chrome JS console) // Observed output: "Mon Dec 31 0181 00:00:00 GMT +0000" https://i.imgur.com/CvxncHR.png PPPS Workaround: add a leading space: https://i.imgur.com/mEjBvcR.png |
#2 : 05/07-25 21:35 Kim Jensen
Administrator
Posts: 1002 |
Reply to #1:
On my system this works perfectly fine: app.log(new Date("14 Jun 2017 00:00:00 PDT")); But for some reason it doesn't work as expected for you. I don't know why, but I notice the timestamp in question has a language specific date notation and I don't know how well that works or which requirements this functionality has. My suggestion is to try converting the string to a format without months in text. Here is a method I quickly mixed together, that will parse a RFC 5322 date string and return it as a Date object. You can put it into the pre script. Note, it doesn't support time zones, so it might be off by a couple of hours (9 in my case). function rfc5322ToISO(rfcStr) { const months = { Jan: 0, Feb: 1, Mar: 2, Apr: 3, May: 4, Jun: 5, Jul: 6, Aug: 7, Sep: 8, Oct: 9, Nov: 10, Dec: 11 }; // Remove optional weekday part (e.g., "Tue, ") const parts = rfcStr.replace(/^\w{3},\s*/, "").split(" "); if (parts.length < 4) throw new Error("Invalid RFC 5322 date format"); const day = parseInt(parts[0], 10); const month = months[parts[1]]; const year = parseInt(parts[2], 10); const [hour, minute, second = "00"] = parts[3].split(":").map(Number); return new Date(year, month, day, hour, minute, second); } |
#3 : 06/07-25 19:18 Chris
Posts: 38
|
Reply to #2:
Thanks for testing it! > On my system this works perfectly fine: > app.log(new Date("14 Jun 2017 00:00:00 PDT")); Here, fails: https://i.imgur.com/Zwg5NIe.png Interesting to note the format discrepancy, your app.log v. my return. > I notice the timestamp in question has a language specific date notation and I don't know how well that works or which requirements this functionality has. That's disproved as the cause by the fact just adding a leading space avoids the fail: https://i.imgur.com/mEjBvcR.png > Here is a method I quickly mixed together, that will parse a RFC 5322 date string and return it as a Date object. Thanks. Appreciated. This is probably the safer workaround than the leading space, give this from the JS spec: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe rence/Global_Objects/Date#date_time_string_format The JavaScript specification only specifies one format to be universally supported: the date time string format, a simplification of the ISO 8601 calendar date extended format. The format is as follows: YYYY-MM-DDTHH:mm:ss.sssZ albeit: However, there are some formats that are supported in all major implementations — like RFC 2822 format — in which case their usage can be acceptable. |