0.1.18 • Published 1 year ago

@lapaliv/datetime-formatter v0.1.18

Weekly downloads
37
License
MIT
Repository
github
Last release
1 year ago

DateTimeFormatter

npm licence

Import

// ES6
import DateTimeFormatter from '@lapaliv/datetime-formatter';
// Node.js/ES5
var DateTimeFormatter = require('@lapaliv/datetime-formatter').default;

API

Constructors

// Create an DateTimeFormatter with current datetime
new DateTimeFormatter()
new DateTimeFormatter(date: Date)
new DateTimeFormatter(timestamp: number)
new DateTimeFormatter(year: number, month: number, day: number, hours: number = 0, minutes: number = 0, seconds: number = 0, microseconds: number = 0)

// Create an DateTimeFormatter with current datetime
DateTimeFormatter.now(): DateTimeFormatter;
DateTimeFormatter.createFromFormat(format: string, date: string): DateTimeFormatter;
DateTimeFormatter.createFromDate(date: Date): DateTimeFormatter;
DateTimeFormatter.createFromTimestamp(timestamp: number): DateTimeFormatter;
// Parse date from the custom format
DateTimeFormatter.parse(format: string): DateTimeFormatter;

Getters

getMicroseconds(): number;
getMilliseconds(): number;
getSeconds(): number;
getMinutes(): number;
getHours(): number;
getDay(): number;
getMonth(): number;
getYear(): number;

Setters

setMicroseconds(value: number): DateTimeFormatter;
setMilliseconds(value: number): DateTimeFormatter;
setSeconds(value: number): DateTimeFormatter;
setMinutes(value: number): DateTimeFormatter;
setHours(value: number): DateTimeFormatter;
setDay(value: number): DateTimeFormatter;
setMonth(value: number): DateTimeFormatter;
setYear(value: number): DateTimeFormatter;

Methods for change date manually

addSecond(): DateTimeFormatter;
addSeconds(count: number): DateTimeFormatter;
addMinute(): DateTimeFormatter;
addMinutes(count: number): DateTimeFormatter;
addHour(): DateTimeFormatter;
addHours(count: number): DateTimeFormatter;
addDay(): DateTimeFormatter;
addDays(count: number): DateTimeFormatter;
addWeek(): DateTimeFormatter;
addWeeks(count: number): DateTimeFormatter;
addMonth(): DateTimeFormatter;
addMonths(count: number): DateTimeFormatter;
addYear(): DateTimeFormatter;
addYears(count: number): DateTimeFormatter;
addDecade(): DateTimeFormatter;
addDecades(count: number): DateTimeFormatter;
addCentury(): DateTimeFormatter;
addCenturies(count: number): DateTimeFormatter;

subSecond(): DateTimeFormatter;
subSeconds(count: number): DateTimeFormatter;
subMinute(): DateTimeFormatter;
subMinutes(count: number): DateTimeFormatter;
subHour(): DateTimeFormatter;
subHours(count: number): DateTimeFormatter;
subDay(): DateTimeFormatter;
subDays(count: number): DateTimeFormatter;
subWeek(): DateTimeFormatter;
subWeeks(count: number): DateTimeFormatter;
subMonth(): DateTimeFormatter;
subMonths(count: number): DateTimeFormatter;
subYear(): DateTimeFormatter;
subYears(count: number): DateTimeFormatter;
subDecade(): DateTimeFormatter;
subDecades(count: number): DateTimeFormatter;
subCentury(): DateTimeFormatter;
subCenturies(count: number): DateTimeFormatter;

Methods for change date automatically

startOfMillisecond(): DateTimeFormatter;
startOfSecond(): DateTimeFormatter;
startOfMinute(): DateTimeFormatter;
startOfHour(): DateTimeFormatter;
startOfDay(): DateTimeFormatter;
startOfWeek(): DateTimeFormatter;
startOfMonth(): DateTimeFormatter;
startOfHalfYear(): DateTimeFormatter;
startOfYear(): DateTimeFormatter;
startOfDecade(): DateTimeFormatter;
startOfCentury(): DateTimeFormatter;

endOfMillisecond(): DateTimeFormatter;
endOfSecond(): DateTimeFormatter;
endOfMinute(): DateTimeFormatter;
endOfHour(): DateTimeFormatter;
endOfDay(): DateTimeFormatter;
endOfWeek(): DateTimeFormatter;
endOfMonth(): DateTimeFormatter;
endOfHalfYear(): DateTimeFormatter;
endOfYear(): DateTimeFormatter;
endOfDecade(): DateTimeFormatter;
endOfCentury(): DateTimeFormatter;

Methods for checks

