Reputation: 2476
How can I convert using date-fns the following string into Date?
Jul-30-2021
(format MMM-DD-YYYY).
Using momentjs I can convert it using:
moment('Jul-30-2021', 'MMM-DD-YYYY')
Upvotes: 5
Views: 14899
Reputation: 5438
<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')
Upvotes: 6
Reputation: 22320
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() )
Upvotes: 5