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')
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')
Share
Improve this question
asked Jul 30, 2021 at 16:10
Stefano BorzìStefano Borzì
2,5165 gold badges24 silver badges41 bronze badges
3
- 2 What is the input & expected output? – Rajendran Nadar Commented Jul 30, 2021 at 16:21
- input: Jul-30-2021 output: Date object with the date Jul-30-2021 and 00:00:00 time or current time – Stefano Borzì Commented Aug 3, 2021 at 12:30
-
Try this
new Date('Jul-30-2021').toISOString()
after that you can use format from Date-fns to convert to any format. – Rajendran Nadar Commented Aug 3, 2021 at 12:37
2 Answers
Reset to default 6
<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() )