0.0.10 • Published 2 years ago

@han41858/date-ex v0.0.10

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

npm

date-ex

한국어

Extensions for Date class.

This library is extensions for JavaScript Date class. If you use this library, you can modify Date class more easily and represent Date in any format.

date-ex is written by TypeScript, and published by JavaScript. So you can use this library in TypeScript code and JavaScript code also.

DateTime

Represent date and time.

DataTime instance is created with new keyword.

const date: DateTime = new DateTime();

If you pass parameter to constructor, you can create instance with specific date. Parameter type supports for undefined, null, number , string, Date, DateTime, json type(DateTimeParam).

const newDateByNumber : DateTime = new DateTime(1603722868252);

const newDateByString : DateTime = new DateTime('2020-10-26');

const newDateByDate : DateTime = new DateTime(new Date());

const newDateByDateTime : DateTime = new DateTime(new DateTime());

const newDateByDateTimeParam : DateTime = new DateTime({
	year : 2020,
	month : 10,
	date : 26
});

Getter - Basic fields

GetterReturn typeValue RangeDescriptionDate methods
yearnumber-Returns year.Date.getFullYear()
monthnumber1 ~ 12Returns month.Date.getMonth()
datenumber1 ~ 31Returns date.Date.getDate()
daynumber0 ~ 6Returns number of day.Date.getDay()
hoursnumber0 ~ 23Returns hours.Date.getHours()
minutesnumber0 ~ 59Returns minutes.Date.getMinutes()
secondsnumber0 ~ 59Returns seconds.Date.getSeconds()
msnumber0 ~ 999Returns milliseconds.Date.getMilliseconds()
timezoneOffsettimezone-Returns timezone offset in minutes unit.Date.getTimezoneOffset()
  • Compared to the Date class, get- prefix has been omitted for better convenience, uses the getter method not the function execution form. For the same reason, getMilliseconds() methods is provided as ms.

  • month field range is not 0 ~ 11. This field represent the real month value 1 ~ 12

Getter - Extended fields

GetterReturn typeValue RangeDescription
quarternumber1 ~ 4Returns quarter of the date.
weekOfYearnumber1 ~ 53Returns the number of week of the date in the year.
weekOfMonthnumber1 ~ 5Returns the number of week of the date in the month.
weeksOfYearnumber52 ~ 53Returns the number of week in year.
weeksOfMonthnumber4 ~ 6Returns the number of week in month.
dayOfYearnumber1 ~ 365Returns the number of day in the year.
daysOfYearnumber1 ~ 366Returns the number of date in the year.
lastDatenumber28 ~ 31Returns the last date of the month.
timezoneOffsetInHoursnumber-12 ~ 14Returns the timezone offset in hours unit.
isAmbooleantrue/falseReturns true for the morning and false for the afternoon.
hours24number0 ~ 23Returns hours in 24 format. Same with hours.
hours12number0 ~ 12Returns hours in 12 format.

UTC fields

Returns date and time in UTC(Coordinated Universal Time). The year, month, date and time may change depending on the timezone. The minutes, seconds and milliseconds is not affected.

The timezoneOffset value in UTC is same with timezoneOffset value in DateTime instance.

const date: DateTime = new DateTime();
const utcDate: DateTime = date.UTC;

With another type

GetterReturn TypeDescription
valueOf()numberReturns with Unix timestamp. Same with +new Date() and +new DateTime()
toDate()DateReturns Date type.
toISOString()stringReturns with ISO string. Same with Date.toISOString().
toUTCString()stringReturns with UTC string. Same with Date.toUTCString().
toJson()objectReturns with DateTimeParam.

Setter - Each fields

SetterValue TypeDescriptionDate Methods
yearnumberSet year.Date.setFullYear()
monthnumberSet month.Date.setMonth()
datenumberSet date.Date.setDate()
hoursnumberSet hours.Date.setHours()
minutesnumberSet minutes.Date.setMinutes()
secondsnumberSet seconds.Date.setSeconds()
msnumberSet milliseconds.Date.setMilliseconds()
  • Compared to the Date class, set- prefix has been omitted for better convenience, uses the setter method not the function execution form. For the same reason, getMilliseconds() methods is provided as ms.

  • If you don't change the year, month setter value range is not 0 ~ 11, but 1 ~ 12.

