1.1.1 • Published 2 years ago

stime v1.1.1

Weekly downloads
1
License
MIT
Repository
github
Last release
2 years ago

Test

Simple Time (STime)

STime is a simple javascript library used to generate and manipulate date/time. STime is designed to have a simple, readable API that is as pleasing to the eye as it is useful.

Installation

npm install stime

Basic Usage

Using in a script

STime does not require any additional calls or instansiation to use, simply call the STime library after installation:

const stime = require('stime');

Getting the current date/time

The current time returned from any of the generator methods is a Time, more information about how to interact with a time can be found in the Interacting with time section.

const stime = require('stime');
const time = stime.now();

Creating an instance of time in the future or past

A builder is provided to create a date at a particular date/time from the current date/time, this can be accessed in the following way:

const stime = require('stime');
stime.create(7).days().from().now(); // An instance of time 7 days from today

A number of options are available for use with the builder, depending which stage you are at:

StageAvailable optionsExample
Creationdays months years hours minutes secondsstime.create(7).hours()
Unitsfrom beforestime.create(7).days().before()
Directionnow timestime.create(6).hours().after().now()

Getting the difference between two times

A difference method is available in STime. This method takes two times and will report the difference between those two times.

Example

const stime = require('stime');
const timeOne = stime.fromDate(new Date('01-01-2020 15:00:00'));
const timeTwo = stime.fromDate(new Date('01-05-2020 15:00:00'));
stime.difference(timeOne, timeTwo).getTotalDays(); // 4

Creating a date from a native date

To allow for the greatest level of flexibility, STime offers a mechanism to get a time from a native javascript date.

const stime = require('stime');
stime.fromDate(new Date('01-01-2020 14:00:00')); // A time at the specified date

Interacting with time

MethodReturn value
getDayThe current day as an integer
getHoursThe current hour as an integer
getMinutesThe current minute as an integer
getSecondsThe current second as an integer
getYearThe current year as an integer
getMonthThe current month as an integer
toTimestampConvert the time instance to a unix timestamp
formatFormat a time into a particular format

Example

const stime = require('stime');
const time = stime.now();
time.getDay(); // Todays day of the month

Formatting a time

Available Formats

Character(s)ResultExample
YYYYFour-digit year2020
YYTwo-digit year20
MMMMFull month nameJanuary
MMMThree character month nameJan
MMMonth number with leading zeros01
MMonth number without leading zeros1
DDDay number with leading zeros03
DDay number without leading zeros3
HHHour with leading zeros07
HHour without leading zeros7
IIMinutes with leading zeros08
IMinutes without leading zeros8
SSSeconds with leading zeros09
SSeconds without leading zeros9

Example

const stime = require('stime');
stime.now().format('DD/MM/YYYY'); // 01/12/2020

Parsing time from a string

Character(s)ResultExample
YYYYFour-digit year2020
MMMMFull month nameJanuary
MMMThree character month nameJan
MMMonth number with leading zeros01
MMonth number without leading zeros1
DDDay number with leading zeros03
DDay number without leading zeros3
HHHour with leading zeros07
HHour without leading zeros7
IIMinutes with leading zeros08
IMinutes without leading zeros8
SSSeconds with leading zeros09
SSeconds without leading zeros9

Example

const stime = require('stime');
stime.parse('13/01/2000', 'DD/MM/YYYY').getYear(); // 2000
1.1.1

2 years ago

1.0.0

4 years ago

0.3.0

4 years ago

0.4.0

4 years ago

0.2.0

4 years ago

0.0.5

4 years ago

0.0.1

4 years ago