Reputation: 305
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")
Upvotes: 0
Views: 26
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
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