set(): DateTime

Set date and time with DateTimeParam. Each field can be omitted.

const date : DateTime = new DateTime();

// set with 27-Oct-2020
date.set({
	year : 2020,
	month : 10,
	date : 27
});
  • If you don't change the year, month setter value range is not 0 ~ 11, but 1 ~ 12.

add(param: DateTimeParam | Duration | DurationParam): DateTime

DateTime type is not supported.

with DateTimeParam

Set to a specific date and time. If a value less than 0 is used, the previous date and time are set.

const date : DateTime = new DateTime();

// set to after 11 months.
date.add({
	year : 1,
	month : -1
});

with Duration

Set to the date and time moved by the duration.

const date : DateTime = new DateTime();
const duration : Duration = new Duration({
	months : 11
});

// set to after 11 months.
date.add(duration);

with DurationParam

Set to the date and time moved by the duration. If a value less than 0 is used, the previous date and time are set.

const date : DateTime = new DateTime();

// set to after 11 months.
date.add({
	months : 11
});

startOf(unit): DateTime, endOf(unit): DateTime

Returns the start/end date and time based on the unit passed as a factor.

unit: year, quarter, month, week, date, hours, minutes, seconds, ms

const date: DateTime = new DateTime();

console.log(date.startOf('year').toISOString()); // 2020-01-01T00:00:00.000Z
console.log(date.endOf('year').toISOString()); // 2020-12-31T23:59:59.999Z

format(): string

Returns string with the format.

TokenToken stringGetterDescriptionValue range
FormatToken.YearShortYY-Converts to a 2-digit year.00 ~ 20, ...
FormatToken.YearYYYYyearConverts to a 4-digit year.1970 ~ 2020, ...
FormatToken.QuarterQquarterConverts to a quarter.1 ~ 4
FormatToken.MonthMmonthConverts to a month.1 ~ 12
FormatToken.MonthPaddedMM-Converts to a month with 2-digit.01 ~ 12
FormatToken.MonthStringShortMMM-Converts to a short name month.Jan ~ Dec
FormatToken.MonthStringLongMMMM-Converts to a long name month.January ~ December
FormatToken.WeekWweekOfYearConverts to a number of the week in year.1 ~ 53
FormatToken.WeekPaddedWW-Converts to a number of the week in year in 2-digit.01 ~ 53
FormatToken.WeekPaddedWithPrefixWww-Converts to a number of the week in year in 2-digit with prefix W.W01 ~ W53
FormatToken.DayOfYearDDDdayOfYearConverts to a numbef of the day in year.1 ~ 365
FormatToken.DayOfYearPaddedDDDD-Converts to a number of the day in year in 3-digit.001 ~ 365
FormatToken.DayOfMonthDdayOfMonthConverts to a number of the day in month.1 ~ 31
FormatToken.DayOfMonthPaddedDD-Converts to a number of the day in month in 2-digit.01 ~ 31
FormatToken.DayOfWeekddayConverts to a number of day.0 ~ 6
FormatToken.DayOfWeekStringShortdd-Converts to a short name day.Su ~ Sa
FormatToken.DayOfWeekStringMiddleddd-Converts to a middle name day.Sun ~ Sat
FormatToken.DayOfWeekStringLongdddd-Converts to a long name day.Sunday ~ Saturday
FormatToken.MeridiemLowera-Converts to am/pm in lower case.am, pm
FormatToken.MeridiemCapitalA-Converts to am/pm in upper case.AM, PM
FormatToken.Hours24Hhours, hours24Converts to a hours in 24 format.0 ~ 23
FormatToken.Hours24PaddedHH-Converts to a hours in 24 format in 2-digit.00 ~ 23
FormatToken.Hours12hhours12Converts to a hours in 12 format.0 ~ 12
FormatToken.Hours12Paddedhh-Converts to a hours in 12 format in 2-digit.00 ~ 12
FormatToken.MinutesmminutesConverts to a minutes.0 ~ 59
FormatToken.MinutesPaddedmm-Converts to a minutes in 2-digit.00 ~ 59
FormatToken.SecondsssecondsConverts to a seconds.0 ~ 59
FormatToken.SecondsPaddedss-Converts to a seconds in 2-digit.00 ~ 59
FormatToken.MilliSecondsSmsConverts to a milliseconds.0 ~ 999
FormatToken.MilliSecondsPadded2SS-Converts to a milliseconds in 2-digit.00 ~ 99
FormatToken.MilliSecondsPadded3SSS-Converts to a milliseconds in 3-digit.000 ~ 999

