Nie Selam
Nie Selam

Reputation: 1451

momentjs format returns 0000-00-00

I am trying to format a string date to yyyy-mm-dd. Typically, it comes as d/m/yyyy but regardless of the input, I want to have it as yyyy-mm-dd.

const date = new Date("13/10/2016");
const DateParsed = moment(date).format("YYYY-MM-DD");

const dateParsed2 = moment("13/10/2016").format('YYYY-MM-DD')

I was expecting 2016-10-13 but I'm getting 0000-00-00 instead.

Upvotes: 0

Views: 1757

Answers (1)

VincenzoC
VincenzoC

Reputation: 31482

Since your input is not in a format recognized by Date.parse, I suggest to use moment(String, String) instead of new Date().

Here a live sample:

const date = "13/10/2016";
const DateParsed = moment(date, 'DD/MM/YYYY').format("YYYY-MM-DD");
console.log(DateParsed);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script>

Upvotes: 1

Related Questions