Mendi Sterenfeld
Mendi Sterenfeld

Reputation: 397

Momentjs get date and set format

for a weight tracking project i want to get a date from a user in his specific format eg:"05-12-19" , and i want to format it with momentjs to the standard javascript format

This code below is what i tried and think is the nearest to the result i want:

let newDate = moment().format("05-12-19","DD-MM-YYYY");
console.log(newDate); //05-12-19 

the result that i was expecting is 05-12-2019 but got something different take a look here (trying to meet stack-overflows quality standards lol)

Upvotes: 0

Views: 41

Answers (2)

Mendi Sterenfeld
Mendi Sterenfeld

Reputation: 397

after looking for a while i found a similair answer here. it wasn't exactly what i was looking for so i'll post here my complete answer:

let newDate = moment("05-12-19", "DD-MM-YY").format("DD-MM-YYYY");
console.log(newDate);

in the moment function the first argument is my date, the second argument is the format of this date, because momentjs doesn't know this format. in the format function i enter the date i want it to format to.

Upvotes: 0

TKoL
TKoL

Reputation: 13892

To create your date, something like this:

let newDate = moment("05-12-19","DD-MM-YY");
console.log(newDate.toDate());

to output your desired format

let newDateStr = moment("05-12-19","DD-MM-YY").format("DD-MM-YYYY");
console.log(newDateStr);

Upvotes: 1

Related Questions