1

I'm talking about ones encoded in the format in which the twitter API returns its dates, like...

"Tue Jan 12 21:33:28 +0000 2010"

The best thing I thought of was to try to slice it up with regexes to become something more like...

20100112213328,

but there's got to be a better way.

wwaawaw
  • 6,867
  • 9
  • 32
  • 42

3 Answers3

1

You can just make a new Date object using a string like "Tue Jan 12 21:33:28 +0000 2010".

var dateString = "Tue Jan 12 21:33:28 +0000 2010";
var twitterDate = new Date(dateString);

Then, you can simply use < and > to make comparisons.

var now = new Date();
if (now < twitterDate) {
    // the date is in the future
}
evan
  • 4,239
  • 1
  • 18
  • 18
  • woah!! Is it specified anywhere how those work? Or is it "specifically unspecified," like `localeCompare()`? – wwaawaw Nov 08 '12 at 08:37
  • I found this with a little googling: https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Date What specifically did you want to know? – evan Nov 08 '12 at 08:39
  • /me heads off to read it, waiting to be able to accept yr answer. – wwaawaw Nov 08 '12 at 08:39
  • 1
    @adlwalrus: The JS date object can be tricky at first [but here's some more details on the matter](http://stackoverflow.com/questions/12007379/what-makes-new-date-1000-a-valid-javascript/12007624#12007624), though I have to make a couple edits to be 100% accurate) – Elias Van Ootegem Nov 08 '12 at 08:41
  • @evan doesn't seem to mention the comparison operators. – wwaawaw Nov 08 '12 at 08:46
  • Not sure what you mean, are you asking about < and > ? – evan Nov 08 '12 at 08:50
  • @adlwalrus: not directly, I've edited it just now, so that it does, but it basically deals with how JS deals with a date object in comparisons, and arithmic operations. more specifically: in combination with the overloaded `+` operator (addition and string concatenation) and the equality operators. All of that stuff _and_ how the Date objects differ from other objects – Elias Van Ootegem Nov 08 '12 at 08:50
  • @EliasVanOotegem, that was a great link! Tha nks. – wwaawaw Nov 08 '12 at 08:51
  • See also: http://stackoverflow.com/questions/13285441/how-does-js-date-comparison-work/ – wwaawaw Nov 09 '12 at 04:17
1

JavaScript Date will correctly parse the Twitter date :

  var d1 = new Date ("Tue Jan 12 21:33:28 +0000 2010")
    , d2 = new Date ("Tue Jan 12 22:33:28 +0000 2010");

The you can compare these using the getTime method which converts to numeric form:

  if (d1.getTime() < d2.getTime())
  {
       //...
  }

Or, simply

if (d1 < d2)
{
}

If needs must, explicitly coerce to number:

if (+(d1) < +(d2))
{
}
Elias Van Ootegem
  • 74,482
  • 9
  • 111
  • 149
HBP
  • 15,685
  • 6
  • 28
  • 34
  • the `>`, `<`, `>=` and `<=` operators automatically coerce a Date object to numbers, as does `+(dateObject)`... your `if` statement is incorrect BTW: you're missing a closing `)`, and it'll always return true if d1 != 0: – Elias Van Ootegem Nov 08 '12 at 08:52
0

As far as the ECMAScript specification 5.1 goes (see 15.9.1.15), the only supported string interchange format for date-times is YYYY-MM-DDTHH:mm:ss.sssZ (along with shorter forms).

Otherwise (15.9.4.2):

If the String does not conform to that format the function may fall back to any implementation-specific heuristics or implementation-specific date formats.

I would use the regular expression based solution which is short and guaranteed to work anywhere; relying on the user's browser and localization parameters seems a little eerie to me.

Julien Royer
  • 1,419
  • 1
  • 14
  • 27