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

1
  • 1
    use moment.js Commented Apr 22, 2020 at 17:35

2 Answers 2

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)

Sign up to request clarification or add additional context in comments.

2 Comments

What time format is the answer in?
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.
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'));

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.