5 Replies - 441 Views - Last Post: 26 July 2014 - 09:58 AM

#1 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3462
  • View blog
  • Posts: 11,729
  • Joined: 12-December 12

Date constructor in US

Posted 26 July 2014 - 07:55 AM

Are you in the US? What date does this give you:
var day1 = new Date("2014-07-01");

Is it 30th June? What hour?

YYYY-MM-YY is the standard ISO date-format but apparently JS doesn't like the hyphens and we should use slashes '/' or one of the other JS-acceptable formats:

Date Creation in Javascript

The point is, I would rather it fail than give an incorrect date. Is there some logic behind this behaviour (decision?)?

This post has been edited by andrewsw: 26 July 2014 - 07:56 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Date constructor in US

#2 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 9195
  • View blog
  • Posts: 34,523
  • Joined: 12-June 08

Re: Date constructor in US

Posted 26 July 2014 - 08:02 AM

Is this just a complaint about javascript, or what?

Four types of constructors.. I typically use one.

var d = new Date();
var d = new Date(milliseconds);
var d = new Date(dateString);
var d = new Date(year, month, day, hours, minutes, seconds, milliseconds); 

www.w3schools.com/jsref/jsref_obj_date.asp
Was This Post Helpful? 0
  • +
  • -

#3 astonecipher  Icon User is offline

  • Major DIC Head
  • member icon

Reputation: 670
  • View blog
  • Posts: 2,946
  • Joined: 03-December 12

Re: Date constructor in US

Posted 26 July 2014 - 08:08 AM

Quote

Mon Jun 30 2014 20:00:00 GMT-0400 (Eastern Standard Time)


Comes up
Was This Post Helpful? 0
  • +
  • -

#4 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3462
  • View blog
  • Posts: 11,729
  • Joined: 12-December 12

Re: Date constructor in US

Posted 26 July 2014 - 08:11 AM

I'm just curious whether this happens and why.

It was suggested that 30th is returned in this article.

View Postastonecipher, on 26 July 2014 - 03:08 PM, said:

Quote

Mon Jun 30 2014 20:00:00 GMT-0400 (Eastern Standard Time)


Comes up

That's what I thought, so it takes the literal date and then adds the GMT offset. As I said, I'd rather it failed.
Was This Post Helpful? 0
  • +
  • -

#5 Dormilich  Icon User is online

  • 痛覚残留
  • member icon

Reputation: 3541
  • View blog
  • Posts: 10,226
  • Joined: 08-June 10

Re: Date constructor in US

Posted 26 July 2014 - 09:50 AM

View Postandrewsw, on 26 July 2014 - 05:11 PM, said:

That's what I thought, so it takes the literal date and then adds the GMT offset. As I said, I'd rather it failed.

that’s not a failure, rather a misinterpretation. the date string is parsed according to ISO 8601 and represents a date in GMT. if your browser then re-localises the output to your local date (which may be the calendar day before) that’s just consistent since the date(time) is still the same.

if you want a localised time, give Date() a timezone:
new Date('2014-07-01 EDT') // Tue Jul 01 2014 06:00:00 GMT+0200 (CEST)
new Date('2014-07-01')     // Tue Jul 01 2014 02:00:00 GMT+0200 (CEST)


This post has been edited by Dormilich: 26 July 2014 - 09:52 AM

Was This Post Helpful? 1
  • +
  • -

#6 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3462
  • View blog
  • Posts: 11,729
  • Joined: 12-December 12

Re: Date constructor in US

Posted 26 July 2014 - 09:58 AM

Thank you Dormilich, that makes sense.
(It was the article that threw me, seeming to suggest that this was odd behaviour.)

This post has been edited by andrewsw: 26 July 2014 - 10:28 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1