Skip to content
This repository has been archived by the owner on May 17, 2022. It is now read-only.

Shane4368/timespan.js

Repository files navigation

TimeSpan.js

Codacy Badge GitHub package.json version GitHub LICENSE Maintenance

Based off C#'s TimeSpan structure.

Installation

Ensure you have git installed, then run npm i shane4368/timespan.js.

Examples

Overview

const { TimeSpan } = require("timespan.js");

const ts = TimeSpan.fromSeconds(3);

console.log(ts);

Numerical Operations

const ts1 = TimeSpan.fromMinutes(1);
const ts2 = TimeSpan.fromSeconds(10);

console.log(ts1 - ts2);	// 50000
console.log(ts1 + ts2);	// 70000

From the above example, subtracting the TimeSpans return the difference in milliseconds. This is possible because the TimeSpan class implements its own valueOf method.

Parsing

const ts1 = TimeSpan.parse("2.04:10:05.006");	// Can also be formatted 00:00:00

const ts2 = TimeSpan.parse("2 days 4 hrs 10 mins 5.006 secs".split(" "));

const ts3 = TimeSpan.parse([2, 4, 10, 5, 6]);	// days, hrs, mins, secs, ms
const ts4 = TimeSpan.parse([2, 4, 10, 5]);	// days, hrs, mins, secs
const ts5 = TimeSpan.parse([2, 4, 10]);		// hrs, mins, secs

Table showing valid units for the second method of parsing

Days Hours Minutes Seconds
days hours minutes seconds
day hour minute second
---- hrs mins secs
---- hr min sec

NOTE: You'd need to provide seconds as a decimal if you wish to specify milliseconds.

About

TimeSpan for JavaScript with web support. Based off .NET's TimeSpan structure.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published