Internationalization

You can set i18n in global context or each instance. If the setting in global and local is different, i18n is working with local setting.

If you set i18n value globally, new instances are set with that value.

console.log(DateTime.locale()); // 'en'

const date1: DateTime = new DateTime();
console.log(date1.locale()); // 'en'

DateTime.locale('ko-kr'); // set globally
console.log(DateTime.locale()); // 'ko-kr'

const date2: DateTime = new DateTime();
console.log(date2.locale()); // 'ko-kr'

const date3: DateTime = new DateTime();
date3.locale('en'); // set i18n locally
console.log(date3.locale()); // 'en'

Global DateTime.locale() and locale() methods in each instance without parameter returns current i18n value. If you want to set i18n value, pass language code to locale() methods.

Default value is en..

Warning

locale() methods uses ES6 import function. import function is working as Promise, so you should wait JavaScript 1 cycle after called locale() to load i18n file.

If the language code is invalid or the language file load fails after 1 cycle, the previous value will be restored.

DateTime.locale(): string

Set language in globally.

locale(): string

Set language in locally.

Language supports

Language codeName
enEnglish
ko-krKorean (Korea)

Compare methods

All compare methods can be received DateTimeUnit. The accuracy of the calculation is based on this unit, and if the unit is omitted, it is calculated in milliseconds.

MethodsReturn typeDescription
diff()numberReturns the difference value.
isEqual()booleanReturns true if the date is same with the parameter.
isBefore()booleanReturns true if the date is before than parameter.
isBeforeOrEqual()booleanReturns true if the date is before than or equal with parameter.
isAfter()booleanReturns true if the date is after than parameter.
isAfterOrEqual()booleanReturns true if the date is after than or equal with parameter.
isBetween()booleanReturns true if the date is between with 2 parameters.
isAfterOrEqual()booleanReturns true if the date is between with 2 parameters or same with one parameter.
const date1 : DateTime = new DateTime({
	year : 2020,
	month : 10,
	date : 20
});

const date2 : DateTime = new DateTime({
	year : 2020,
	month : 10,
	date : 27
});

console.log(date1.diff(date2, 'date')); // -7

console.log(date1.isEqual(date2, 'month')); // true

console.log(date1.isBefore(date2, 'date')); // true
console.log(date1.isBeforeOrEqual(date2, 'month')); // true

console.log(date1.isAfter(date2, 'date')); // false
console.log(date1.isBeforeOrEqual(date2, 'month')); // true

const date3 : DateTime = new DateTime({
  year : 2020,
  month : 10,
  date : 27
});

console.log(date2.isBetween(date1, date3, 'date')); // true
console.log(date2.isBetweenOrEqual(date1, date2, 'date')); // true

DateTimeParam, DateTimeParamEx

Represent date, time value. This object consists with this fields.

FieldField TypeValue RangeDescription
yearnumber-Represents year.
quarternumber1 ~ 4Represents quarter. (only for DateTimeParamEx)
monthnumber1 ~ 12Represents month.
weeknumber1 ~ 42Represents week number. (only for DateTimeParamEx)
datenumber1 ~ 31Represents date.
hoursnumber0 ~ 23Represents hours.
minutesnumber0 ~ 59Represents minutes.
secondsnumber0 ~ 59Represents seconds.
msnumber0 ~ 999Represents mlliseconds.
  • month field range is not 0 ~ 11. This field represent the real month value 1 ~ 12
  • quarter, week are fields of DateTimeParamEx.

