datetime-utility
Simple tools for date manipulation in Javascript or TypeScript
Install
npm install datetime-utility --save
PERIODS
- MILLISECOND
- SECOND
- MINUTE
- HOUR
- DAY
- WEEK
- MONTH
- TWO_MONTHS
- QUARTER
- SEMESTER
- YEAR
isISODate(str : String)
check if a string is an ISO-compliant date
parameters
- str: string to check
examples
console; // trueconsole; // falseconsole; // trueconsole; // falseconsole; // trueconsole; // falseconsole; // false
toDate(str : String, pattern : String, strict: boolean)
Returns a date based on a string with a given pattern
parameters
-
str: String to convert to date
-
pattern: String containing date mask (default value 'yyyy/MM/dd hh:mm:ss.S')
-
strict: if true, if the amount of characters for a value in "pattern" is not respected, the function returns null. Default value: true
Pattern | Description |
---|---|
dd | day of the month containing two characters |
d | day of the month |
MM | month of the year (minimum 1) containing two characters |
M | month of the year |
yyyy | full year containing four characters |
y | full year |
hh | hours of day with two characters |
h | hours of day |
mm | minutes of hour with two characters |
m | minutes of hour |
ss | seconds of minute with two characters |
s | seconds of minute |
SSS | millisecond of second with tree characters |
SS | millisecond of second with at least two characters |
S | millisecond of second |
examples
// returns Date // returns Date // returns Date // returns null, invalid millisecond // returns Date // returns Date // returns Date // returns Date // returns null, invalid millisecond // returns Date // returns null, invalid month // returns Date
dateToStr(date : Date|String, pattern : String)
Converts a date to a string in the format described in the pattern
parameters
-
date: date (or string in ISO format) to convert to string
-
pattern: date format (default value: 'yyyy/MM/dd')
Pattern | Description |
---|---|
dd | day of the month containing two characters |
d | day of the month |
MM | month of the year (minimum 1) containing two characters |
M | month of the year |
yyyy | full year containing four characters |
yy | year containing the last two digits |
y | full year |
hh | hours of day with two characters |
h | hours of day |
mm | minutes of hour with two characters |
m | minutes of hour |
ss | seconds of minute with two characters |
s | seconds of minute |
SSS | millisecond of second with tree characters |
SS | millisecond of second with at least two characters |
S | millisecond of second |
examples
// 10/06/2019 21:13 // 10/6/2019 21:13 // 10/6/19 21:13 // 10/6/2019 21:13:26.002 // 10/6/2019 21:13:26.02 // 10/6/2019 21:13:26.2 // 10/6/2019 21:13:26.27 // null
getMinPattern(strDate: String, pattern: String)
Returns the minimum pattern (strictly necessary) of a given formatted string representing a date
parameters
- strDate: date in string format
- pattern: 'strDate' parameter date format
Pattern | Description |
---|---|
dd | day of the month containing two characters |
d | day of the month |
MM | month of the year (minimum 1) containing two characters |
M | month of the year |
yyyy | full year containing four characters |
yy | year containing the last two digits |
y | full year |
hh | hours of day with two characters |
h | hours of day |
mm | minutes of hour with two characters |
m | minutes of hour |
ss | seconds of minute with two characters |
s | seconds of minute |
SSS | millisecond of second with tree characters |
SS | millisecond of second with at least two characters |
S | millisecond of second |
examples
let date = let minPattern = // dd/MM/yyyy hh:mmdate = // 10/06/2020 21:13 date = minPattern = // dd/MM/yyyy hh:mmdate = // 10/06/2020 21:13 // null
plus(date: Date|String, period: String|Number, duration : Number)
Adds a value of a time period on a date
parameters
-
date: date (or string in ISO format) to be increased by a period of time
-
period: textual or numeric representation (stored in 'PERIODS') of a period of time to be added to the date
-
duration: unit of time to be added to the date
examples
// date with one day less // date with one year more
dateEquals(date1 : Date|String, date2 : Date|String, ignore : Number)
Returns true if both dates are equal, ignoring certain lower values
parameters
-
date1: first date (or string in ISO format) of comparison
-
date2: second date (or string in ISO format) of comparison
-
ignore: position from which the lowest values will be ignored (if not informed, nothing is ignored.)
ignore | ignored values |
---|---|
default | nothing ignored |
7 | nothing ignored |
6 | millisecond |
5 | millisecond and second |
4 | millisecond, second and minute |
3 | millisecond, second, minute and hour |
2 | millisecond, second, minute, hour and day |
1 | millisecond, second, minute, hour, day and month |
0 | ignoring everything, that is, the dates are the same |
examples
// true // false // ignoring millisecond, second, minute, hour and day // true // ignoring millisecond, second, minute and hour // true // ignoring millisecond, second and minute // false
dateEqualsReverse(date1 : Date|String, date2 : Date|String, ignore : Number)
Returns true if both dates are equal, ignoring certain higher values
parameters
-
date1: first date (or string in ISO format) of comparison
-
date2: second date (or string in ISO format) of comparison
-
ignore: position from which the highest values will be ignored (if not informed, nothing will be ignored).
ignore | ignored values |
---|---|
default | nothing ignored |
7 | nothing ignored |
6 | year |
5 | year and month |
4 | year, month and day |
3 | year, month, day and hour |
2 | year, month, day, hour and minute |
1 | year, month, day, hour, minute and second |
0 | ignoring everything, that is, the dates are the same |
examples
// true // false // ignoring year, month and day // true // ignoring year and month // true // ignoring year // false
getDateIgnore(date : Date|String, ignore : Number)
Gets date ignoring lower values
parameters
-
date: date (or string in ISO format) that will have higher values ignored
-
ignore: position from which the lowest values will be ignored (if not informed, nothing is ignored.)
ignore | ignored values |
---|---|
default | nothing ignored |
7 | nothing ignored |
6 | millisecond |
5 | millisecond and second |
4 | millisecond, second and minute |
3 | millisecond, second, minute and hour |
2 | millisecond, second, minute, hour and day |
1 | millisecond, second, minute, hour, day and month |
0 | ignoring everything |
examples
// Date only with year, month and day // Date only with year, month, day and hour // gets exactly the same date
getDateIgnoreReverse(date : Date|String, ignore : Number)
Gets date ignoring high values
parameters
-
date: date (or string in ISO format) that will have higher values ignored
-
ignore: position from which the highest values will be ignored (if not informed, nothing will be ignored).
ignore | ignored values |
---|---|
default | nothing ignored |
7 | nothing ignored |
6 | year |
5 | year and month |
4 | year, month and day |
3 | year, month, day and hour |
2 | year, month, day, hour and minute |
1 | year, month, day, hour, minute and second |
0 | ignoring everything, that is, the dates are the same |
examples
// Date only with hour, minute, second and millisecond // Date only with day, hour, minute, second and millisecond // gets exactly the same date
formatTime(time : Number, ...args : Number)
Gets values from a time
parameters
- time: milliseconds obtained by the getTime() function
- ...args: types of values to be extracted and placed in an array
examples
// [<amount of minutes in 200100 milliseconds>, <number of seconds remaining>]
Attention!
The MONTH, TWO_MONTHS, SEMESTER, QUARTER, and YEAR variables can not be used in formatTime.
dateInApointment(date : Date | String, target : Date|String, period : String|Number, duration : Number, marginErrorPeriod: string | number, marginErrorDuration: number)
Returns true if the date is present within a recurring schedule.
parameters
-
date: first scheduling date (or string in ISO format)
-
target: check to see if you are on scheduling.
-
period: textual or numeric representation (stored in 'PERIODS') of a time period of the schedule
-
duration: unit to include new periodic dates in schedule
-
marginErrorPeriod: textual or numeric representation (stored in 'PERIODS') of a programming period to be used to define a "margin of error". (Default value: PERIODS.MILLISECOND)
-
marginErrorDuration: margin of error value. (Default value: 0)
examples
// returns true because the date 2025/07/02 is included in a timeline for each semester from the date of 2000/01/02 // returns false because the date 2025/07/02 is not included in a timeline for each two semester from the date of 2000/01/02 // returns false because the date 2025/07/03 is not included in a timeline for each semester from the date 2000/01/01 and the margin of error is only 1 day // returns true because although the date 2025/07/02 is not included in a timeline for each semester from the date 2000/01/01, the margin of error has been set to 1 day
scape(str : String)
Returns string with special regular expression characters with escape
parameters
- str: string to have its special RegExp characters with escape
example
// ab\.\*\+\?\^\$\{c\}\(\)\|d\[\]\\ef\/