I have duration string that looks like:
1:16.352
where 1
is minutes part, 16
is seconds part and 352
is millisecond part.
I wanted to use Duration.fromISOTime
but I get:
{
"reason": "unparsable",
"explanation": "the input \"1:16.352\" can't be parsed as ISO 8601"
}
Is there a clean way of parsing such duration in Luxon?
I have duration string that looks like:
1:16.352
where 1
is minutes part, 16
is seconds part and 352
is millisecond part.
I wanted to use Duration.fromISOTime
but I get:
{
"reason": "unparsable",
"explanation": "the input \"1:16.352\" can't be parsed as ISO 8601"
}
Is there a clean way of parsing such duration in Luxon?
Share Improve this question edited Feb 18, 2021 at 13:27 pixel asked Feb 18, 2021 at 10:38 pixelpixel 26.5k39 gold badges168 silver badges285 bronze badges 3-
1
@OlegValter afaik
Duration
does not have such method – pixel Commented Feb 18, 2021 at 11:11 -
apologies, I misread the question - how about
fromObject
method? Seems like you can split on a dot and use the values - or do you want to find a method that accepts custom formats directly? – 0Valt Commented Feb 18, 2021 at 11:18 -
2
1:16.354
is neither a valid ISO-8601 time (00:01:16.354
) or duration (PT1M16.352S
) and cannot be parsed as if it is. – phuzi Commented Feb 18, 2021 at 14:14
3 Answers
Reset to default 5Duration.fromISOTime
does not work since 1:16.352
is not an ISO 8601 time string, the hour part is missing (see ISO 8601 Times).
A workaround to build a Luxon Duration
object could be the following:
const DateTime = luxon.DateTime;
const Duration = luxon.Duration;
const startOfHour = DateTime.local().startOf('hour').toMillis();
const dt = DateTime.fromFormat("1:16.352", "m:ss.SSS"). toMillis();
const dur = Duration.fromMillis(dt - startOfHour);
console.log(dur.toFormat("m 'minute' s 'second' S 'millis'"));
<script src="https://cdn.jsdelivr/npm/[email protected]/build/global/luxon.js"></script>
Similarly to @VincenzoC I adjusted my input string
:
const Duration = luxon.Duration;
var output;
const durationInput = "1:16.352"
if (durationInput.match(/:/g) || [].length === 1) {
const semicolonLocation = durationInput.indexOf(":");
if (semicolonLocation === 1) {
output = "00:0" + durationInput;
}
if (semicolonLocation === 2) {
output = "00:" + durationInput;
}
}
console.log(Duration.fromISOTime(output));
<script src="https://cdn.jsdelivr/npm/[email protected]/build/global/luxon.js"></script>
As I mentioned in the ments, you can use the fromObject
static method bined with simply splitting the input into minutes
, seconds
, and milliseconds
configuration options. In your case a trivial regular expression (\d+):(\d+)\.(\d+)
should do the trick, no temporary dates or normalization required.
const { Duration } = luxon;
const durationInput = "1:16.352";
const fromCustom = (input) => {
const [, minutes, seconds, milliseconds ] = input.match(/(\d+):(\d+)\.(\d+)/);
return Duration.fromObject({
minutes, seconds, milliseconds
});
};
console.log(fromCustom(durationInput));
<script src="https://cdn.jsdelivr/npm/[email protected]/build/global/luxon.js"></script>
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744214816a4563502.html
评论列表(0条)