1

I have an arry of strings like this "2020-04-22T09:05:28.774000+00:00", how can I convert this to a datetime and operate it with the current time? Thank you for the help

Luis Medina
  • 535
  • 3
  • 15

2 Answers2

2

let list = ["2020-04-22T09:05:28.774000+00:00","2020-03-22T09:05:28.774000+00:00"]
let diff = [];
for(let i = 0; i < list.length; i++)
     diff.push(new Date() - Date.parse(list[i]));
console.log(diff)
Majed Badawi
  • 27,616
  • 4
  • 25
  • 48
  • What time format is the answer in? – Luis Medina Apr 22 '20 at 17:51
  • It returns the number of ms since you did not specify a specific time format. You can reference to https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date and https://stackoverflow.com/questions/25275696/javascript-format-date-time in order get your desired format. – Majed Badawi Apr 22 '20 at 17:55
0

Pass it as an argument to new Date(string), since your string is a valid ISO format into a Date object. You can also use Date.parse(string), which will convert it to a UTC timestamp:

// Returns Date obkect
console.log(new Date('2020-04-22T09:05:28.774000+00:00'));

// Returns ms elapsed since unix epoch
console.log(Date.parse('2020-04-22T09:05:28.774000+00:00'));
Terry
  • 63,248
  • 15
  • 96
  • 118