JSPM

parse-input-time

1.0.0
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • 0
  • Score
    100M100P100Q12609F
  • License MIT

Parse input string to hour/minute

Package Exports

  • parse-input-time
  • parse-input-time/dist/human-input-time-parser.js

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (parse-input-time) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

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