DateTimeUnit

Represents date and time unit.

TokenFieldTypeDescription
DateTimeUnit.YearyearnumberRepresents year.
DateTimeUnit.QuarterquarternumberRepresents quarter.
DateTimeUnit.MonthmonthnumberRepresents month.
DateTimeUnit.WeekweeknumberRepresents week number.
DateTimeUnit.DatedatenumberRepresents date.
DateTimeUnit.HourshoursnumberRepresents hours.
DateTimeUnit.MinutesminutesnumberRepresents minutes.
DateTimeUnit.SecondssecondsnumberRepresents seconds.
DateTimeUnit.MsmsnumberRepresents milliseconds.

Calendar

DateTime.getYearCalendar(): YearCalendar

Returns the year calendar for that year value of the instance. Time fields are set to 0.

Length of dates array field is same with the number of date in that year.

DateTime.getMonthCalendar(): MonthCalendar

Returns the month calendar for that month value of the instance. Time fields are set to 0.

Length of dates array field is same with the number of date in that month.

Duration

Represents duration.

Duration instance is created with new keyword.

const duration: Duration = new Duration();

If you pass parameter to constructor, you can create instance with specific duration. Parameter type supports for undefined, null, number, string, Duration, json type(DurationParam).

const newDurationByString : Duration = new Duration('PY2');

const newDurationByDuration : Duration = new Duration(new Duration());

const newDurationByDurationParam : Duration = new Duration({
  years : 2
});

Getter & Setter - Each fields

TokenFieldType
DurationUnit.Yearsyearsnumber
DurationUnit.Monthsmonthsnumber
DurationUnit.Datesdatesnumber
DurationUnit.Hourshoursnumber
DurationUnit.Minutesminutesnumber
DurationUnit.Secondssecondsnumber
DurationUnit.Msmsnumber
const duration: Duration = new Duration(); // value : {}

duration.years = 2; // value : { years : 2 }
duration.ms = 1001; // value : { years : 2, seconds : 1, ms : 1} - with rebalancing

console.log(duration.seconds); // 1

add()

add (param : Duration | DurationParam) : Duration

If param is Duration or DurationParam, returns Duration.

add (param : DateTime | DateTimeParam) : DateTime

If param is DateTime or DateTimeParam, returns DateTime.

divide(count : number) : Duration[]

Divide total duration by count as Duration array.

const duration: Duration = { seconds: 1 };

console.log(duration.divide(4)); // 4 Duration instances with value of { ms : 250 }

DurationParam

Represent duration value. This object consists with this fields.

FieldField TypeValue RangeDescription
yearsnumber-Represents years.
monthsnumber1 ~ 12Represents months.
datesnumber1 ~ 31Represents dates.
hoursnumber0 ~ 23Represents hours.
minutesnumber0 ~ 59Represents minutes.
secondsnumber0 ~ 59Represents seconds.
msnumber0 ~ 999Represents milliseconds.

DurationUnit

Represents duration unit.

TokenFieldTypeDescription
DurationUnit.YearsyearsnumberRepresents years.
DurationUnit.QuartersquartersnumberRepresents quarters.
DurationUnit.MonthsmonthsnumberRepresents months.
DurationUnit.WeeksweeksnumberRepresents weeks.
DurationUnit.DatesdatesnumberRepresents dates.
DurationUnit.HourshoursnumberRepresents hours.
DurationUnit.MinutesminutesnumberRepresents minutes.
DurationUnit.SecondssecondsnumberRepresents seconds.
DurationUnit.MsmsnumberRepresents milliseconds.
0.0.10

2 years ago

0.0.9

2 years ago

0.0.8

2 years ago

0.0.7

2 years ago

0.0.5

3 years ago

0.0.6

3 years ago

0.0.4

3 years ago

0.0.3

3 years ago

0.0.2-re

3 years ago

0.0.2

3 years ago

0.0.1

3 years ago