I'm trying to convert duration in seconds using Luxon using the Duration.fromMillis().toFormat()
.
I want it to output 1d2h
for one day and two hours, but I can't escape the d
and h
.
Here's what I tried so far
Duration.fromMillis(3600 * 26 * 1000).toFormat('d\dh\h')
Duration.fromMillis(3600 * 26 * 1000).toFormat('d[d]h[h]') // Like MomentJS
Both doesn't work
I'm trying to convert duration in seconds using Luxon using the Duration.fromMillis().toFormat()
.
I want it to output 1d2h
for one day and two hours, but I can't escape the d
and h
.
Here's what I tried so far
Duration.fromMillis(3600 * 26 * 1000).toFormat('d\dh\h')
Duration.fromMillis(3600 * 26 * 1000).toFormat('d[d]h[h]') // Like MomentJS
Both doesn't work
Share Improve this question asked May 5, 2022 at 14:23 starleaf1starleaf1 2,8826 gold badges41 silver badges73 bronze badges 2- 1 Formatting guide : escape characters with single quotes. – k.tten Commented May 5, 2022 at 14:28
-
1
Use
.toFormat("d'd'h'h'")
– Timur Commented May 5, 2022 at 18:09
1 Answer
Reset to default 9As already stated by others in the ment, you can use single quotes to escape charcters in luxon, see Escaping secction of the docs:
You may escape strings using single quotes:
DateTime.now().toFormat("HH 'hours and' mm 'minutes'"); //=> '20 hours and 55 minutes'
Here a working example:
const Duration = luxon.Duration;
console.log(Duration.fromMillis(3600 * 26 * 1000).toFormat("d'd'h'h'"));
<script src="https://cdn.jsdelivr/npm/[email protected]/build/global/luxon.min.js"></script>