39

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.

What JavaScript or jQuery techniques could be used?

brasofilo
  • 25,496
  • 15
  • 91
  • 179
sozhen
  • 7,627
  • 14
  • 36
  • 53

4 Answers4

67

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

var timeStamp = ( moment('2009-07-15 00:00:00').unix() )*1000
shmigi
  • 15
  • 5
Grzegorz Kaczan
  • 21,186
  • 3
  • 19
  • 17
  • 2
    This library does make syntax cleaner and logic simpler. It seems well worth including this with a date/time focused node.js feature. 6KB minified, for those concerned with including this on the browser side. – awhie29urh2 Aug 08 '13 at 03:33
  • 3
    Really. After a whole lot of struggling with the other answers when I tried `moment.js`, it was an immediate peace of mind. Bless the person who created `moment.js`!!! – Nav Mar 24 '15 at 18:38
  • 1
    This works with NodeJs as well which is a huge plus. – The Dude Jun 07 '15 at 01:12
  • 1
    If Moment.js isn't out of the equation, this should be the accepted answer. Lightweight and super awesome. For those who imbibe Angular, there's an ng version as well (I'm spoiled by it's amCalendar filter, honestly). It looks like you're trying to get a 'time since x' - I used this for that, using moment: `Math.floor((new Date().getTime() / 1000) - moment(timestamp).unix());` – Aetiranos May 16 '16 at 14:50
  • 1
    I didn't need to multiply by 1000. The following returned a perfect timestamp: `moment('2009-07-15 00:00:00').unix()` – Anriëtte Myburgh Aug 17 '17 at 15:01
  • Sadly, moment.js has been deprecated. https://sebastiandedeyne.com/moment-js-thank-you-for-your-service/ – mpelzsherman Nov 10 '20 at 23:39
22

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

Community
  • 1
  • 1
Saket Patel
  • 6,573
  • 1
  • 27
  • 36
  • ok, firefox somehow isn't able to parse the dateformat, it expects only one of these formats http://www.w3.org/TR/NOTE-datetime – Saket Patel Mar 22 '13 at 20:46
  • yes, it works :) another way is to replace the dashes with slashes: http://codebins.com/bin/4ldqpas/3 – Sergiu Apr 01 '13 at 17:11
  • 1
    Your answer has just helped with fix an issue with my ap on LG webOS. for some reason their system cannot parse a typical date string such as yyyy-mm-dd hh:mm:ss without the "T" in there. – Mr Pablo Jul 23 '15 at 15:59
19
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.

Some Guy
  • 15,854
  • 10
  • 58
  • 67
0

using split method you can change the datetime formate in to date, its very simple and easy to everyone.

 var str = "2017-05-09T00:00:00.000Z";
 var res = str.split("-",2);
 var res = str.split("T",1);
 var temp=res;