-1

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

nani10
  • 301
  • 4
  • 9
  • First step is to extract the date from the string. But as we don't know what kind of patterns you are dealing with, we can't really help. Also please read [How do I ask a good question?](https://stackoverflow.com/help/how-to-ask) – str Sep 02 '20 at 13:28

2 Answers2

0

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.

0

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>
User863
  • 19,346
  • 2
  • 17
  • 41