4

I'm having trouble using datajs with a date format from Google Calendar API. The datetime format I believe is RFC3339 and this is a sample datetime returned from the calendar api

2012-01-05T08:45:00Z

This is from the datejs documentation here

Date.parse('1985-04-12T23:20:50Z')          // RFC 3339 Formats

But this just returns null.

I'm assuming I have datejs working correctly as

Date.today().next().friday() 

returns Fri May 11 2012 00:00:00 GMT+0100 (BST)

Matt Price
  • 34,499
  • 6
  • 24
  • 33
  • What browser and browser version you use? IE7-8? `Date.parse('1985-04-12T23:20:50Z')` return `NaN` in IE7-8. In FF correct value returned. – Andrew D. May 10 '12 at 09:49
  • @AndrewD. I'm using the JavaScript console in Chrome – Matt Price May 10 '12 at 09:52
  • @mplungjan thanks but that doesn't help as I want to use datejs and datejs should be able to use 1985-04-12T23:20:50Z. If you go to datejs.com you can enter a date on their website and it will work fine. – Matt Price May 10 '12 at 09:56

1 Answers1

7

SOLVED: Use Date.parseExact OR this version according to this Bug report

DEMO using date.js

DEMO using date-en-US.js


Using the first version I get

null
http://datejs.googlecode.com/files/date.js
Line 13

when I take the assertions out of the testsuite:

// null
console.log('Date.parse("1985-04-12T23:20:50Z")',
  Date.parse('1985-04-12T23:20:50Z'));


// my previous answer works
console.log('Date.parse("1985-04-12T23:20:50Z".replace(...))',
     Date.parse('1985-04-12T23:20:50Z'
           .replace(/\-/g,'\/')
           .replace(/[T|Z]/g,' ')
     )
  );

// the test suite without the Z works
console.log('1985-04-12T23:20:50',
  new Date(1985,3,12,23,20,50).equals( Date.parse('1985-04-12T23:20:50')));

// but this one fails when not in the test suite    
try {
  console.log('1985-04-12T23:20:50Z',
    new Date(1985,3,12,23,20,50).equals( Date.parse('1985-04-12T23:20:50Z')));
}
catch(e) {
     console.log('1985-04-12T23:20:50Z',e.message);
}

Here is an older answer for this issue when not using date.js

Community
  • 1
  • 1
mplungjan
  • 169,008
  • 28
  • 173
  • 236
  • It seems that Date.parseExact("1985-04-12T23:20:50Z", "yyyy-MM-ddTHH:mm:ssZ") works ok. – Matt Price May 10 '12 at 11:38
  • According to http://www.datejs.com/test/date_and_time/index.html Its Much faster with Date.parseExact("1985-04-12T23:20:50Z", "yyyy-MM-ddTHH:mm:ssZ") – Matt Price May 10 '12 at 11:40
  • So either use the above version or use parseExact. I do not think speed is an issue here. – mplungjan May 10 '12 at 12:08