Format seconds

    0

    0

    Giovanny Gongora

    Codiga's TypeScript Recipes

    Returns the ISO format of the given number of seconds.

    const formatSeconds = (s: number) => {
      const [hour, minute, second, sign] =
        s > 0
          ? [s / 3600, (s / 60) % 60, s % 60, '']
          : [-s / 3600, (-s / 60) % 60, -s % 60, '-'];
      return (
        sign +
        [hour, minute, second]
          .map(v => `${Math.floor(v)}`.padStart(2, '0'))
          .join(':')
      );
    };
    
    Codiga Logo
    Codiga Hub
    • Rulesets
    • Playground
    • Snippets
    • Cookbooks
    Legal
    • Security
    • Privacy Policy
    • Code Privacy
    • Terms of Service
    soc-2 icon

    We are SOC-2 Compliance Certified

    G2 high performer medal

    Codiga – All rights reserved 2022.