0

I have a date like this :

var intervalStart="13-11-2014 16:22:00"

and an epoch time like this :

1415876254

I need to convert the string date into epoch to compare the two! Am kind of lost here. I looked at the other answers and by using :

var formattedDays = intervalStart.split(" ")[0].split("-");
var epoch = new Date(formattedDays[2], formattedDays[1] - 1, formattedDays[0]).getSeconds;

I tried something like this too :

var formattedDays = intervalStart.split(" ")[0].split("-");
var formattedTime = intervalStart.split(" ")[1].split(":");
var epochStart = new Date(formattedDays[2], formattedDays[1] - 1,formattedDays[0],formattedTime[0],formattedTime[1],formattedTime[2],0).getTime()/1000;

^ But this seems to be giving numbers like 16 etc!!

I can get the seconds for upto days("13-11-2014") correctly but how do i include time("16:22:00") here?Thanks a lot in advance!

P.S : I do not want to use some external library . And I would prefer no regex !

JJJ
  • 32,902
  • 20
  • 89
  • 102
joanOfArc
  • 467
  • 1
  • 6
  • 16

3 Answers3

1

If I understood your question correctly, you want the Epoch time in seconds and not in milliseconds. So just divide by 1000:

var epoch = Math.floor(new Date(...).getTime() / 1000);

What you did to get the Date object is correct.

MaxArt
  • 22,200
  • 10
  • 82
  • 81
1

You're really close, getSeconds is a method, not a property so:

var formattedDays = intervalStart.split(" ")[0].split("-");
var epoch = new Date(formattedDays[2], formattedDays[1] - 1, formattedDays[0]).getSeconds();

note the () after getSeconds. But seconds returns just the seconds part of the date, not seconds since epoch. For that you need ...getTime() / 1000 since getTime returns milliseconds since epoch.

You can do it more simply using:

var b = intervalStart.split(/\D+/);
var epoch = new Date(b[2], --b[1], b[0], b[3], b[4], b[5]) / 1000;

you can even do:

var after = new Date(b[2], --b[1], b[0], b[3], b[4], b[5]) / 1000 > 1415876254; // false

or

var after = new Date(b[2], --b[1], b[0], b[3], b[4], b[5]) > (1415876254 * 1000);

You're right to manually parse the string. Giving that format string to the Date constructor will not necessarily be consistently parsed by all browsers, and it will not be parsed at all by some.

RobG
  • 142,382
  • 31
  • 172
  • 209
-1

You can use this code :

<script>
    var d = new Date();//date you want to convert to timestamp
    alert(d.getTime());//this is how you can convert any date to timestamp
 </script>

in your case you can simple write:

<script>
var formattedDays = intervalStart.split(" ")[0].split("-");
var epoch = new Date(formattedDays[2], formattedDays[1] - 1, formattedDays[0]).getSeconds;
var timestamp = intervalStart.getTime();
</script>

for more info, please check this question. How do you get a timestamp in JavaScript?

Community
  • 1
  • 1
Talha
  • 41
  • 7
  • You missed the point. I want to include the "time" part in the date object . i know i can use .getTime() !!! – joanOfArc Nov 13 '14 at 11:16
  • And, in addition, because he says "I need to convert the string date into epoch to compare the two" – Mario Levrero Nov 13 '14 at 11:20
  • use this, i have also checked this. var d = new Date(); d.setFullYear(2014, 11, 13); // your date var n = d.getTime(); – Talha Nov 13 '14 at 11:25