isFuture(): boolean;
isLeapYear(): boolean;
isPast(): boolean;
isNextMonth(): boolean;
isNextYear(): boolean;
isPrevMonth(): boolean;
isPrevYear(): boolean;
isToday(): boolean;
isTomorrow(): boolean;
isYesterday(): boolean;

isJanuary(): boolean;
isFebruary(): boolean;
isMarch(): boolean;
isApril(): boolean;
isMay(): boolean;
isJune(): boolean;
isJuly(): boolean;
isAugust(): boolean;
isSeptember(): boolean;
isOctober(): boolean;
isNovember(): boolean;
isDecember(): boolean;

isCurrentMicrosecond(): boolean;
isCurrentMillisecond(): boolean;
isCurrentSecond(): boolean;
isCurrentMinute(): boolean;
isCurrentHour(): boolean;
isCurrentDay(): boolean;
isCurrentMonth(): boolean;
isCurrentYear(): boolean;
isCurrentDecade(): boolean;
isCurrentCentury(): boolean;

Methods for compare

greaterThan(date: DateTimeFormatter): boolean;
greaterThanOrEqualTo(date: DateTimeFormatter): boolean;
lessThan(date: DateTimeFormatter): boolean;
lessThanOrEqualTo(date: DateTimeFormatter): boolean;

diffInMicroseconds(date: DateTimeFormatter): number;
diffInMilliseconds(date: DateTimeFormatter): number;
diffInSeconds(date: DateTimeFormatter): number;
diffInMinutes(date: DateTimeFormatter): number;
diffInHours(date: DateTimeFormatter): number;
diffInDays(date: DateTimeFormatter): number;
diffInMonths(date: DateTimeFormatter): number;
diffInYears(date: DateTimeFormatter): number;
diffInDecades(date: DateTimeFormatter): number;
diffInCenturies(date: DateTimeFormatter): number;

equal(date: DateTimeFormatter | Date | number | string): boolean;
// Compare year, month, day, hours, minutes, seconds and milliseconds
equalWithoutMicroseconds(date: DateTimeFormatter | Date | number | string): boolean;
// Compare year, month, day, hours, minutes and seconds
equalWithoutMilliseconds(target: DateTimeFormatter | number | Date): boolean;
// Compare year, month, day, hours and minutes
equalWithoutSeconds(date: DateTimeFormatter | Date | number | string): boolean;
// Compare year, month, day and hours
equalWithoutMinutes(date: DateTimeFormatter | Date | number | string): boolean;
// Compare year, month and day
equalWithoutHours(date: DateTimeFormatter | Date | number | string): boolean;
// Compare year and month
equalWithoutDays(date: DateTimeFormatter | Date | number | string): boolean;
// Compare years
equalWithoutMonths(date: DateTimeFormatter | Date | number | string): boolean;

notEqual(date: DateTimeFormatter | Date | number | string): boolean;
notEqualWithoutMicroseconds(date: DateTimeFormatter | Date | number | string): boolean;
notEqualWithoutMilliseconds(date: DateTimeFormatter | Date | number | string): boolean;
notEqualWithoutSeconds(date: DateTimeFormatter | Date | number | string): boolean;
notEqualWithoutMinutes(date: DateTimeFormatter | Date | number | string): boolean;
notEqualWithoutHours(date: DateTimeFormatter | Date | number | string): boolean;
notEqualWithoutDays(date: DateTimeFormatter | Date | number | string): boolean;
notEqualWithoutMonths(date: DateTimeFormatter | Date | number | string): boolean;

Methods for convert

format(format: string): string;
// An alternative for format('Y-m-d')
toDateString(): string;
// An alternative for format('Y-m-d H:i:s')
toDateTimeString(): string;
// Returns a Unix timestamp with milliseconds
toJsTimestamp(): number;
// Returns a Unix timestamp
toTimestamp(): number;
toDate(): Date;
/**
 * Returns the Unix-timestamp with a precision
 * @param precision
 * @example getPreciseTimestamp()   1589833741123456
 * @example getPreciseTimestamp(6)  1589833741123456
 * @example getPreciseTimestamp(5)  158983374112345
 * @example getPreciseTimestamp(4)  158983374112345
 * @example getPreciseTimestamp(3)  15898337411234
 * @example getPreciseTimestamp(2)  1589833741123
 * @example getPreciseTimestamp(1)  158983374112
 * @example getPreciseTimestamp(0)  15898337411
 */
getPreciseTimestamp(precision?: number): number;

Translations

Global

DateTimeFormatter.setGlobalTranslations({
    monthNames: ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'],
    shortMonthNames: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
    dayNames: ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'],
    shortDayNames: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'],
});

Local

const formatter = new DateTimeFormatter();
formatter.setTranslations({
    monthNames: ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'],
    shortMonthNames: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
    dayNames: ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'],
    shortDayNames: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'],
});

Other methods

