I am trying to convert this format of duration to unix time stamp eg. ( 2h, 2m, 2s)
I could do this in moment lib with the valueof syntax. how to do the same in date-fns lib.
return moment(value,'M[M], d[d], h[h], m[m], s[s]').valueOf();
or if we can do it using plain javascript.
I have to write a custom asc/desc duration function so need to compare 2 durations using date-fns or reg javascript.
I tried the same with parse but shows Invalid date.
const dateString = '2h, 2m, 2s';
const date1 = parse(dateString, 'HH[HH], mm[mm], ss[ss]', new Date())
console.log(date1);
You had a few issues with your code:
h
, m
, and s
for the literals, not double letters.'X'
), not brackets ([X]
).<script type="module">
// NOTE: The line below is just to make the code work here on the
// Stack Overflow site. In your own code, you can import from `date-fns`
// however you were already planning to do it.
import { parse } from 'https://cdn.skypack.dev/date-fns';
const dateString = '2h, 2m, 2s';
const date1 = parse(dateString, "HH'h', mm'm', ss's'", new Date())
console.log(date1);
</script>