Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

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

I am piping a time value that looks like this PT43H22M15S for example through some array methods so that as it counts it can come 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?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

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

StackBlitz: https://stackblitz.com/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
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda