README.md (4474B)
1 <!-- 2 3 @license Apache-2.0 4 5 Copyright (c) 2021 The Stdlib Authors. 6 7 Licensed under the Apache License, Version 2.0 (the "License"); 8 you may not use this file except in compliance with the License. 9 You may obtain a copy of the License at 10 11 http://www.apache.org/licenses/LICENSE-2.0 12 13 Unless required by applicable law or agreed to in writing, software 14 distributed under the License is distributed on an "AS IS" BASIS, 15 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 See the License for the specific language governing permissions and 17 limitations under the License. 18 19 --> 20 21 # Datespace 22 23 > Generate an array of linearly spaced [dates][mdn-date-object]. 24 25 <section class="usage"> 26 27 ## Usage 28 29 ```javascript 30 var datespace = require( '@stdlib/array/datespace' ); 31 ``` 32 33 #### datespace( start, stop\[, length]\[, opts] ) 34 35 Generates an `array` of linearly spaced [`Date`][mdn-date-object] objects. If a `length` is not provided, the default output `array` length is `100`. 36 37 ```javascript 38 var end = '2014-12-02T07:00:54.973Z'; 39 var start = new Date( end ) - 60000; 40 41 var arr = datespace( start, end, 6 ); 42 /* returns [ 43 'Mon Dec 01 2014 22:59:54 GMT-0800 (PST)', 44 'Mon Dec 01 2014 23:00:06 GMT-0800 (PST)', 45 'Mon Dec 01 2014 23:00:18 GMT-0800 (PST)', 46 'Mon Dec 01 2014 23:00:30 GMT-0800 (PST)', 47 'Mon Dec 01 2014 23:00:42 GMT-0800 (PST)', 48 'Mon Dec 01 2014 23:00:54 GMT-0800 (PST)' 49 ] 50 */ 51 ``` 52 53 The `start` and `stop` times may be either [`Date`][mdn-date-object] objects, date strings, Unix timestamps, or JavaScript timestamps. 54 55 ```javascript 56 // JavaScript timestamps: 57 var end = 1417503654973; 58 var start = new Date( end - 60000 ); 59 60 var arr = datespace( start, end, 6 ); 61 /* returns [ 62 'Mon Dec 01 2014 22:59:54 GMT-0800 (PST)', 63 'Mon Dec 01 2014 23:00:06 GMT-0800 (PST)', 64 'Mon Dec 01 2014 23:00:18 GMT-0800 (PST)', 65 'Mon Dec 01 2014 23:00:30 GMT-0800 (PST)', 66 'Mon Dec 01 2014 23:00:42 GMT-0800 (PST)', 67 'Mon Dec 01 2014 23:00:54 GMT-0800 (PST)' 68 ] 69 */ 70 71 // Unix timestamps: 72 end = 1417503655; 73 start = end - 60; 74 75 arr = datespace( start, end, 6 ); 76 /* returns [ 77 'Mon Dec 01 2014 22:59:54 GMT-0800 (PST)', 78 'Mon Dec 01 2014 23:00:06 GMT-0800 (PST)', 79 'Mon Dec 01 2014 23:00:18 GMT-0800 (PST)', 80 'Mon Dec 01 2014 23:00:30 GMT-0800 (PST)', 81 'Mon Dec 01 2014 23:00:42 GMT-0800 (PST)', 82 'Mon Dec 01 2014 23:00:54 GMT-0800 (PST)' 83 ] 84 */ 85 ``` 86 87 The output `array` is guaranteed to include the `start` and `end` times. Beware, however, that values between the `start` and `end` are subject to rounding errors. For example, 88 89 ```javascript 90 var arr = datespace( 1417503655000, 1417503655001, 3 ); 91 // returns [ 1417503655000, 1417503655000, 1417503655001 ] 92 ``` 93 94 where sub-millisecond values are truncated by the [`Date`][mdn-date-object] constructor. Duplicate values should only be a problem when the interval separating consecutive times is less than a millisecond. As the interval separating consecutive dates goes to infinity, the quantization noise introduced by millisecond resolution is negligible. 95 96 By default, fractional timestamps are floored. To specify that timestamps always be rounded up or to the nearest millisecond **when converted to [`Date`][mdn-date-object] objects**, set the `round` option (default: `floor`). 97 98 ```javascript 99 // Equivalent of Math.ceil(): 100 var arr = datespace( 1417503655000, 1417503655001, 3, { 101 'round': 'ceil' 102 }); 103 // returns [ 1417503655000, 1417503655001, 1417503655001 ] 104 105 // Equivalent of Math.round(): 106 arr = datespace( 1417503655000, 1417503655001, 3, { 107 'round': 'round' 108 }); 109 // returns [ 1417503655000, 1417503655001, 1417503655001 ] 110 ``` 111 112 </section> 113 114 <!-- /.usage --> 115 116 <section class="notes"> 117 118 </section> 119 120 <!-- /.notes --> 121 122 <section class="examples"> 123 124 ## Examples 125 126 ```javascript 127 var datespace = require( '@stdlib/array/datespace' ); 128 var start; 129 var arr; 130 var end; 131 132 end = '2014-12-02T07:00:54.973Z'; 133 start = new Date( end ) - 100000; 134 135 // Default behavior: 136 arr = datespace( start, end ); 137 console.log( arr.join( '\n' ) ); 138 139 // Specify length: 140 arr = datespace( start, end, 10 ); 141 console.log( arr.join( '\n' ) ); 142 143 arr = datespace( start, end, 11 ); 144 console.log( arr.join( '\n' ) ); 145 146 // Create an array with decremented values: 147 arr = datespace( end, start, 11 ); 148 console.log( arr.join( '\n' ) ); 149 ``` 150 151 </section> 152 153 <!-- /.examples --> 154 155 <section class="links"> 156 157 [mdn-date-object]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date 158 159 </section> 160 161 <!-- /.links -->