0

I am querying facebook graph api. It returns date in following format: 2012-01-23T23:52:29+0000.

I need to find difference of dates of this type in javascript. It's not a valid date in javascript ( by Date.parse() or new Date() )

I am thinking of replacing 'T' with ' ' (a space), '-' with '/' and '+0000' with '' (empty string). Is this the only way? Or am I missing something here?

Also, if this is the only way, can someone give me a regex to replace all in one go?

Execution speed is my main concern.

nnnnnn
  • 147,572
  • 30
  • 200
  • 241
Jashwant
  • 28,410
  • 16
  • 70
  • 105
  • 1
    Does speed really matter *that* much? (If so, it would be interesting to see a jsperformance test-case for it.) The time format shown is ISO8601 (keyword), btw. –  Jan 25 '12 at 05:32
  • When you say speed is the main factor, do you mean execution speed? If so, are you seeing any performance issues? – Anurag Jan 25 '12 at 05:33
  • 2
    possible duplicate of [How can I convert a date value in UTC format to a date object in Javascript?](http://stackoverflow.com/questions/498578/how-can-i-convert-a-date-value-in-utc-format-to-a-date-object-in-javascript) – Philip Fourie Jan 25 '12 at 05:34
  • 1
    A search for "Javascript ISO 8601" reveals: https://github.com/csnover/js-iso8601 Also: http://anentropic.wordpress.com/2009/06/25/javascript-iso8601-parser-and-pretty-dates/ – Josh Jan 25 '12 at 05:35
  • Thanks for timeformat. By speed, I meant the calculation must be fast. I am doing 3-4 javascript replace() here. Isnt there any way faster than this ? – Jashwant Jan 25 '12 at 05:35
  • 1
    @PhilipFourie , Yes that questions solves my problem. Thanks alot guys. 'Pst', 'Josh' thanks for your time. Github one seems short and nice. – Jashwant Jan 25 '12 at 05:39
  • If a general sense, if you have a known input format that JS won't handle you can easily create a JS date manually by using `.substr()` to extract the pieces from your input. As far as execution speed, several replaces won't be a problem on a string that is only 24 characters long... – nnnnnn Jan 25 '12 at 05:39
  • @nnnnnn thanks for editing and your suggestion. I'll keep this in mind. – Jashwant Jan 25 '12 at 06:31
  • possible duplicate of [How can I convert datetime microformat to local time in javascript?](http://stackoverflow.com/questions/436374/how-can-i-convert-datetime-microformat-to-local-time-in-javascript) – outis Jan 25 '12 at 21:53

1 Answers1

2

I'd say yes to replacing - with /, since that's that the ISO-whatever standard dictates (Facebook likes to screw things around, like <meta> tags with property attributes instead of name like they should be).

Keep the timezone part, since JS understands that and will handle it accordingly.

Overall, you want new Date(input.replace(/-/g,'/'));.

In response to comments, a better (more complete) solution would be:

new Date(input.replace(/-/g,'/').replace("T"," ").replace(/\+[0-9]+$/,''));
Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592
  • It throws invalid date in firefox 9.0. Works in chrome though – Jashwant Jan 25 '12 at 05:44
  • Do you have a regex for firefox too ? – Jashwant Jan 25 '12 at 05:58
  • `new Date(input.replace(/-/g,'/').replace(/T|\+[0-9]+/g,''));` – Niet the Dark Absol Jan 25 '12 at 05:59
  • http://en.wikipedia.org/wiki/ISO_8601 ISO 8601 have '-' in place of '/'. Its standard. This time, facebook does not screw. Firefox example is not working. 'T' should be replaced with ' ' not ''. Thanks Kolink, you made me realise that regex it not magic. I too can learn it. I thought regex can do all replacements in one go. So fool of me :( – Jashwant Jan 25 '12 at 06:15
  • Huh, insteresting. So the browsers are all wrong? Strange... Starting to wish `Date.parse()` were as good as PHP's `strtotime()`... – Niet the Dark Absol Jan 25 '12 at 06:17
  • can you edit your original answer to suit firefox too, so that I can mark it as answer ? – Jashwant Jan 25 '12 at 06:21
  • Done - note that I've put the `T` replacement as a string replacement, since there's only one T and it can't be anything else there. – Niet the Dark Absol Jan 25 '12 at 06:23