Javascript new Date -
there funny thing in javascript, not big deal know, why happening.
if this:
new date('2014-6-12') you'll get:
thu jun 12 2014 00:00:00 gmt-0600 (central america standard time) which totally fine if same using day format 'dd' instead of 'd' this:
new date('2014-06-12') you'll different result:
wed jun 11 2014 18:00:00 gmt-0600 (central america standard time)
because '2014-06-12' appears iso 8601 date format without time zone, it's treated utc (per es5) browsers not all. '2014-6-12' seen other format treated local.
es6 alter iso 8601 dates without timezone should treated local (per iso 8601). confused?
so not pass strings date constructor, it's behaviour inconsistent across browsers. write own simple parser:
// expect year-month-day, treat local date function parseymd(s) { var b = s.split(/\d+/); homecoming new date(b[0], --b[1], b[2]); } the above simple not test if date valid, takes line:
// expect year-month-day, treat local date function parseymd(s) { var b = s.split(/\d+/); var d = new date(b[0], --b[1], b[2]); homecoming d && d.getfullyear() == b[0] && d.getdate() == b[2]? d : nan; } javascript
No comments:
Post a Comment