question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

toISOString() behaviour changed in v3

See original GitHub issue
const newSchedule = '*/10 * * * *';
const tz =  'America/New_York';

const interval = CronParser.parseExpression(newSchedule, { tz });

const prev = interval.prev();
console.error({ prev, 'prev.toISOString()': prev.toISOString() });

V2 output:

{
  prev: CronDate { _date: Moment<2021-01-11T06:00:00-05:00> },
  'prev.toISOString()': '2021-01-11T11:00:00.000Z'
}

V3 output:

{
  prev: CronDate {
    _date: DateTime {
      ts: 1610362800000,
      _zone: [IANAZone],
      loc: [Locale],
      invalid: null,
      weekData: [Object],
      c: [Object],
      o: -300,
      isLuxonDateTime: true
    }
  },
  'prev.toISOString()': '2021-01-11T06:00:00.000-05:00'
}

I can hack it to get the previous behaviour by doing prev._date._getUTC().toISOString(), but that’s a bit of a hack. Less of a hack but a bit of a pain could be something like (new Date(prev.getTime())).toISOString();

One can argue whether the previous behaviour was right or wrong (I can argue both sides) as it is arguably odd passing in a time with timezone and ultimately getting out a date in UTC format, but I’d still like to get to the date and turn it into UTC somehow anyway.

Ways out would be to have an official way to get a luxon date out (no underscore methods) or to add a utc() or getUTC() method (unsure which name would be best), etc. Or maybe that method should return a CronDate to keep luxon hidden?

I’m wondering if this change should be documented as well.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
harrisiirakcommented, Jan 11, 2021

@dmitry-yudakov thanks, also a good point.

This can be possibly solved by passing the exactly the same format (defined ECMA-262 - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toString#description) to toFormat function and return its results instead:

CronDate.prototype.toString = function() {
  return this._date.toFormat('EEE MMM d yyyy HH:mm:ss \'GMT\'ZZ \'(\'ZZZZZ\')\'');
};

Or well, even a simpler solution:

CronDate.prototype.toString = function() {
  return this.toDate().toString();
};

Created a quick commit with changes: https://github.com/harrisiirak/cron-parser/commit/65f6f29

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why the toISOString() applied on a Date object generate a ...
As per the docs, toISOString() always returns the date in UTC+0 timezone, so you have to search for a different conversion method. –...
Read more >
Date.prototype.toISOString() - JavaScript - MDN Web Docs
A string representing the given date in the ISO 8601 format according to universal time. It's the same format as the one required...
Read more >
Ionic 3.1.0 changes ion-datetime functionality
Ionic team didn't put it as a breaking change, but ion-datetime behavior has changed a bit. ... Guide: How to update to Ionic...
Read more >
Moment.js moment().toISOString() Method - GeeksforGeeks
This behavior can be disabled by passing true to the keepOffset parameter. Note: The library will try to use the native Date toISOString()...
Read more >
Client side date operation issue converting from date to ...
If I use toISOString() then I will get a string representation of the date but ... 3- Change client side code before saving...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found