I've been trying to format a date using date-fns but I keep failing. Basically I have it working just fine with momentJS but not with date-fns:
Here's my date:
"10-13-20" // month, day, and year
Now with momentJS it works just fine like this:
let result = moment("10-13-20", 'MM-DD-YY').format()
// result = "2020-10-13T00:00:00-06:00"
So I'm trying to do the same using date-fns but no luck. Can anyone point me in the right direction? Thanks in advance!
let result = format(new Date("10-13-20"), 'MM-DD-YY') // Not working
As you can see, with
momentlib, we need 2 steps to get the result: parse string toDateobject, then format date object to string.Your code -
format(new Date("10-13-20"), 'MM-DD-YY')isformatstep, try convert a date object to a string with format template isMM-DD-YY. But your date object is not correct.The solution is to do the same as with
momentlib:Parse date string to date object. Use parse
Format date object to result string. Use format
Result will be like (the same with moment's result in my timezone):