e140399189dda6cf39b8b53c7f4bbb0901c84a15
[platform/framework/web/crosswalk-tizen.git] /
1 # date #
2
3 Date utilities.
4
5
6 ## dayOfTheYear(date):Number
7
8 How many days elapsed since begining of the year (following gregorian
9 calendar).
10
11 ```js
12 // Jan 1st
13 dayOfTheYear(new Date(2013, 0, 1)); // 1
14 // Dec 31th
15 dayOfTheYear(new Date(2013, 11, 31)); // 364
16 ```
17
18
19
20 ## diff(date1, date2, [unitName]):Number
21
22 Calculate the difference between dates (range).
23
24 The returned value is always positive. The default `unitName` is `"ms"`.
25
26 Available units: `year`, `month`, `week`, `day`, `hour`, `minute`, `second`,
27 `millisecond`.
28
29 See: [`time/convert()`](time.html#convert)
30
31 ```js
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
39 ```
40
41
42
43 ## isLeapYear(fullYear|date):Boolean
44
45 Checks if it's a [leap year](http://en.wikipedia.org/wiki/Leap_year) according
46 to the Gregorian calendar.
47
48 see: [`totalDaysInMonth()`](#totalDaysInMonth)
49
50 ```js
51 isLeapYear(2012); // true
52 isLeapYear(2013); // false
53 isLeapYear(new Date(2012, 2, 28)); // true
54 ```
55
56
57 ## isSame(date1, date2[, period]):Boolean
58
59 Check if both dates are the "same".
60
61 You can pass an optional *period* used to set the comparisson precision.
62
63 Available periods: `year`, `month`, `week`, `day`, `hour`, `minute`, `second`.
64
65 ```js
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
72 ```
73
74
75
76 ## parseIso(str):Number
77
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.
81
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.
86
87 If no time zone offset is specified, it assumes UTC time.
88
89 ```js
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
96
97 // Jan 02, 2000 20:10 GMT+04:00
98 parseIso('2000-01-02T20:10+04:00') // 946829400000
99 ```
100
101
102 ## quarter(date):Number
103
104 Get a number between 1 to 4 that represents the quarter of the year.
105
106 ```js
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
111 ```
112
113
114 ## startOf(date, period):Date
115
116 Get a new Date at the start of the period.
117
118 Available periods: `year`, `month`, `week`, `day`, `hour`, `minute`, `second`.
119
120 ```js
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
127 ```
128
129
130
131 ## strftime(date, format, [l10n]):String
132
133 Format date based on strftime format.
134
135 Replaced tokens:
136
137 <dl>
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
169 Monday.</dd>
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
177 Sunday.</dd>
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>
188 </dl>
189
190 ```js
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"
195 ```
196
197 You can also set a custom locale:
198
199 ```js
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'
203 ```
204
205 To set it globally:
206
207 ```js
208 require('mout/date/i18n_').set( customLocaleData );
209 ```
210
211 See [date/i18n](https://github.com/mout/mout/tree/master/src/date/i18n)
212 for localization examples.
213
214
215
216 ## timezoneAbbr(date):String
217
218 Return timezone abbreviation or similar data.
219
220 The result will vary based on the OS/browser since some environments doesn't
221 provide enough info about the current locale.
222
223 ```js
224 // IE 7-8
225 timezoneAbbr(new Date()); // "-0500"
226 // Chrome, FF, V8
227 timezoneAbbr(new Date()); // "EST"
228 ```
229
230
231
232 ## timezoneOffset(date):String
233
234 Return time zone as hour and minute offset from UTC (e.g. +0900).
235
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.
240
241 ```js
242 // if system locale is EST
243 timezoneOffset(new Date()); // -0500
244 ```
245
246
247
248 ## totalDaysInMonth(fullYear, monthIndex):Number
249
250 Returns the amount of days in the month taking into consideration leap years
251 (following Gregorian calendar).
252
253 see: [`isLeapYear()`](#isLeapYear)
254
255 ```js
256 totalDaysInMonth(2008, 1); // 29 (leap year)
257 totalDaysInMonth(2009, 1); // 28
258
259 // you can also pass a Date object as single argument
260 totalDaysInMonth( new Date(2013, 0, 1) ); // 31
261 ```
262
263
264 ## totalDaysInYear(fullYear):Number
265
266 Returns the amount of days in the year taking into consideration leap years
267 (following Gregorian calendar).
268
269 see: [`isLeapYear()`](#isLeapYear), [`totalDaysInMonth()`](#totalDaysInMonth)
270
271 ```js
272 totalDaysInYear(2008); // 366 (leap year)
273 totalDaysInYear(2009); // 365
274
275 // you can also pass a Date object as single argument
276 totalDaysInYear( new Date(2013, 0, 1) ); // 365
277 ```
278
279
280
281 ## weekOfTheYear(date, [firstDayOfWeek]):Number
282
283 Returns how many weeks elapsed since start of the year (`0..53`).
284
285 `firstDayOfWeek` can be `0` (Sunday) or `1` (Monday). By default weeks start at
286 Sunday.
287
288 It will return `0` if `date` is before the first `firstDayOfWeek` of the year.
289
290 ```js
291 // Tue Jan 01 2013
292 weekOfTheYear( new Date(2013,0,1) ); // 0
293 // Wed Jan 09 2013
294 weekOfTheYear( new Date(2013,0,9) ); // 1
295 // Sun Jan 01 2012
296 weekOfTheYear( new Date(2012,0,1) ); // 1
297 // Mon Jan 09 2012
298 weekOfTheYear( new Date(2012,0,9) ); // 2
299 ```
300
301
302
303 -------------------------------------------------------------------------------
304
305 For more usage examples check specs inside `/tests` folder. Unit tests are the
306 best documentation you can get...
307