One way to do this might be to do a simple split()
on the T
character, and only taking the first segment:
var dateAndTime = "2020-05-18T10:11:08Z";
var date = dateAndTime.split('T')[0];
console.log(date);
In a similar text manipulation vein, it’d also be possible to do something similar using RegExp, which might be helpful if you’d like to stick with the pure text manipulation methodology, but the rules by which you’re manipulating the date text could become slightly more complex in the future:
var dateAndTime = "2020-05-18T10:11:08Z";
var date = /\d{4}-\d{2}-\d{2}/g.exec(dateAndTime)[0];
console.log(date);
1
solved how to remove some characters from string? [duplicate]