javascript - Using Luxon, what is the best way to convert a duration into a countdown with the following format T-HH:MM:SS? - St

I am piping a time value that looks like this PT43H22M15S for example through some array methods so tha

I am piping a time value that looks like this PT43H22M15S for example through some array methods so that as it counts it can e out the other side looking like this 43:22:15. The way I do this is like this ...

    return this.millisRemaining$.pipe(
      map((val) => {
        console.log(val);
        const duration = Duration.fromMillis(val);
        console.log(duration);
        const result = duration
          .shiftTo('hours', 'minutes', 'seconds')
          .toString();
        console.log(result);
        return result;
      }),
      map((val) => {
        console.log(`from tap: ${val}`);
        const slicedVal = val.slice(2);
        console.log(slicedVal);
        const replacedVal = slicedVal
          .replace('H', ':')
          .replace('M', ':')
          .replace('S', '');
        console.log(replacedVal);
        return replacedVal;
      })
    );
  }

however, I have an issue. Lets say I want to start with 43 Hours. The output will be 43: and if the preceding number is a 0 it will not show up. like :11 ... :10 ... :9 ...etc How do I get it to display 43:00:00 and :09 ... :08 etc?

I am piping a time value that looks like this PT43H22M15S for example through some array methods so that as it counts it can e out the other side looking like this 43:22:15. The way I do this is like this ...

    return this.millisRemaining$.pipe(
      map((val) => {
        console.log(val);
        const duration = Duration.fromMillis(val);
        console.log(duration);
        const result = duration
          .shiftTo('hours', 'minutes', 'seconds')
          .toString();
        console.log(result);
        return result;
      }),
      map((val) => {
        console.log(`from tap: ${val}`);
        const slicedVal = val.slice(2);
        console.log(slicedVal);
        const replacedVal = slicedVal
          .replace('H', ':')
          .replace('M', ':')
          .replace('S', '');
        console.log(replacedVal);
        return replacedVal;
      })
    );
  }

however, I have an issue. Lets say I want to start with 43 Hours. The output will be 43: and if the preceding number is a 0 it will not show up. like :11 ... :10 ... :9 ...etc How do I get it to display 43:00:00 and :09 ... :08 etc?

Share Improve this question asked Feb 24, 2022 at 21:17 pachonga9pachonga9 411 gold badge1 silver badge5 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 5

Use the toFormat function: https://moment.github.io/luxon/api-docs/index.html#durationtoformat

StackBlitz: https://stackblitz./edit/node-v364nf?file=index.js

In NodeJS using Luxon version 2.3.1:

import { Duration } from 'luxon';

const duration1 = Duration.fromObject({ hour: 43 });
const output1 = duration1.toFormat('T-hh:mm:ss');
console.log(output1); //T-43:00:00

const duration2 = duration1.minus({ hours: 3, minutes: 33, seconds: 33 });
const output2 = duration2.toFormat('T-hh:mm:ss');
console.log(output2); //T-39:26:27

const isoDuration = Duration.fromISO('PT43H22M15S');
const output3 = isoDuration.toFormat('T-hh:mm:ss');
console.log(output3); //T-43:22:15

Code Result:

❯ npm run start
$ node index.js
T-43:00:00
T-39:26:27
T-43:22:15

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744249417a4565093.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信