npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@ciaksoy/hhmmss

v1.0.0

Published

Convert seconds to HH:MM:SS format

Downloads

8

Readme

This function converts a given number of seconds into a formatted time string. It supports various time formats, including hours, minutes, and seconds, and adjusts the output based on the provided format string.

Function Signature

export default function(fromSeconds: number, format: string): string;

Parameters

  • fromSeconds (number): The time in seconds that you want to convert.
  • format (string): The format string that specifies how the time should be formatted. The format can include placeholders for hours (HH), minutes (MM), and seconds (SS).

Description

The formatTime function takes an input time in seconds and formats it according to the specified format string. The function supports the following placeholders in the format string:

  • HH: Hours
  • MM: Minutes
  • SS: Seconds

Example Usage

import formatTime from './formatTime';
const time1 = formatTime(3661, 'HH:MM:SS'); // "01:01:01" 
const time2 = formatTime(61, 'MM:SS'); // "01:01" 
const time3 = formatTime(59, 'SS'); // "59" 
const time4 = formatTime(3661, 'MM:SS'); // "61:01" 
const time5 = formatTime(3661, 'SS'); // "3661" 
const time6 = formatTime(3661, '(HH:)MM:SS'); // "01:01:01" 
const time7 = formatTime(361, '(HH:)MM:SS'); // "06:01" 
const time8 = formatTime(61, '(MM:)SS'); // "01:01" 
const time9 = formatTime(1, '(MM:)SS'); // "01" 
const time10 = formatTime(3661, '(HH:)(MM:)SS'); // "01:01:01" 
const time11 = formatTime(61, '(HH:)(MM:)SS'); // "01:01" 
const time12 = formatTime(1, '(HH:)(MM:)SS'); // "01" 
const time13 = formatTime(60, 'MM:SS'); // "01:00" 
const time14 = formatTime(3600, 'HH:MM:SS'); // "01:00:00" 
const time15 = formatTime(0, 'HH:MM:SS'); // "00:00:00" 
const time16 = formatTime(0, '(HH:)(MM:)SS'); // "00"

Features

  • Dynamic Adjustment: If the format string does not include hours (HH), the hours are added to the minutes. Similarly, if the format string does not include minutes (MM), the minutes are added to the seconds.
  • Flexible Formatting: The format string can include optional placeholders within parentheses. If the value for a placeholder is zero, the placeholder and its parentheses are removed from the output.

Implementation Details

  1. Calculation: The function first calculates the hours, minutes, and seconds from the input fromSeconds.
  2. Adjustment: The function adjusts the minutes and seconds based on the absence of hours and minutes in the format string.
  3. Replacement: The function creates a replacements object with zero-padded string representations of hours, minutes, and seconds.
  4. Formatting: The function processes the format string to handle optional placeholders within parentheses and replaces the placeholders with the corresponding values from the replacements object.

Edge Cases

  • If the input seconds are less than 60, and the format string includes hours or minutes, the output will properly display 00 for those components.
  • If the input seconds are exactly 0, the output will be 00:00:00 or equivalent based on the format string.

Contributing

Contributions are welcome! Please open an issue or submit a pull request with your improvements or bug fixes.