1

I am working on an app, that I figure the best way to pass dates and times around is to use a unix timestamp as in the end of the whole process I have going on it makes an ajax call to PHP that passes these times to be used in mysql its easier to convert the timestamp to datetime format overall.

Currently I am being passed a date like 9/9/12 1:00 PM this is a date I have no control over the initial formatting. What I need to do is take this date/time format and convert it into a unix timestamp so I can work with it server side later when I pass it to a server with PHP on it. Ive been looking for a while now and I can't seem to find an answer for this, more so one that involves taking a specific time/date and passing that to something javascript side and converting it to a unix timestamp

chris
  • 36,115
  • 52
  • 143
  • 252
  • possible duplicate: http://stackoverflow.com/questions/1791895/converting-date-and-time-to-unix-timestamp – Hoàng Long Aug 03 '12 at 03:34
  • http://stackoverflow.com/questions/847185/convert-a-unix-timestamp-to-time-in-javascript – Hoàng Long Aug 03 '12 at 03:34
  • Close with the first link, gives me some idea of something to work with. I can splice my current time string to match that of the one in the first links answer, however if I can do parse like 2012-11-04 00:00:00 and make that a unix timestamp ill be golden – chris Aug 03 '12 at 03:47
  • 1
    Hi chris, as in the answers of the first question above, date.js is the solution you seek. I'm pretty sure that library is able to parse your format as well: http://www.datejs.com/ – Hoàng Long Aug 03 '12 at 04:21
  • @HoàngLong thats perfect! Yes very much so, can't say its as gratifying as coming up with my own solution, but it saves me time and is a perfect fit for what I need. Thanks! Feel free to post that as an answer to get the check. – chris Aug 03 '12 at 19:44
  • I'm glad that you find your answer :) If you feel it ok, just upvote the original answer – Hoàng Long Aug 04 '12 at 09:25

1 Answers1

0

I highly recommend using moment.js. To convert time to UNIX timestamp:

moment('9/9/12 1:00 PM', 'M/D/YY h:mm A').valueOf()

To convert time back to your format: moment(unixTimeStamp).format('M/D/YY h:mm A')

To use moment.js in the browser:

<script src="moment.js"></script>
<script>
    var unixTimeStamp = moment('9/9/12 1:00 PM', 'M/D/YY h:mm A').valueOf();
</script>

ref

FullStack
  • 5,902
  • 4
  • 43
  • 77