1.0.1 • Published 1 month ago

parse-input-time v1.0.1

Weekly downloads
-
License
MIT
Repository
bitbucket
Last release
1 month ago

parse-input-time

A lightweight, zero-dependency JavaScript library for parsing human-readable time strings into a structured time format. Ideal for applications requiring user-friendly time input fields that need to be parsed into a format that can be easily manipulated or compared.

Features

  • Parses various human-readable time formats (e.g., "1a", "10:30p", "1300").
  • Handles AM/PM inputs and converts them into 24-hour time format.
  • Validates input to ensure it represents a valid time.
  • Automatically trims whitespace from input strings.
  • Returns undefined for invalid time inputs, allowing for easy error handling.

Installation

Use npm to install parse-input-time:

npm install parse-input-time

Usage

Import parseInputTime from the package and pass a time string to it. The function returns an object with hour and minute properties if the input is valid, or undefined if the input is invalid.

import { parseInputTime } from 'parse-input-time';

const time = parseInputTime('10:30p');
console.log(time); // { hour: 22, minute: 30 }

Examples

// Valid time inputs
parseInputTime('1a'); // { hour: 1, minute: 0 }
parseInputTime('10:30p'); // { hour: 22, minute: 30 }
parseInputTime('1300'); // { hour: 13, minute: 0 }

// Invalid time inputs

parseInputTime('25:00'); // undefined
parseInputTime('abc'); // undefined
parseInputTime(''); // undefined
1.0.1

1 month ago

1.0.0

1 month ago