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
2 Answers
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)
2 Comments
Luis Medina
What time format is the answer in?
Majed Badawi
It returns the number of ms since you did not specify a specific time format. You can reference to developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… and stackoverflow.com/questions/25275696/… in order get your desired format.
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'));
moment.js