-3

Here is my date string and i want to convert this into javascript date object.

Input: -- Wednesday, March 4th, 2020, 5:00:00 pm

Expected result: -- 2020-03-13T15:04:16.913Z'

I tried :

moment('Wednesday, March 4th, 2020, 5:00:00 pm').format('YYYY-MM-DDTHH:mm:ss\\Z')
Jeremy Thille
  • 26,047
  • 12
  • 43
  • 63

2 Answers2

0

You need to inform Moment about the format of the string you're giving it.

From the documentation / display :

Wednesday = dddd
March = MMMM
4th = Qo
2020 = YYYY
5:00:00 pm = h:mm:ss a

So yout input format is "dddd, MMMM Qo, YYYY, h:mm:ss a"

Now you can create a valid Moment and manipulate it.

moment('Wednesday, March 4th, 2020, 5:00:00 pm', "dddd, MMMM Qo, YYYY, h:mm:ss a").format(whatever)
Jeremy Thille
  • 26,047
  • 12
  • 43
  • 63
0

Firstly you need to inform moment the format of the expected input , followed by format() to convert the same into required format.

Based on their docs: https://momentjs.com/docs/#/displaying/

Check this code out. It will help you get started:

var input = "Wednesday, March 4th, 2020, 5:00:00 pm";
var date = moment(input, "dddd, MMMM Do, YYYY, h:mm:SSSS a").format(
  "YYYY-MM-DDTHH:mm:ss\\Z"
);
console.log(date);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script>
joy08
  • 9,004
  • 8
  • 38
  • 73