0

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)

Mendi Sterenfeld
  • 378
  • 5
  • 26
  • Possible duplicate of [format date with moment.js](https://stackoverflow.com/questions/15993913/format-date-with-moment-js) – Dalorzo May 24 '19 at 16:12
  • @Dalorzo he's trying to CREATE a date from a string, not OUTPUT a date to a string – TKoL May 24 '19 at 16:16

2 Answers2

1

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);
TKoL
  • 13,158
  • 3
  • 39
  • 73
0

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.

Mendi Sterenfeld
  • 378
  • 5
  • 26