David Simon
David Simon

Reputation: 305

Convert date and time string to valid date

I have the following string.

"2021-07-01 10:00"

How can I convert this into a valid date with moment.js?

I tried the below, it says invalid date.

moment("2021-07-01 10:00", "YYYY-MM-DD HH:mm")

enter image description here

Upvotes: 0

Views: 26

Answers (2)

David Simon
David Simon

Reputation: 305

I realised, nothing is wrong with this script. I used a variable instead of the string and I realised that was an object: {_text: "2021-07-01 10:00"}

Upvotes: 0

joy08
joy08

Reputation: 9652

Try some something like this:

var res = moment("2021-07-01 10:00", "YYYY-MM-DD hh:mm").format(
  "YYYY-MM-DD hh:mm"
);
console.log(res);
 <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script> 

Upvotes: 1

Related Questions