getFirstDayInYearOnFullWeek(): number;
getCountDaysInMonth(): number;
getDayOfWeek(): number;
getDayOfWeekIso(): number;
getDayOfYear(): number;
clone(): DateTimeFormatter;

// See below
getIndexOfMicrosecondPeriodOnWhichDateIsIncluded(startedFrom: DateTimeFormatter, ...microseconds: Array<number> | Array<Array<number>>): number;
getIndexOfMillisecondPeriodOnWhichDateIsIncluded(startedFrom: DateTimeFormatter, ...milliseconds: Array<number> | Array<Array<number>>): number;
getIndexOfSecondPeriodOnWhichDateIsIncluded(startedFrom: DateTimeFormatter, ...seconds: Array<number> | Array<Array<number>>): number;
getIndexOfMinutePeriodOnWhichDateIsIncluded(startedFrom: DateTimeFormatter, ...minutes: Array<number> | Array<Array<number>>): number;
getIndexOfHourPeriodOnWhichDateIsIncluded(startedFrom: DateTimeFormatter, ...hours: Array<number> | Array<Array<number>>): number;
getIndexOfDayPeriodOnWhichDateIsIncluded(startedFrom: DateTimeFormatter, ...days: Array<number> | Array<Array<number>>): number;
getIndexOfMonthPeriodOnWhichDateIsIncluded(startedFrom: DateTimeFormatter, ...months: Array<number> | Array<Array<number>>): number;
getIndexOfYearPeriodOnWhichDateIsIncluded(startedFrom: DateTimeFormatter, ...years: Array<number> | Array<Array<number>>): number;
getIndexOfDecadePeriodOnWhichDateIsIncluded(startedFrom: DateTimeFormatter, ...decades: Array<number> | Array<Array<number>>): number;
getIndexOfCenturyPeriodOnWhichDateIsIncluded(startedFrom: DateTimeFormatter, ...centuries: Array<number> | Array<Array<number>>): number;

Please see the doc about getIndexOf*PeriodOnWhichDateIsIncluded methods

Format parameters

The following characters are recognized in the format parameter string. The table was copied from php.net

Day

Format characterDescriptionExample returned values
dDay of the month, 2 digits with leading zeros01 to 31
DA textual representation of a day, three lettersMon through Sun
jDay of the month without leading zeros1 to 31
l (lowercase 'L')A full textual representation of the day of the weekSunday through Saturday
NISO 8601 numeric representation of the day of the week1 (for Monday) through 7 (for Sunday)
SEnglish ordinal suffix for the day of the month, 2 charactersst, nd, rd or th. Works well with j
wNumeric representation of the day of the week0 (for Sunday) through 6 (for Saturday)
zThe day of the year (starting from 0)0 through 365

Week

Format characterDescriptionExample returned values
WISO 8601 week number of year, weeks starting on Monday42 (the 42nd week in the year). 00 through 52

Month

Format characterDescriptionExample returned values
FA full textual representation of a month, such as January or MarchJanuary through December
mNumeric representation of a month, with leading zeros01 through 12
MA short textual representation of a month, three lettersJan through Dec
nNumeric representation of a month, without leading zeros1 through 12
tNumber of days in the given month28 through 31

Year

Format characterDescriptionExample returned values
LWhether it's a leap year 1 if it is a leap year, 0 otherwise.
oISO 8601 week-numbering year. This has the same value as Y, except that if the ISO week number (W) belongs to the previous or next year, that year is used instead1999 or 2003
YA full numeric representation of a year, 4 digits1999 or 2003
yA two digit representation of a year99 or 03

Time

Format characterDescriptionExample returned values
aLowercase Ante meridiem and Post meridiemam or pm
AUppercase Ante meridiem and Post meridiemAM or PM
BSwatch Internet time000 through 999
g12-hour format of an hour without leading zeros1 through 12
G24-hour format of an hour without leading zeros0 through 23
h12-hour format of an hour with leading zeros01 through 12
H24-hour format of an hour with leading zeros00 through 23
iMinutes with leading zeros00 to 59
sSeconds with leading zeros00 through 59
uMicroseconds654321
vMilliseconds. Same note applies as for u.654

Full datetime

Format characterDescriptionExample returned values
cISO 8601 date2004-02-12T15:19:21+00:00
rRFC 2822 formatted dateThu, 21 Dec 2000 16:01:07 +0000
USeconds since the Unix Epoch (January 1 1970 00:00:00 GMT)1585517413

Tests

yarn test
0.1.18

1 year ago

0.1.16

3 years ago

0.1.13

3 years ago

0.1.14

3 years ago

0.1.15

3 years ago

0.1.12

4 years ago

0.1.11

4 years ago

0.1.10

4 years ago

0.1.9

4 years ago

0.1.8

4 years ago