Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have date string in this format:

2009-07-15 00:00:00 - 2009-07-15 08:16:23

Could anyone help me to tweak the date string to unix timestamp into something like this:

1247634000 - 1247663783

I am using GMT-5 Timezone.

JavaScript or jQuery techniques would be great! Thanks in advance.

share|improve this question

3 Answers

up vote 6 down vote accepted
var input = "2009-07-15 00:00:00 - 2009-07-15 08:16:23";
input = input.split(" - ").map(function (date){
    return Date.parse(date+"-0500")/1000;
}).join(" - ");

Demo

Date.parse docs

Note: This won't work on old browsers since I'm using Array.map, but I think you should easily be able to shim it.

share|improve this answer
Oh man, I just noticed that OpenDNS is calling fiddle.jshell.net a phishing site... – honyock Jul 16 '12 at 18:13
1  
this does not work even or your demo in Firefox 19.0.2 – NeS Mar 21 at 22:53

in Javascript you can directly pass the string to Date object constructor, like

var date = new Date('2009-07-15 00:00:00'.split(' ').join('T'))

that will give you date object and to get timestamp from it you can do

date.getTime() / 1000

dividing by 1000 because getTime will give timestamps in milliseconds

Working Demo

NOTE:

Firefox is not able to parse the given date time format, so we need to convert it into proper format first, for that we just need to replace space between date and time component with 'T' that will make it a valid ISO 8601 format and firefox will be able to parse it

Reference:

Date.parse in MDN

ISO 8601 Date Format

Same question asked here

share|improve this answer
this does not work even or your demo in Firefox 19.0.2 – NeS Mar 21 at 22:53
ok, firefox somehow isn't able to parse the dateformat, it expects only one of these formats w3.org/TR/NOTE-datetime – SilentSakky Mar 22 at 20:46
1  
check updated demo link will work with firefox aswell – SilentSakky Mar 22 at 20:55
yes, it works :) another way is to replace the dashes with slashes: codebins.com/bin/4ldqpas/3 – NeS Apr 1 at 17:11

I strongly advise you to use Moment Date lib when working with dates in js. It really light and awesome.

moment('2009-07-15 00:00:00').unix()
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.