Git Product home page Git Product logo

convert-time-string's Introduction

convert-time-string

The JavaScript/TypeScript library that convert time string to seconds or milliseconds.

Installation:

Install convert-time-string using npm:

npm install convert-time-string

Install convert-time-string using yarn:

yarn add convert-time-string

Install convert-time-string using pnpm:

pnpm add convert-time-string

Usage

The convertTimeString function takes a time string as input and returns the corresponding number of milliseconds. The time string can be in the format of a single time unit (e.g. "1h") or multiple time units separated by spaces or without spaces (e.g. "1h 30m" or "1h30m").

// in CommonJS
const { convertTimeString } = require('convert-time-string');

// or in ES module
import { convertTimeString } from 'convert-time-string';

Parameters:

import { TimeUnitOutPut } from 'convert-time-string';

convertTimeString(timeString: string, unitOutPut?: TimeUnitOutPut | string, leapYear?: boolean): number;
  • timeString: The time string to convert.

    const timeString = '1h 30m';
    const timeValue = convertTimeString(timeString);
    console.log(timeValue); // Output: 5400000
  • unitOutPut: The time unit to convert to. Can be "second" or "millisecond". Default is "millisecond". Hint: You can use the TimeUnitOutPut enum to avoid typo.

    const timeString = '1h 30m';
    const timeValue = convertTimeString(timeString, 'millisecond'); // default is "millisecond"
    console.log(timeValue); // Output: 5400000
    
    const timeValue = convertTimeString(timeString, 'second');
    console.log(timeValue); // Output: 5400
  • leapYear: Whether to consider leap year when converting to "y" (years). Default is false.

    const timeString = '1y';
    const timeValue = convertTimeString(timeString, 'second', false); // default is false
    console.log(timeValue); // Output: 31536000000
    
    const timeValue = convertTimeString(timeString, 'second', true);
    console.log(timeValue); // Output: 31622400000

Contribution

convert-time-string's People

Contributors

lehuygiang28 avatar dependabot[bot] avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.