I have a retrieve data with a text plus date on it example below:
var textData = "Today is 2020-09-02 09:32:00+00:00"
let format = moment(date).format(MMM. DD, YYYY hh:ss A);
I want the output to be like this. Today is Sept. 02, 2020 9:32 PM
I have a retrieve data with a text plus date on it example below:
var textData = "Today is 2020-09-02 09:32:00+00:00"
let format = moment(date).format(MMM. DD, YYYY hh:ss A);
I want the output to be like this. Today is Sept. 02, 2020 9:32 PM
You need to find a match of a date like that (DD/MM/YYYY):
function parseDate(str) {
var m = str.match(/^(\d{1,2})\/(\d{1,2})\/(\d{4})$/);
return (m) ? new Date(m[3], m[2]-1, m[1]) : null;
}
Second. Get position of the match (date) like that.
var str = "this is a \"quoted\" string as you can 'read'";
var patt = /'((?:\\.|[^'])*)'|"((?:\\.|[^"])*)"/igm;
while (match = patt.exec(str)) {
console.log(match.index + ' ' + patt.lastIndex);
}
Third. Taken position of the date use for getting substring. Take a look here.
Using unusedInput
.
Note: Couldn't find a documented way to achieve this.
var textData = "Today is 2020-09-02 09:32:00+00:00"
let mdate = moment(textData, "YYYY-MM-DD HH:mm:ssZ");
console.log(mdate._pf.unusedInput.concat('').join(mdate.format("MMM. DD, YYYY hh:ss A")))
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.27.0/moment-with-locales.min.js" integrity="sha512-qSnlnyh7EcD3vTqRoSP4LYsy2yVuqqmnkM9tW4dWo6xvAoxuVXyM36qZK54fyCmHoY1iKi9FJAUZrlPqmGNXFw==" crossorigin="anonymous"></script>