0.1.1 • Published 8 years ago

@wazio/date-time v0.1.1

Weekly downloads
-
License
MIT
Repository
github
Last release
8 years ago

Build Status Coverage Status styled with prettier

DateTime

A small JavaScript library written in TypeScript with some useful methods that I've always missed in JavaScript's Date object. Written without Regex. Based on C# DateTime class.

Installation

npm install @wazio/date-time

Test

npm test

Usage

import { DateTime, TimeSpan } from '@wazio/date-time';

// format
DateTime.format(new Date(2017, 5, 12, 16, 30), 'yyyy-MM-dd hh:mm tt'); // will output '2017-06-12 04:30 PM'

// parseExact
DateTime.parseExact('2017-06-12 04:30 PM', 'yyyy-MM-dd hh:mm tt'); // will be equal to new Date(2017, 5, 12, 16, 30)

// subtract
let timeSpan: TimeSpan = DateTime.subtract(new Date(2017, 5, 12, 16, 30), new Date(2017, 5, 11, 13, 30));
timeSpan.days; // will output 1
timeSpan.hours; // will output 3
timeSpan.totalDays; // will output 1.125
timeSpan.totalHours; // will output 27

// subtractDate
let timeSpan: TimeSpan = DateTime.subtractDate(new Date(2017, 5, 12, 16, 30), new Date(2017, 5, 11, 13, 30));
timeSpan.days; // will output 1
timeSpan.hours; // will output 0
timeSpan.totalDays; // will output 1
timeSpan.totalHours; // will output 24

// day checking with subtractDate
let timeSpan: TimeSpan = DateTime.subtractDate(new Date(2017, 5, 12, 16, 30), new Date(2017, 5, 13, 13, 30)); // second parameter is current date by default
let isYesterday: boolean = timeSpan.days === -1; // will output true
let isToday: boolean = timeSpan.days === 0; // will output false
let isTomorrow: boolean = timeSpan.days === 1; // will output false

Classes

DateTime

Methods
NameDescription
static format(date: Date, format: string): stringConverts the value of the given Date object to its equivalent string representation using the specified format.
static parseExact(datetime: string, format: string): DateConverts the specified string representation of a date and time to its Date equivalent using the specified format.
static subtract(date1: Date, date2: Date = new Date()): TimeSpanReturns a new TimeSpan object based on date and time subtraction between given Date objects.
static subtractDate(date1: Date, date2: Date = new Date()): TimeSpanReturns a new TimeSpan object based on date subtraction between given Date objects.

TimeSpan

Constructor
constructor(milliseconds: number);
constructor(hours: number, minutes: number, seconds: number);
constructor(days: number, hours: number, minutes: number, seconds: number, milliseconds?: number);
Methods
NameDescription
get days(): numberReturns the days component of the time interval represented by the current TimeSpan structure.
get hours(): numberReturns the hours component of the time interval represented by the current TimeSpan structure.
get minutes(): numberReturns the minutes component of the time interval represented by the current TimeSpan structure.
get seconds(): numberReturns the seconds component of the time interval represented by the current TimeSpan structure.
get milliseconds(): numberReturns the milliseconds component of the time interval represented by the current TimeSpan structure.
get totalDays(): numberReturns the milliseconds value of the current TimeSpan structure expressed in whole and fractional days.
get totalHours(): numberReturns the milliseconds value of the current TimeSpan structure expressed in whole and fractional hours.
get totalMinutes(): numberReturns the milliseconds value of the current TimeSpan structure expressed in whole and fractional minutes.
get totalSeconds(): numberReturns the milliseconds value of the current TimeSpan structure expressed in whole and fractional seconds.
get totalMilliseconds(): numberReturns the milliseconds value of the current TimeSpan structure expressed in whole and fractional milliseconds.

Format Specifiers

Format specifierDescriptionExampleCompatible with parseExact

| Year | y | The year, from 0 to 99. | 7 | yes | | yy | The year, from 00 to 99. | 17 | yes | | yyy | The year, with a minimum of three digits. | 017 | yes | | yyyy | The year as a four-digit number. | 2017 | yes | | yyyyy | The year as a five-digit number. | 02017 | yes | || | Month | M | The month, from 1 through 12. | 6 | - | | MM | The month, from 01 through 12. | 06 | yes | || | Day | d | The day of the month, from 1 through 31. | 8 | - | | dd | The day of the month, from 01 through 31. | 08 | yes | || | Hour | h | The hour, using a 12-hour clock from 1 to 12. | 6 | - | | hh | The hour, using a 12-hour clock from 01 to 12. | 06 | yes | | H | The hour, using a 24-hour clock from 0 to 23. | 6 | - | | HH | The hour, using a 24-hour clock from 00 to 23. | 06 | yes | | t | The first character of the AM/PM designator. | P | yes | | tt | The AM/PM designator. | PM | yes | || | Minute | m | The minute, from 0 through 59. | 4 | - | | mm | The minute, from 00 through 59. | 04 | yes | || | Second | s | The second, from 0 through 59. | 9 | - | | ss | The second, from 00 through 59. | 09 | yes | || | Millisecond | f | The tenths of a second in a date and time value. | 8 | yes | | ff | The hundredths of a second in a date and time value. | 81 | yes | | fff | The milliseconds in a date and time value. | 813 | yes |