62

I'm displaying the date and time like this

24-Nov-2009 17:57:35

I'd like to convert it to a unix timestamp so I can manipulate it easily. I'd need to use regex to match each part of the string then work out the unix timestamp from that.

I'm awful with regex but I came up with this. Please suggest improvements ^.^

/((\d){2}+)-((Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)+)-((\d){4}+) ((\d){2}+):((\d){2}+):((\d){2}+)/gi

How can I do this?

Ben Shelock
  • 20,154
  • 26
  • 92
  • 125
  • Complete side comment, but everytime I see your name I think "Ben Shalock Lock Ben" (Ref: http://www.discogs.com/House-Of-Pain-Shamrocks-And-Shenanigans-Boom-Shalock-Lock-Boom/release/229941 :) – Crescent Fresh Nov 24 '09 at 18:16
  • 1
    My evil twin brother, we don't talk about him. – Ben Shelock Nov 24 '09 at 18:18
  • Duplicate: http://stackoverflow.com/questions/1457607/how-to-convert-a-gmt-timestamp-to-unix-timestamp-javascript and here http://stackoverflow.com/questions/1416296/parsing-a-date-in-long-format-from-atom-feed –  Nov 24 '09 at 18:49
  • 1
    You can remove the '+' on the regex groupings. That means "1 or more times" and you only want the group to match one time. – Marco Nov 24 '09 at 18:54

4 Answers4

92

If you just need a good date-parsing function, I would look at date.js. It will take just about any date string you can throw at it, and return you a JavaScript Date object.

Once you have a Date object, you can call its getTime() method, which will give you milliseconds since January 1, 1970. Just divide that result by 1000 to get the unix timestamp value.

In code, just include date.js, then:

var unixtime = Date.parse("24-Nov-2009 17:57:35").getTime()/1000
Ian Clelland
  • 43,011
  • 8
  • 86
  • 87
  • 6
    You don't even need the library to parse that format, do you? – Dexter Nov 24 '09 at 18:35
  • 1
    It's worth noting that without date.js, Date.parse will take a string and return a unix timestamp. But it's very strict about the format. In the OP's example, you would need to string.replace('-', '/'). Other than that, I'm pretty sure it would work in all modern browsers. I agree that date.js is much more reliable though. – Marco Nov 24 '09 at 18:59
  • 4
    stick to your regexp. A localized javascript library is overkill if you are using a fixed date format. If you have control over this format, you should consider using something more neutral and easier to parse, like ISO8601. – peller Nov 24 '09 at 20:49
  • 1
    @Marco: I didn't even bother trying the native Date.parse method -- That exact format works in Chrome, but returns NaN in Firefox (it works with "/" in both, though) – Ian Clelland Nov 24 '09 at 21:49
  • Ahh - I only tested in Chrome.. that explains my confusion! – Dexter Nov 26 '09 at 15:44
  • 38
    `Date.parse("24-Nov-2009 17:57:35").getTime` isn't a function. All you need is `Date.parse("24-Nov-2009 17:57:35")/1000`. Why did this get so many upvotes when its the WRONG answer? – B T Oct 12 '15 at 00:14
  • 1
    Date.parse() isn't entirely bulletproof. Here's some info on cross-browser Date.parse() behavior: http://dygraphs.com/date-formats.html. In my case, I learned the hard way that a date time string in the form of "2016-08-09 05:00 PM" will yield NaN in Firefox 47. – gps Aug 11 '16 at 00:12
  • 1
    That's why the answer is actually recommending *not* using the browser's Date.parse(), but rather using date.js, which provides a more robust alternative. – Ian Clelland Aug 11 '16 at 12:17
67

Seems like getTime is not function on above answer.

Date.parse(currentDate)/1000
chovy
  • 72,281
  • 52
  • 227
  • 295
7

You can use Date.getTime() function, or the Date object itself which when divided returns the time in milliseconds.

var d = new Date();

d/1000
> 1510329641.84

d.getTime()/1000
> 1510329641.84
Xeoncross
  • 55,620
  • 80
  • 262
  • 364
0

Using a date picker to get date and a time picker I get two variables, this is how I put them together in unixtime format and then pull them out...

let datetime = oDdate+' '+oDtime;
let unixtime = Date.parse(datetime)/1000;
console.log('unixtime:',unixtime);

to prove it:

let milliseconds = unixtime * 1000;
dateObject = new Date(milliseconds);
console.log('dateObject:',dateObject);

enjoy!

David White
  • 621
  • 1
  • 10
  • 23