6 ## dayOfTheYear(date):Number
8 How many days elapsed since begining of the year (following gregorian
13 dayOfTheYear(new Date(2013, 0, 1)); // 1
15 dayOfTheYear(new Date(2013, 11, 31)); // 364
20 ## diff(date1, date2, [unitName]):Number
22 Calculate the difference between dates (range).
24 The returned value is always positive. The default `unitName` is `"ms"`.
26 Available units: `year`, `month`, `week`, `day`, `hour`, `minute`, `second`,
29 See: [`time/convert()`](time.html#convert)
32 var d1 = new Date(2012, 4, 5);
33 var d2 = new Date(2013, 4, 8);
34 diff(d1, d2); // 31795200000
35 diff(d1, d2, 'hour'); // 8832
36 diff(d1, d2, 'week'); // 52.57142857142857
37 diff(d1, d2, 'month'); // 12.096774193548388
38 diff(d1, d2, 'year'); // 1.0082191780821919
43 ## isLeapYear(fullYear|date):Boolean
45 Checks if it's a [leap year](http://en.wikipedia.org/wiki/Leap_year) according
46 to the Gregorian calendar.
48 see: [`totalDaysInMonth()`](#totalDaysInMonth)
51 isLeapYear(2012); // true
52 isLeapYear(2013); // false
53 isLeapYear(new Date(2012, 2, 28)); // true
57 ## isSame(date1, date2[, period]):Boolean
59 Check if both dates are the "same".
61 You can pass an optional *period* used to set the comparisson precision.
63 Available periods: `year`, `month`, `week`, `day`, `hour`, `minute`, `second`.
66 var date1 = new Date(2013, 1, 3);
67 var date2 = new Date(2013, 2, 9);
68 isSame(date1, date2); // false
69 isSame(date1, date2, 'day'); // false
70 isSame(date1, date2, 'month'); // false
71 isSame(date1, date2, 'year'); // true
76 ## parseIso(str):Number
78 Parses an [ISO8601](http://en.wikipedia.org/wiki/Iso8601) date and returns the
79 number of milliseconds since January 1, 1970, 00:00:00 UTC, or `NaN` if it is
80 not a valid ISO8601 date.
82 This parses *all* ISO8601 dates, including dates without times, [ordinal
83 dates](https://en.wikipedia.org/wiki/ISO_8601#Ordinal_dates), and the compact
84 representation (omitting delimeters). The only exception is [ISO week
85 dates](https://en.wikipedia.org/wiki/ISO_week_date), which are not parsed.
87 If no time zone offset is specified, it assumes UTC time.
90 // Jan 01, 1970 00:00 GMT
91 parseIso('1970-01-01T00:00:00') // 0
92 parseIso('1970-001') // 0
93 parseIso('1970-01-01') // 0
94 parseIso('19700101T000000.00') // 0
95 parseIso('1970-01-01T02:00+02:00') // 0
97 // Jan 02, 2000 20:10 GMT+04:00
98 parseIso('2000-01-02T20:10+04:00') // 946829400000
102 ## quarter(date):Number
104 Get a number between 1 to 4 that represents the quarter of the year.
107 quarter(new Date(2013, 1, 19)); // 1
108 quarter(new Date(2013, 4, 12)); // 2
109 quarter(new Date(2013, 7, 25)); // 3
110 quarter(new Date(2013, 10, 8)); // 4
114 ## startOf(date, period):Date
116 Get a new Date at the start of the period.
118 Available periods: `year`, `month`, `week`, `day`, `hour`, `minute`, `second`.
121 // Apr 05 2013 11:27:43
122 var date = new Date(2013, 3, 5, 11, 27, 43, 123);
123 startOf(date, 'year'); // Jan 01 2013 00:00:00
124 startOf(date, 'month'); // Apr 01 2013 00:00:00
125 startOf(date, 'day'); // Apr 05 2013 00:00:00
126 startOf(date, 'hour'); // Apr 05 2013 11:00:00
131 ## strftime(date, format, [l10n]):String
133 Format date based on strftime format.
138 <dt>%a</dt><dd> locale's abbreviated weekday name.</dd>
139 <dt>%A</dt><dd> locale's full weekday name.</dd>
140 <dt>%b</dt><dd> locale's abbreviated month name.</dd>
141 <dt>%B</dt><dd> locale's full month name.</dd>
142 <dt>%c</dt><dd> locale's appropriate date and time representation.</dd>
143 <dt>%C</dt><dd> century number (the year divided by 100 and truncated
144 to an integer) as a decimal number [00..99].</dd>
145 <dt>%d</dt><dd> day of the month as a decimal number [01..31].</dd>
146 <dt>%D</dt><dd>same as %m/%d/%y.</dd>
147 <dt>%e</dt><dd> day of the month as a decimal number [1..31];
148 a single digit is preceded by a space.</dd>
149 <dt>%F</dt><dd>The ISO 8601 date format (%Y-%m-%d)</dd>
150 <dt>%h</dt><dd>same as %b.</dd>
151 <dt>%H</dt><dd> hour (24-hour clock) as a decimal number [00..23].</dd>
152 <dt>%I</dt><dd> hour (12-hour clock) as a decimal number [01..12].</dd>
153 <dt>%j</dt><dd> day of the year as a decimal number [001..366].</dd>
154 <dt>%l</dt><dd> hour (12-hour clock) as a decimal number (range 1 to 12); single digits are preceded by a blank</dd>
155 <dt>%L</dt><dd> zero-padded milliseconds [000..999]</dd>
156 <dt>%m</dt><dd> month as a decimal number [01..12].</dd>
157 <dt>%M</dt><dd> minute as a decimal number [00..59].</dd>
158 <dt>%n</dt><dd> newline character.</dd>
159 <dt>%p</dt><dd> locale's equivalent of either "am" or "pm"</dd>
160 <dt>%P</dt><dd> locale's equivalent of either "AM" or "PM"</dd>
161 <dt>%r</dt><dd> time in a.m. and
162 p.m. notation; in the POSIX locale this is equivalent to %I:%M:%S %p.</dd>
163 <dt>%R</dt><dd> time in 24 hour notation (%H:%M).</dd>
164 <dt>%s</dt><dd> seconds since Epoch (1970-01-01 00:00:00 UTC)</dd>
165 <dt>%S</dt><dd> second as a decimal number [00..60].</dd>
166 <dt>%t</dt><dd> tab character.</dd>
167 <dt>%T</dt><dd> time (%H:%M:%S).</dd>
168 <dt>%u</dt><dd> weekday as a decimal number [1..7], with 1 representing
170 <dt>%U</dt><dd> week number of the year (Sunday as the first day of
171 the week) as a decimal number [00..53].</dd>
172 <del><dt>%V</dt><dd> week number of the year (Monday as the first day of the
173 week) as a decimal number [01..53]. If the week containing 1 January has
174 four or more days in the new year, then it is considered week 1. Otherwise,
175 it is the last week of the previous year, and the next week is week 1.</dd></del>
176 <dt>%w</dt><dd> weekday as a decimal number [0..6], with 0 representing
178 <dt>%W</dt><dd> week number of the year (Monday as the first day of
179 the week) as a decimal number [00..53]. All days in a new year preceding
180 the first Monday are considered to be in week 0.</dd>
181 <dt>%x</dt><dd> locale's appropriate date representation.</dd>
182 <dt>%X</dt><dd> locale's appropriate time representation.</dd>
183 <dt>%y</dt><dd> year without century as a decimal number [00..99].</dd>
184 <dt>%Y</dt><dd> year with century as a decimal number.</dd>
185 <dt>%Z</dt><dd> timezone name or abbreviation, or by no bytes
186 if no timezone information exists.</dd>
187 <dt>%%</dt><dd>is replaced by %.</dd>
191 var date = new Date(2013, 3, 8, 9, 2, 4);
192 strftime(date, '%Y-%m-%d'); // "2013-04-08"
193 strftime(date, '%R'); // "09:02"
194 strftime(date, '%Y-%m-%dT%H:%M:%S%z'); // "2013-04-08T09:02:04+0000"
197 You can also set a custom locale:
200 var ptBr = require('mout/date/i18n/pt-BR');
201 strftime(date, '%a, %d %b', ptBr); // 'Seg, 08 Abr'
202 strftime(date, '%A, %d %B', ptBr); // 'Segunda, 08 Abril'
208 require('mout/date/i18n_').set( customLocaleData );
211 See [date/i18n](https://github.com/mout/mout/tree/master/src/date/i18n)
212 for localization examples.
216 ## timezoneAbbr(date):String
218 Return timezone abbreviation or similar data.
220 The result will vary based on the OS/browser since some environments doesn't
221 provide enough info about the current locale.
225 timezoneAbbr(new Date()); // "-0500"
227 timezoneAbbr(new Date()); // "EST"
232 ## timezoneOffset(date):String
234 Return time zone as hour and minute offset from UTC (e.g. +0900).
236 It's important to note that JavaScript Date object will use the system locale
237 info to determinate the [timezone
238 offset](https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Date/getTimezoneOffset)
239 and that daylight saving time affects the result.
242 // if system locale is EST
243 timezoneOffset(new Date()); // -0500
248 ## totalDaysInMonth(fullYear, monthIndex):Number
250 Returns the amount of days in the month taking into consideration leap years
251 (following Gregorian calendar).
253 see: [`isLeapYear()`](#isLeapYear)
256 totalDaysInMonth(2008, 1); // 29 (leap year)
257 totalDaysInMonth(2009, 1); // 28
259 // you can also pass a Date object as single argument
260 totalDaysInMonth( new Date(2013, 0, 1) ); // 31
264 ## totalDaysInYear(fullYear):Number
266 Returns the amount of days in the year taking into consideration leap years
267 (following Gregorian calendar).
269 see: [`isLeapYear()`](#isLeapYear), [`totalDaysInMonth()`](#totalDaysInMonth)
272 totalDaysInYear(2008); // 366 (leap year)
273 totalDaysInYear(2009); // 365
275 // you can also pass a Date object as single argument
276 totalDaysInYear( new Date(2013, 0, 1) ); // 365
281 ## weekOfTheYear(date, [firstDayOfWeek]):Number
283 Returns how many weeks elapsed since start of the year (`0..53`).
285 `firstDayOfWeek` can be `0` (Sunday) or `1` (Monday). By default weeks start at
288 It will return `0` if `date` is before the first `firstDayOfWeek` of the year.
292 weekOfTheYear( new Date(2013,0,1) ); // 0
294 weekOfTheYear( new Date(2013,0,9) ); // 1
296 weekOfTheYear( new Date(2012,0,1) ); // 1
298 weekOfTheYear( new Date(2012,0,9) ); // 2
303 -------------------------------------------------------------------------------
305 For more usage examples check specs inside `/tests` folder. Unit tests are the
306 best documentation you can get...