datetime-periods
Advanced tools
Comparing version 0.1.0 to 1.0.0
@@ -1,2 +0,2 @@ | ||
function e(e,t){return new Date(new Date((e<12?t:t+1)+"-"+(e<12?e+1:1)+"-1").setDate(0)).getDate()}function t(e){return{year:e.getFullYear(),month:e.getMonth()+1,day:e.getDate(),hour:e.getHours(),minute:e.getMinutes(),second:e.getSeconds(),tzOffset:e.getTimezoneOffset()}}exports.getDaysInMonth=e,exports.getDateTimeObject=t,exports.getValidDates=function(n,r,a){void 0===n&&(n=new Date),n=new Date(n.setMilliseconds(0));var o=new Date(n),u=!1,i=new Date(new Date(n).setFullYear(n.getFullYear()-100)),l=new Date(new Date(n).setFullYear(n.getFullYear()+100));r=r?new Date(r.setMilliseconds(0)):i,a=a?new Date(a.setMilliseconds(0)):l;var m=r.getTime(),f=a.getTime(),s=n.getTime();m>f&&(console.warn("min > max, setting min and max to defaults"),r=i,m=i.getTime(),a=l,f=l.getTime()),m>s&&(console.warn("min > value, setting value to min"),n=new Date(r),s=m,u=!0),f<s&&(console.warn("max < value, setting value to max"),n=new Date(a),s=f,u=!0);var c=t(r),g=t(a),h=t(n),y=Array.from({length:e(h.month,h.year)},function(e,t){return t+1}),d=Array.from({length:12},function(e,t){return t+1}),D=Array.from({length:24},function(e,t){return t}),w=Array.from({length:60},function(e,t){return t}),v=Array.from({length:60},function(e,t){return t});h.year===c.year&&(d=d.filter(function(e){return e>=c.month}),h.month===c.month&&(y=y.filter(function(e){return e>=c.day}),h.day===c.day&&(D=D.filter(function(e){return e>=c.hour}),h.hour===c.hour&&(w=w.filter(function(e){return e>=c.minute}),h.minute===c.minute&&(v=v.filter(function(e){return e>=c.second})))))),h.year===g.year&&(d=d.filter(function(e){return e<=g.month}),h.month===g.month&&(y=y.filter(function(e){return e<=g.day}),h.day===g.day&&(D=D.filter(function(e){return e<=g.hour}),h.hour===g.hour&&(w=w.filter(function(e){return e<=g.minute}),h.minute===g.minute&&(v=v.filter(function(e){return e<=g.second}))))));var x=Array.from({length:g.year-c.year+1},function(e,t){return c.year+t});return{value:h,originalValue:t(o),originalValueChanged:u,max:g,min:c,validDateTimes:{days:y,hours:D,minutes:w,months:d,seconds:v,years:x}}}; | ||
function e(e,t){return new Date(new Date((e<12?t:t+1)+"-"+(e<12?e+1:1)+"-1").setDate(0)).getDate()}function t(e){return{year:e.getFullYear(),month:e.getMonth()+1,day:e.getDate(),hour:e.getHours(),minute:e.getMinutes(),second:e.getSeconds(),tzOffset:e.getTimezoneOffset()}}exports.getDaysInMonth=e,exports.getDateTimeObject=t,exports.getDateTimePeriods=function(n,r,a){void 0===n&&(n=new Date),n=new Date(n.setMilliseconds(0));var o=new Date(n),u=!1,i=new Date(new Date(n).setFullYear(n.getFullYear()-100)),l=new Date(new Date(n).setFullYear(n.getFullYear()+100));r=r?new Date(r.setMilliseconds(0)):i,a=a?new Date(a.setMilliseconds(0)):l;var m=r.getTime(),f=a.getTime(),s=n.getTime();m>f&&(console.warn("min > max, setting min and max to defaults"),r=i,m=i.getTime(),a=l,f=l.getTime()),m>s&&(console.warn("min > value, setting value to min"),n=new Date(r),s=m,u=!0),f<s&&(console.warn("max < value, setting value to max"),n=new Date(a),s=f,u=!0);var c=t(r),g=t(a),h=t(n),y=Array.from({length:e(h.month,h.year)},function(e,t){return t+1}),d=Array.from({length:12},function(e,t){return t+1}),D=Array.from({length:24},function(e,t){return t}),w=Array.from({length:60},function(e,t){return t}),v=Array.from({length:60},function(e,t){return t});h.year===c.year&&(d=d.filter(function(e){return e>=c.month}),h.month===c.month&&(y=y.filter(function(e){return e>=c.day}),h.day===c.day&&(D=D.filter(function(e){return e>=c.hour}),h.hour===c.hour&&(w=w.filter(function(e){return e>=c.minute}),h.minute===c.minute&&(v=v.filter(function(e){return e>=c.second})))))),h.year===g.year&&(d=d.filter(function(e){return e<=g.month}),h.month===g.month&&(y=y.filter(function(e){return e<=g.day}),h.day===g.day&&(D=D.filter(function(e){return e<=g.hour}),h.hour===g.hour&&(w=w.filter(function(e){return e<=g.minute}),h.minute===g.minute&&(v=v.filter(function(e){return e<=g.second}))))));var x=Array.from({length:g.year-c.year+1},function(e,t){return c.year+t});return{value:h,originalValue:t(o),originalValueChanged:u,max:g,min:c,periods:{days:y,hours:D,minutes:w,months:d,seconds:v,years:x}}}; | ||
//# sourceMappingURL=datetime-periods.js.map |
@@ -1,2 +0,2 @@ | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t(e.datetimePeriods={})}(this,function(e){function t(e,t){return new Date(new Date((e<12?t:t+1)+"-"+(e<12?e+1:1)+"-1").setDate(0)).getDate()}function n(e){return{year:e.getFullYear(),month:e.getMonth()+1,day:e.getDate(),hour:e.getHours(),minute:e.getMinutes(),second:e.getSeconds(),tzOffset:e.getTimezoneOffset()}}e.getDaysInMonth=t,e.getDateTimeObject=n,e.getValidDates=function(e,r,o){void 0===e&&(e=new Date),e=new Date(e.setMilliseconds(0));var a=new Date(e),i=!1,u=new Date(new Date(e).setFullYear(e.getFullYear()-100)),f=new Date(new Date(e).setFullYear(e.getFullYear()+100));r=r?new Date(r.setMilliseconds(0)):u,o=o?new Date(o.setMilliseconds(0)):f;var l=r.getTime(),m=o.getTime(),s=e.getTime();l>m&&(console.warn("min > max, setting min and max to defaults"),r=u,l=u.getTime(),o=f,m=f.getTime()),l>s&&(console.warn("min > value, setting value to min"),e=new Date(r),s=l,i=!0),m<s&&(console.warn("max < value, setting value to max"),e=new Date(o),s=m,i=!0);var c=n(r),d=n(o),g=n(e),y=Array.from({length:t(g.month,g.year)},function(e,t){return t+1}),h=Array.from({length:12},function(e,t){return t+1}),D=Array.from({length:24},function(e,t){return t}),w=Array.from({length:60},function(e,t){return t}),v=Array.from({length:60},function(e,t){return t});g.year===c.year&&(h=h.filter(function(e){return e>=c.month}),g.month===c.month&&(y=y.filter(function(e){return e>=c.day}),g.day===c.day&&(D=D.filter(function(e){return e>=c.hour}),g.hour===c.hour&&(w=w.filter(function(e){return e>=c.minute}),g.minute===c.minute&&(v=v.filter(function(e){return e>=c.second})))))),g.year===d.year&&(h=h.filter(function(e){return e<=d.month}),g.month===d.month&&(y=y.filter(function(e){return e<=d.day}),g.day===d.day&&(D=D.filter(function(e){return e<=d.hour}),g.hour===d.hour&&(w=w.filter(function(e){return e<=d.minute}),g.minute===d.minute&&(v=v.filter(function(e){return e<=d.second}))))));var x=Array.from({length:d.year-c.year+1},function(e,t){return c.year+t});return{value:g,originalValue:n(a),originalValueChanged:i,max:d,min:c,validDateTimes:{days:y,hours:D,minutes:w,months:h,seconds:v,years:x}}}}); | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t(e.datetimePeriods={})}(this,function(e){function t(e,t){return new Date(new Date((e<12?t:t+1)+"-"+(e<12?e+1:1)+"-1").setDate(0)).getDate()}function n(e){return{year:e.getFullYear(),month:e.getMonth()+1,day:e.getDate(),hour:e.getHours(),minute:e.getMinutes(),second:e.getSeconds(),tzOffset:e.getTimezoneOffset()}}e.getDaysInMonth=t,e.getDateTimeObject=n,e.getDateTimePeriods=function(e,r,o){void 0===e&&(e=new Date),e=new Date(e.setMilliseconds(0));var i=new Date(e),a=!1,u=new Date(new Date(e).setFullYear(e.getFullYear()-100)),f=new Date(new Date(e).setFullYear(e.getFullYear()+100));r=r?new Date(r.setMilliseconds(0)):u,o=o?new Date(o.setMilliseconds(0)):f;var l=r.getTime(),m=o.getTime(),s=e.getTime();l>m&&(console.warn("min > max, setting min and max to defaults"),r=u,l=u.getTime(),o=f,m=f.getTime()),l>s&&(console.warn("min > value, setting value to min"),e=new Date(r),s=l,a=!0),m<s&&(console.warn("max < value, setting value to max"),e=new Date(o),s=m,a=!0);var c=n(r),d=n(o),g=n(e),y=Array.from({length:t(g.month,g.year)},function(e,t){return t+1}),h=Array.from({length:12},function(e,t){return t+1}),D=Array.from({length:24},function(e,t){return t}),w=Array.from({length:60},function(e,t){return t}),v=Array.from({length:60},function(e,t){return t});g.year===c.year&&(h=h.filter(function(e){return e>=c.month}),g.month===c.month&&(y=y.filter(function(e){return e>=c.day}),g.day===c.day&&(D=D.filter(function(e){return e>=c.hour}),g.hour===c.hour&&(w=w.filter(function(e){return e>=c.minute}),g.minute===c.minute&&(v=v.filter(function(e){return e>=c.second})))))),g.year===d.year&&(h=h.filter(function(e){return e<=d.month}),g.month===d.month&&(y=y.filter(function(e){return e<=d.day}),g.day===d.day&&(D=D.filter(function(e){return e<=d.hour}),g.hour===d.hour&&(w=w.filter(function(e){return e<=d.minute}),g.minute===d.minute&&(v=v.filter(function(e){return e<=d.second}))))));var x=Array.from({length:d.year-c.year+1},function(e,t){return c.year+t});return{value:g,originalValue:n(i),originalValueChanged:a,max:d,min:c,periods:{days:y,hours:D,minutes:w,months:h,seconds:v,years:x}}}}); | ||
//# sourceMappingURL=datetime-periods.umd.js.map |
@@ -0,1 +1,8 @@ | ||
/** | ||
* Returns the days count in a month. | ||
* | ||
* @param month - The month number (1-12) | ||
* @param year - The full year number | ||
* @returns The days in a month as number | ||
*/ | ||
export declare function getDaysInMonth(month: number, year: number): number; | ||
@@ -17,3 +24,3 @@ export interface DateTimeObject { | ||
min: DateTimeObject; | ||
validDateTimes: { | ||
periods: { | ||
days: number[]; | ||
@@ -27,3 +34,17 @@ hours: number[]; | ||
} | ||
/** | ||
* Returns a date object based on a JS date. | ||
* | ||
* @param date - A javascript date | ||
* @returns The date as a plain old javascript object representation | ||
*/ | ||
export declare function getDateTimeObject(date: Date): DateTimeObject; | ||
export declare function getValidDates(value?: Date, min?: Date, max?: Date): GetValidDatesResult; | ||
/** | ||
* Returns a valid list of years down to seconds for a date and min and max date. | ||
* | ||
* @param value - A javascript date | ||
* @param min - The optional min date | ||
* @param max - The optional max date | ||
* @returns The date params as object representation and the valid periods | ||
*/ | ||
export declare function getDateTimePeriods(value?: Date, min?: Date, max?: Date): GetValidDatesResult; |
{ | ||
"name": "datetime-periods", | ||
"version": "0.1.0", | ||
"version": "1.0.0", | ||
"description": "Generates a valid list of years down to seconds for a date and min and max date", | ||
@@ -5,0 +5,0 @@ "scripts": { |
231
README.md
@@ -10,2 +10,20 @@ # datetime-periods | ||
## Donate/Support | ||
If you like my work, feel free to support it. Donations to the project are always welcomed :) | ||
PayPal: [PayPal.Me/bengtler](https://paypal.me/bengtler) | ||
BTC Wallet Address: | ||
`3QVyr2tpRLBCw1kBQ59sTDraV6DTswq8Li` | ||
ETH Wallet Address: | ||
`0x394d44f3b6e3a4f7b4d44991e7654b0cab4af68f` | ||
LTC Wallet Address: | ||
`MFif769WSZ1g7ReAzzDE7TJVqtkFpmoTyT` | ||
XRP Wallet Address: | ||
`rXieaAC3nevTKgVu2SYoShjTCS2Tfczqx?dt=159046833` | ||
## Installation | ||
@@ -16,2 +34,49 @@ | ||
## 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`. | ||
```html | ||
<html> | ||
<head> | ||
<script src="../lib/datetime-periods.umd.js"></script> | ||
<script> | ||
// functions are stored globally on the window object | ||
console.log('window.datetimePeriods:', window.datetimePeriods) | ||
</script> | ||
</head> | ||
<body> | ||
</body> | ||
</html> | ||
``` | ||
### ESModules or Typescript | ||
Just import the methods you need from the package. | ||
```typescript | ||
import { getDateTimeObject } from 'datetime-periods' | ||
import * as dateTimePeriods from 'datetime-periods' | ||
``` | ||
### Node and require | ||
Just `require` the methods you need from the package. | ||
```typescript | ||
const getDateTimeObject = require('datetime-periods').getDateTimeObject | ||
const dateTimePeriods = require('datetime-periods') | ||
``` | ||
### 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](https://killercodemonkey.github.io/datetime-periods/example). | ||
Or use [RunKit](https://npm.runkit.com/datetime-periods) to try it out! | ||
## Methods | ||
@@ -21,4 +86,6 @@ | ||
Transforms a date in a object respresentation: | ||
Transforms a date in an object respresentation. | ||
**Result value**: | ||
```typescript | ||
@@ -37,6 +104,43 @@ // DateTimeObject interface | ||
Example: | ||
```typescript | ||
import { getDateTimeObject } from 'datetime-periods' | ||
const dateTimeObject = getDateTimeObject(new Date()) | ||
console.log(dateTimeObject) | ||
``` | ||
Output: | ||
```json | ||
{ | ||
"day": 14, | ||
"hour": 13, | ||
"minute": 15, | ||
"month": 3, | ||
"second": 15, | ||
"tzOffset": -60, | ||
"year": 2019 | ||
} | ||
``` | ||
### getDaysInMonth(year: number, month: number) | ||
Returns the day count of a month and year. | ||
Returns the day count of a month and year. The month is based on numbers from 1 (january) to 12 (december). | ||
Example: | ||
```typescript | ||
import { getDaysInMonth } from 'datetime-periods' | ||
const daysInFebruary2019 = getDaysInMonth(2, 2019) | ||
console.log(daysInFebruary2019) | ||
``` | ||
Output: | ||
```typescript | ||
28 | ||
``` | ||
### getDateTimePeriods(value: Date, min: Date (optional), max: Date (optional)) | ||
@@ -48,2 +152,3 @@ | ||
**Result value** | ||
```typescript | ||
@@ -67,4 +172,124 @@ { | ||
#### Default min and max | ||
Example: | ||
```typescript | ||
import { getDateTimePeriods } from 'datetime-periods' | ||
const value = new Date() | ||
const min = new Date() | ||
min.setFullYear(value.getFullYear() - 5) | ||
const max = new Date() | ||
max.setFullYear(value.getFullYear() + 5) | ||
const result = getDateTimePeriods(value, min, max) | ||
console.log(result) | ||
``` | ||
Output: | ||
```json | ||
{ | ||
"value": { | ||
"year": 2019, | ||
"month": 3, | ||
"day": 14, | ||
"hour": 14, | ||
"minute": 40, | ||
"second": 21, | ||
"tzOffset": -60 | ||
}, | ||
"originalValue": { | ||
"year": 2019, | ||
"month": 3, | ||
"day": 14, | ||
"hour": 14, | ||
"minute": 40, | ||
"second": 21, | ||
"tzOffset": -60 | ||
}, | ||
"originalValueChanged": false, | ||
"max": { | ||
"year": 2024, | ||
"month": 1, | ||
"day": 1, | ||
"hour": 1, | ||
"minute": 0, | ||
"second": 2, | ||
"tzOffset": -60 | ||
}, | ||
"min": { | ||
"year": 2014, | ||
"month": 3, | ||
"day": 14, | ||
"hour": 14, | ||
"minute": 40, | ||
"second": 21, | ||
"tzOffset": -60 | ||
}, | ||
"periods": { | ||
"days": [ | ||
1, | ||
2, | ||
3, | ||
... | ||
28, | ||
29, | ||
30, | ||
31 | ||
], | ||
"hours": [ | ||
0, | ||
1, | ||
2, | ||
... | ||
21, | ||
22, | ||
23 | ||
], | ||
"minutes": [ | ||
0, | ||
1, | ||
2, | ||
3, | ||
... | ||
56, | ||
57, | ||
58, | ||
59 | ||
], | ||
"months": [ | ||
1, | ||
2, | ||
3, | ||
... | ||
10, | ||
11, | ||
12 | ||
], | ||
"seconds": [ | ||
0, | ||
1, | ||
2, | ||
... | ||
57, | ||
58, | ||
59 | ||
], | ||
"years": [ | ||
2014, | ||
2015, | ||
2016, | ||
... | ||
2022, | ||
2023, | ||
2024 | ||
] | ||
} | ||
} | ||
``` | ||
#### Default min and max values | ||
- min: `value - 100 years` | ||
@@ -71,0 +296,0 @@ - max: `value + 100 years` |
@@ -0,1 +1,8 @@ | ||
/** | ||
* Returns the days count in a month. | ||
* | ||
* @param month - The month number (1-12) | ||
* @param year - The full year number | ||
* @returns The days in a month as number | ||
*/ | ||
export function getDaysInMonth(month: number, year: number): number { | ||
@@ -31,2 +38,8 @@ return new Date(new Date(`${month < 12 ? year : year + 1}-${month < 12 ? month + 1 : 1}-1`).setDate(0)).getDate() | ||
/** | ||
* Returns a date object based on a JS date. | ||
* | ||
* @param date - A javascript date | ||
* @returns The date as a plain old javascript object representation | ||
*/ | ||
export function getDateTimeObject(date: Date): DateTimeObject { | ||
@@ -44,2 +57,10 @@ return { | ||
/** | ||
* Returns a valid list of years down to seconds for a date and min and max date. | ||
* | ||
* @param value - A javascript date | ||
* @param min - The optional min date | ||
* @param max - The optional max date | ||
* @returns The date params as object representation and the valid periods | ||
*/ | ||
export function getDateTimePeriods(value: Date = new Date(), min?: Date, max?: Date): GetValidDatesResult { | ||
@@ -46,0 +67,0 @@ value = new Date(value.setMilliseconds(0)) |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
62660
19
645
0
299