New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

parse-input-time

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

parse-input-time

Parse input string to hour/minute

latest
Source
npmnpm
Version
1.0.1
Version published
Maintainers
1
Created
Source

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

Keywords

parse

FAQs

Package last updated on 23 Mar 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts