As you can see, with moment lib, we need 2 steps to get the result: parse string to Date object, then format date object to string.
Your code - format(new Date("10-13-20"), 'MM-DD-YY') is format step, try convert a date object to a string with format template is MM-DD-YY. But your date object is not correct.
The solution is to do the same as with moment lib:
Parse date string to date object. Use parse
const dateString = '10-13-20'; const date = parse(dateString, 'MM-dd-yy', new Date()) // not MM-DD-YYFormat date object to result string. Use format
const result = format(date, "yyyy-MM-dd'T'HH:mm:ss.SSSxxx") console.log(result)Result will be like (the same with moment's result in my timezone):
2020-10-13T00:00:00.000+09:00
As you can see, with moment lib, we need 2 steps to get the result: parse string to Date object, then format date object to string.
Your code - format(new Date("10-13-20"), 'MM-DD-YY') is format step, try convert a date object to a string with format template is MM-DD-YY. But your date object is not correct.
The solution is to do the same as with moment lib:
Parse date string to date object. Use parse
const dateString = '10-13-20'; const date = parse(dateString, 'MM-dd-yy', new Date()) // not MM-DD-YYFormat date object to result string. Use format
const result = format(date, "yyyy-MM-dd'T'HH:mm:ss.SSSxxx") console.log(result)Result will be like (the same with moment's result in my timezone):
2020-10-13T00:00:00.000+09:00
const date = "2021-12-20"
console.log(format(parseISO(date), "dd-MM-yyyy"));
// output: 20-12-2021
» npm install date-fns
Started getting an issue when processing DateTime strings from a json array. I am reading in values from a list, specifically DateTime strings from a variable called record_date.
This string comes pre-formatted from another application, usually excel csv files that have been converted into json. I am attempting to convert this DateTime string into the "pp" format supplied by date-fns (12:00:00 AM).
So for instance, If I have a DateTime string that says 15/04/2021 11:28:00. I would like to format this as simple 11:28:00 AM, but I am getting an error that says "Invalid time value". I'm not really sure what is wrong with my code as I do all of my processing on a single line. I have tried processing it using a couple different methods, but they aren't working. Maybe someone here knows what's going wrong?My code;
item.record_date = format(new Date(parseISO(item.record_date)), "pp");
have also tried this;
item.record_date = format(parseISO(item.record_date), "pp");
and also this;
item.record_date = format(new Date(item.record_date), "pp");
<script type="module">
import { format } from 'https://esm.run/date-fns'
const date = new Date('Jul-30-2021');
console.log(date);
console.log(format(date, 'MMM-dd-yyyy, mm:HH:ss'));
</script>
Now you can apply the date-fns format(date, 'MMM-dd-yyyy')
this way :
const setDateMDY = dteSTR =>
{
let [m,d,y] = dteSTR.split('-')
return new Date(`${m} ${d}, ${y} 00:00:00`)
}
let date1 = setDateMDY('Jul-30-2021')
console.log( date1.toLocaleString() )
» npm install date-fns-tz