workflowproducts / envelope

Publish web apps based on your PostgreSQL database fast!
https://workflowproducts.com/envelope.html
Apache License 2.0
53 stars 2 forks source link

GS-Datetime One Day Off #122

Closed michaeltocci closed 6 years ago

michaeltocci commented 6 years ago

The built-in Javascript date parser has an issue that most of our code takes into account. However, gs-datetime doesn't have the fix.

Here is an example of a date being parsed correctly:

console.log(new Date('2018/05/05')); 
// returns: Sat May 05 2018 00:00:00 GMT-0500 (Central Daylight Time)

Here is an example of a date being parsed incorrectly:

console.log(new Date('2018-05-05')); 
// returns: Fri May 04 2018 19:00:00 GMT-0500 (Central Daylight Time)

When a date that is delimited with slash characters is parsed, it parses correctly. However, if dash is the delimiter (as is the default for Postgres), the date is parsed and ends up one day off.

To fix this, I always replace the first two dashes with slashes. Like this:

var strDate = '2018-05-05';
strDate = (
    strDate
        .replace(/\-/, '/')
        .replace(/\-/, '/')
);
console.log(new Date(strDate));

So, we need a simple date parsing utility function in greyspots.js that's just a wrapper for new Date() and we need to use it in gs-datetime to prevent further errors. In the future, instead of writing the code above for every new date function or element, we'll just use the new function.

crosstocci commented 6 years ago

Fixed in next release. the function is called GS.newDate