datetime-periods
Get a valid list of years down to seconds for a date and min and max date.
You ever wanted to create a simple date time picker or wanted to show the user available dates or only the days?
It is a zero dependecy lib for browser and node environments.
Donate/Support
If you like my work, feel free to support it. Donations to the project are always welcomed :)
PayPal: PayPal.Me/bengtler
BTC Wallet Address:
3QVyr2tpRLBCw1kBQ59sTDraV6DTswq8Li
ETH Wallet Address:
0x394d44f3b6e3a4f7b4d44991e7654b0cab4af68f
LTC Wallet Address:
MFif769WSZ1g7ReAzzDE7TJVqtkFpmoTyT
XRP Wallet Address:
rXieaAC3nevTKgVu2SYoShjTCS2Tfczqx?dt=159046833
Installation
npm install datetime-periods
yarn add datetime-periods
Usage
Script tag
If you want to use datetime-periods via script tag, you should use the datetime-periods.umd.js
. After the script is loaded you can access the methods through the global datetimePeriods
.
ESModules or Typescript
Just import the methods you need from the package.
Node and require
Just require
the methods you need from the package.
Example
Some simple and easy to use examples can be found in the example
folder in the repo.
There is also a plain JavaScript example page.
Or use RunKit to try it out!
Methods
getDateTimeObject(date: Date)
Transforms a date in an object respresentation.
Result value:
// DateTimeObject interface
Example:
console.logdateTimeObject
Output:
getDaysInMonth(year: number, month: number)
Returns the day count of a month and year. The month is based on numbers from 1 (january) to 12 (december).
Example:
console.logdaysInFebruary2019
Output:
28
getDateTimePeriods(value: Date, min: Date (optional), max: Date (optional), needed: NeededPeriods (optional))
This method contains the main functionality of the lib. The other methods are only internally used, but maybe someone finds them useful :).
It generates a data structure of valid years, months, days, hours, minutes and seconds for a given date and an optional min and max date. In addition to that it returns all the calculation dates as object representation.
If only some period values are interesting, it is possible to only retrieve, years, months, ... . Therefore use the needed
parameter and pass an object of the values you do not need. This periods keys will be an empty array.
getDateTimePeriodsnew Date, undefined, undefined,
Result value
Example:
min.setFullYearvalue.getFullYear - 5 max.setFullYearvalue.getFullYear + 5 console.logresult
Output:
Default min and max values
- min:
value - 100 years
- max:
value + 100 years
Special cases
Leading to a console.warn
!
min > max
: min and max are fall back to the defaultvalue < min
: value is set to min, setsoriginalValueChanged
totrue
value > max
: value is set to max, setsoriginalValueChanged
totrue