-1

I have the following timestamp (example) 20131024010907

that I would like to convert to date format. I will be subtracting the time stamp from another timestamp. For example:

20131024010907 - 20131024010856 = 11 seconds have elapsed

The problem is that if I subtract the timestamps as is, it will not work properly. E.g.

var x = 20131024010907
var y = 20131024010856
x-y != 11
x-y = 51

How would I convert these timestamps to dates in javascript so I can subtract them from each other? Is this possible?

user2041458
  • 97
  • 1
  • 2
  • 7
  • The format seem to be close to "iso8601", but without separators at all. You need to verify what format actually represents and than should be trivial to write code to split the string into components. – Alexei Levenkov Oct 28 '13 at 06:39
  • possible duplicate of [Formatting a date in JavaScript](http://stackoverflow.com/questions/1056728/formatting-a-date-in-javascript) – Ramesh Rajendran Oct 28 '13 at 06:40

2 Answers2

4

Those don't look like "timestamp" values to me in the usual sense (seconds or milliseconds since The Epoch [Jan 1, 1970 at midnight GMT]), they look like packed date/time strings:

20131024010907
yyyyMMddHHmmss

If so, you just split up the string and use the new Date(year, month, day, hour, minute, second) constructor:

var x = parseDate("20131024010907");
var y = parseDate("20131024010856");

console.log("x - y = " + (x - y) + "ms"); // 11000ms = 11 seconds

function parseDate(str) {
    return new Date(
        parseInt(str.substring(0, 4), 10),
        parseInt(str.substring(4, 6), 10) - 1, // Months start with 0
        parseInt(str.substring(6, 8), 10),
        parseInt(str.substring(8, 10), 10),
        parseInt(str.substring(10, 12), 10),
        parseInt(str.substring(12), 10)
    );
}

Live Example | Source

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • +1. Would be so much easier if OP have normal ISO8601: `var dt=new Date("2012-10-23T12:33")`... – Alexei Levenkov Oct 28 '13 at 06:41
  • @AlexeiLevenkov: :-) FWIW, on implementations that support the new (as of ECMAScript5) ISO-8601 subset, the `Date` constructor supports it directly, no need to use `Date.parse` first. But sadly, IE8 doesn't support that format, and still has over 20% of the general browsing market (and will do for some time, since it's the highest version of IE for XP). – T.J. Crowder Oct 28 '13 at 06:50
1

You should convert your timestamps to almost-ISO dates, and then use the Date constructor:(fiddle: http://jsfiddle.net/6d6hU/)

var ts = "20131024010907";

var isoTs = ts.substring(0,4) + "-" + ts.substring(4,6) + "-" + ts.substring(6,8) + " " + ts.substring(8,10) + ":" + ts.substring(10, 12) + ":" + ts.substring(12)

console.log(isoTs)
console.log(new Date(isoTs))

And something even easier. Use moment.js to parse the date, with a format that matches your timestamp: http://momentjs.com/docs/#/parsing/string-format/

pax162
  • 4,735
  • 2
  • 22
  • 28
  • Not a bad idea, but sadly IE8 doesn't support that format, but still has a big chunk (more than 20%) of the general browsing market (and will do for some time, thanks to being the highest IE version available for XP). – T.J. Crowder Oct 28 '13 at 06:48