format-hours
v1.3.3
Published
Simple library for hours formatting
Downloads
548
Readme
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.