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

format-hours

v1.3.3

Published

Simple library for hours formatting

Downloads

548

Readme

Version npm License: MIT Minsize Node.js CI codecov Coverage

Installation

You can install format-hours using standard tools:

$> npm install format-hours
# or
$> yarn add format-hours

Usage

import formatTime from 'format-hours';

formatTime(9); // 9:00
formatTime(15); // 15:00

// Also works with float
formatTime(16.5); // 16:30
formatTime(6.25); // 6:15
formatTime(22.75); // 22:45

// Read about `options` below

// `timeFormat`
formatTime(22, { timeFormat: '12h' }); // 8:00
formatTime(13.5, { timeFormat: '12h' }); // 1:30

// AM/PM also available
formatTime(13.5, { timeFormat: 'AM-PM' }); // 1:30 PM
formatTime(0, { timeFormat: 'AM-PM' }); // 12:00 AM
formatTime(4, { timeFormat: 'AM-PM' }); // 4:00 AM
formatTime(12, { timeFormat: 'AM-PM' }); // 12:00 PM

// and customaziable
formatTime(12, { timeFormat: 'AM-PM', suffixes: [' a.m', ' p.m'] }); // 12:00 p.m.
formatTime(18.5, { timeFormat: 'AM-PM', suffixes: [' a.m', ' p.m'] }); // 6:30 p.m.

// `fullSize`
formatTime(7, { fullSize: true }); // 09:00
formatTime(1, { fullSize: true }); // 01:00

// `divider`
formatTime(23.5, { divider: '.' }); // 23.30
formatTime(11.2, { divider: '|' }); // 11|12

// 'removeOverflow'
formatTime(31.5, { removeOverflow: true }); // 7:30
formatTime(24, { removeOverflow: true }); // 00:00

// of course, you can combine them as you wish

// Or even use it with Dates
formatTime(new Date('March 13, 08 14:20')); // 14:20
formatTime(new Date('March 13, 08 14:20'), { divider: '.' }); // 14.20

API

formatTime

Interface:

function formatTime(input: Date | number, options?: Options): string {
    // ...
}

Options

|Option| Type | Default | Description | |--|--|--|--| | timeFormat | '24h'/'12h'/'AM-PM'' | '24h' | The way of formatting hours | | divider | string | : | Divider between hours and minutes | | fullSize | boolean | false | Appends leading zero if hour less than 10 | | removeOverflow | boolean | false | Trims extra-hours if input value more than 24 | | suffixes | [string, string] | [' AM', ' PM'] | Suffixes for 'AM-PM'' time format. ['<suffix if before noon>', '<siffux if after noon'] |

parseTime

Kind of reverse parsing for hours: Interface:

function parseTime(inputStr: string, timeFormat?: '24h' | '12h' | 'AM-PM'): string {
    // ...
}

Options

|Option| Type | Default | Description | |--|--|--|--| | timeFormat | '24h'/'12h'/'AM-PM'' | '24h' | The way of parsing hours. Try to avoid using '12h', it may cause wrong parsing of 12:00. Use 'AM-PM' instead.|

Example

import { parseTime } from 'format-hours';

parseTime('13:00') // 13
parseTime('23:30') // 23.5
parseTime('12:15') // 12.25

// Works with AM-PM time
parseTime('12:00 a.m.', 'AM-PM') // 12
parseTime('12:00 p.m.', 'AM-PM') // 0

// in any notation
parseTime('1:00 PM', 'AM-PM') // 13
parseTime('01:30 PM', 'AM-PM') // 13.5

// with any dividers
parseTime('17-00') // 17
parseTime('14/45') // 14.75
parseTime('09|30') // 9.30

Contributing

Please, use PRs for your proposals.

Setting up local development

For start, just clone repo and install dependencies via npm/yarn:

$> git clone https://github.com/opa-oz/format-hours.git
$> yarn install
# or
$> npm install

Tests

To run test, simply use command:

$> yarn test
#or
$> npm run test

Lint & Prettier

Easy to check code style and formatting:

$> yarn lint && yarn prettier-format

License

format-hours is copyright © 2020 opa_oz. It is free software and may be redistributed under the terms specified in the license.


ko-fi