0

I am having a lot of trouble converting a timestamp from Rails into something I can use in my Javascript.

Specifically, I have an alert set up to show the time of an event happening, and it produces a string like this: 2016-02-18T23:07:00.000-08:00. What's weird is that doesn't look like a UTC string to me (as this answer outlines), and I can't figure out what time format that is.

I have tried using this method:

  var t = "2016-02-18T23:07:00.000-08:00"
  var u = t.to_time 

But that returns "undefined." Ideally, I'd like to convert this string in Javascript to seconds or milliseconds. Is that possible?

Community
  • 1
  • 1
darkginger
  • 652
  • 1
  • 10
  • 38

2 Answers2

1
var t = "2016-02-18T23:07:00.000-08:00"
var u = Date.parse(t);
Andy
  • 49,085
  • 60
  • 166
  • 233
Hien Nguyen
  • 581
  • 1
  • 4
  • 9
  • Just tried the `Date.parse`, and unfortunately it is returning NaN. Is it possible that string is not parseable? – darkginger Feb 20 '16 at 02:55
  • I was tried it in here : https://jsfiddle.net/8ra4j287/ .I think you should convert to Int before calculate. `var sec = Number(u);` – Hien Nguyen Feb 20 '16 at 03:45
  • Gave it a shot. `var sec = Number(u)` returned NaN as well. I'll investigate and report back if I figure it out. – darkginger Feb 20 '16 at 05:05
0

Just use Moment.js:

moment('2016-02-18T23:07:00.000-08:00').format('LLL');

Returns:

February 19, 2016 8:07 AM

Or just a timestamp:

Date.parse('2016-02-18T23:07:00.000-08:00');

Works fine

https://momentjs.com

Franck Boudraa
  • 123
  • 1
  • 2
  • 14