dates-range-parser
Advanced tools
Comparing version 1.0.3 to 1.0.4
44
index.js
@@ -28,2 +28,4 @@ /*! | ||
drp.UTC = false; // set to true to use UTC dates (default is local time) | ||
drp.defaultRange = 1000 * 60 * 60 * 24; | ||
@@ -44,4 +46,8 @@ | ||
value: { | ||
fromDate: new Date(r.start).toISOString(), | ||
toDate: new Date(r.end).toISOString(), | ||
fromDate: drp.UTC | ||
? new Date(r.start).toISOString() | ||
: new Date(r.start).toString(), | ||
toDate: drp.UTC | ||
? new Date(r.end).toISOString() | ||
: new Date(r.end).toString(), | ||
timeRange: v, | ||
@@ -89,11 +95,21 @@ }, | ||
const da = new Date(d); | ||
return [ | ||
da.getUTCFullYear(), | ||
da.getUTCMonth() + 1, | ||
da.getUTCDate(), | ||
da.getUTCHours(), | ||
da.getUTCMinutes(), | ||
da.getUTCSeconds(), | ||
da.getUTCMilliseconds(), | ||
]; | ||
return drp.UTC | ||
? [ | ||
da.getUTCFullYear(), | ||
da.getUTCMonth() + 1, | ||
da.getUTCDate(), | ||
da.getUTCHours(), | ||
da.getUTCMinutes(), | ||
da.getUTCSeconds(), | ||
da.getUTCMilliseconds(), | ||
] | ||
: [ | ||
da.getFullYear(), | ||
da.getMonth() + 1, | ||
da.getDate(), | ||
da.getHours(), | ||
da.getMinutes(), | ||
da.getSeconds(), | ||
da.getMilliseconds(), | ||
]; | ||
} | ||
@@ -105,3 +121,3 @@ | ||
d[1]--; | ||
return Date.UTC.apply(null, d); | ||
return drp.UTC ? new Date(Date.UTC(...d)) : new Date(...d); | ||
} | ||
@@ -233,3 +249,5 @@ | ||
if (r) { | ||
dt[2] -= new Date(fromArray(dt)).getUTCDay(); | ||
dt[2] -= drp.UTC | ||
? new Date(fromArray(dt)).getUTCDay() | ||
: new Date(fromArray(dt)).getDay(); | ||
} | ||
@@ -236,0 +254,0 @@ return makePrecRange(dt, p, r); |
{ | ||
"name": "dates-range-parser", | ||
"version": "1.0.3", | ||
"version": "1.0.4", | ||
"author": "Xoredge <info@xoredge.com>", | ||
@@ -5,0 +5,0 @@ "description": "A simple parser for dates range", |
161
README.md
@@ -26,7 +26,9 @@ # Date Range Parser | ||
```javascript | ||
DatesRangeParser.parse('yesterday') // returns {start: Date, end: Date} for the previous day | ||
DatesRangeParser.parse('now -> 7days') // returns {start: Date, end: Date} for the next 7 days (week) from today | ||
DatesRangeParser.UTC = true; // calculate all dates input and outputs in UTC; | ||
DatesRangeParser.parse("yesterday"); // returns {start: Date, end: Date} for the previous day | ||
DatesRangeParser.parse("now -> 7days"); // returns {start: Date, end: Date} for the next 7 days (week) from today | ||
``` | ||
### Explanation | ||
### Explanation | ||
`DatesRangeParser.parse()` either returns null, if the string cannot be converted, or an object with start and end attributes. | ||
@@ -38,4 +40,3 @@ start and end are either null (meaning no constraint) or the number of seconds since epoch. | ||
DatesRangeParser.js works entirely in UTC / GMT / Z (+0) timezone. Usually databases will store dates like this. | ||
If local time is preferred, the date can be extracted with the timezone applied using methods from the JavaScript Date object. | ||
DateRangeParser has a property .UTC which is by default false, and all calculations are happening in local timezone. If set to true DatesRangeParser.js wil work entirely in UTC / GMT / Z (+0) timezone. Usually databases will store dates like this. | ||
@@ -49,44 +50,44 @@ #### Note on now | ||
* now | ||
* today | ||
* tomorrow | ||
* yesterday | ||
* last/this/next week | ||
* last/this/next month | ||
* last/this/next year | ||
- now | ||
- today | ||
- tomorrow | ||
- yesterday | ||
- last/this/next week | ||
- last/this/next month | ||
- last/this/next year | ||
* 1000secs | ||
* 5mins | ||
* 1day | ||
* 2days | ||
* 8d | ||
* 9months | ||
* 2yrs | ||
- 1000secs | ||
- 5mins | ||
- 1day | ||
- 2days | ||
- 8d | ||
- 9months | ||
- 2yrs | ||
* 5 | ||
* 5:35 | ||
* 5:35:12 | ||
- 5 | ||
- 5:35 | ||
- 5:35:12 | ||
* 2011 | ||
* 2011-03 | ||
* 2011-03-04 | ||
- 2011 | ||
- 2011-03 | ||
- 2011-03-04 | ||
* 2011-03-04 04 | ||
* 2011-03-04 04:15 | ||
* 2011-03-04 04:15:29 | ||
- 2011-03-04 04 | ||
- 2011-03-04 04:15 | ||
- 2011-03-04 04:15:29 | ||
* 2010 -> 2011 | ||
* 2005-11-05 16:13:49 -> 2005-11-06 05:12:11 | ||
* last week -> next week | ||
* 2011-05 -> | ||
* < now | ||
* 2000-01-01 -> last week | ||
- 2010 -> 2011 | ||
- 2005-11-05 16:13:49 -> 2005-11-06 05:12:11 | ||
- last week -> next week | ||
- 2011-05 -> | ||
- < now | ||
- 2000-01-01 -> last week | ||
* 2000 -> 10y | ||
* 3mins < now | ||
* last year -> 6months | ||
- 2000 -> 10y | ||
- 3mins < now | ||
- last year -> 6months | ||
* 2010-05-13 05:13 <> 10m | ||
* now <> 1yr | ||
* lastweek <> 1month | ||
- 2010-05-13 05:13 <> 10m | ||
- now <> 1yr | ||
- lastweek <> 1month | ||
@@ -97,10 +98,10 @@ ## Syntax in More Detail | ||
* now | ||
* today | ||
* tomorrow | ||
* yesterday | ||
* last/this/next week | ||
* last/this/next month | ||
* last/this/next quarter | ||
* last/this/next year | ||
- now | ||
- today | ||
- tomorrow | ||
- yesterday | ||
- last/this/next week | ||
- last/this/next month | ||
- last/this/next quarter | ||
- last/this/next year | ||
@@ -111,9 +112,9 @@ Creates a range covering all value dates relative to now. | ||
* 1000secs | ||
* 5mins | ||
* 1day | ||
* 2days | ||
* 8d | ||
* 9months | ||
* 2yrs | ||
- 1000secs | ||
- 5mins | ||
- 1day | ||
- 2days | ||
- 8d | ||
- 9months | ||
- 2yrs | ||
@@ -123,2 +124,3 @@ Entering a range alone creates a date search centered on now and spreading into the past and future by the specified amount. | ||
Given now is 2001-09-09 01:46:40 | ||
- "3days" searches from 3 days in the past to 3 days in the future (2001-09-03 01:46:40 -> 2001-09-12 01:46:40) | ||
@@ -129,2 +131,3 @@ - "1hr" searches from 1 hour in the past to 1 hour in the future (2001-09-09 00:46:40 -> 2001-09-09 02:46:40) | ||
The following aliases can be used with ranges: | ||
- seconds: s, sec, secs, second, seconds | ||
@@ -142,7 +145,8 @@ - minutes: m, min, mins, minute, minutes | ||
* 2011 | ||
* 2011-03 | ||
* 2011-03-04 | ||
- 2011 | ||
- 2011-03 | ||
- 2011-03-04 | ||
A date format alone will search the range of dates covered by the date. | ||
- "2011" searches the year of 2011 (2011-01-01 00:00:00.000 -> 2011-12-31 23:59:59.999) | ||
@@ -156,8 +160,9 @@ - "2011-03" searches the month of March 2011 (2011-03-01 00:00:00.000 -> 2011-03-31 23:59:59.999) | ||
* 5 | ||
* 5:35 | ||
* 5:35:12 | ||
- 5 | ||
- 5:35 | ||
- 5:35:12 | ||
Entering a time creates a range in today. | ||
Examples: | ||
- "5" searches the hour of 5 AM for today (today 05:00:00.000 -> today 05:59:59.999) | ||
@@ -171,8 +176,9 @@ - "5:35" searches the minute of 5:35 for today (today 05:35:00.000 -> today 05:35:59.999) | ||
* 2011-03-04 04 | ||
* 2011-03-04 04:15 | ||
* 2011-03-04 04:15:29 | ||
- 2011-03-04 04 | ||
- 2011-03-04 04:15 | ||
- 2011-03-04 04:15:29 | ||
Entering a date and time part creates a range on the specified interval. | ||
Examples: | ||
- "2011-03-04 04" searches the hour of 4 AM, 4th March 2011 (2011-03-04 04:00:00.000 -> 2011-03-04 04:59:59.999) | ||
@@ -184,10 +190,11 @@ - "2011-03-04 04:15" searches the minute of 15 minutes past 4 AM, 4th March 2011 (2011-03-04 04:15:00.000 -> 2011-03-04 04:15:59.999) | ||
* 2010 -> 2011 | ||
* 2005-11-05 16:13:49 -> 2005-11-06 05:12:11 | ||
* last week -> next week | ||
* 2011-05 -> | ||
* < now | ||
* 2000-01-01 -> last week | ||
- 2010 -> 2011 | ||
- 2005-11-05 16:13:49 -> 2005-11-06 05:12:11 | ||
- last week -> next week | ||
- 2011-05 -> | ||
- < now | ||
- 2000-01-01 -> last week | ||
Entering two dates separated by "->" creates a range between the two dates. | ||
- "2010 -> 2011" searches the range from 2010-01-01 00:00:00.000 to 2011-12-31 23:59:59.999 | ||
@@ -202,7 +209,8 @@ - "2005-11-05 16:13:49 -> 2005-11-06 05:12:11" searches the range from 2005-11-05 16:13:49.000 to 2005-11-06 05:12:11.999 | ||
* 2000 -> 10y | ||
* 3mins < now | ||
* last year -> 6months | ||
- 2000 -> 10y | ||
- 3mins < now | ||
- last year -> 6months | ||
Ranges can be combined to create more complex queries. | ||
- "2000 -> 10y" searches the range from 2000-01-01 00:00:00.000 to 10 years in the future | ||
@@ -214,7 +222,8 @@ - "3mins < now" searches the range from 3 minutes in the past to the current moment (exclusive) | ||
* 2010-05-13 05:13 <> 10m | ||
* now <> 1yr | ||
* lastweek <> 1month | ||
- 2010-05-13 05:13 <> 10m | ||
- now <> 1yr | ||
- lastweek <> 1month | ||
Entering a date or range followed by "<>" and a range creates an offset search. | ||
- "2010-05-13 05:13 <> 10m" searches the range 10 minutes before and after 2010-05-13 05:13:00.000 | ||
@@ -221,0 +230,0 @@ - "now <> 1yr" searches the range 1 year before and after the current moment (exclusive) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
27760
517
223