forked from espruino/BangleApps
-
Notifications
You must be signed in to change notification settings - Fork 0
/
date_utils.js
64 lines (60 loc) · 2.21 KB
/
date_utils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// module "date_utils"
//
// Utility functions that use the "locale" module so can produce
// date-related text in the currently selected language.
//
// Some functions have a "firstDayOfWeek" parameter.
// Most used values are:
// - 0/undefined --> Sunday
// - 1 --> Monday
// but you can start the week from any day if you need it.
//
// Some functions have an "abbreviated" parameter.
// It supports the following 3 values:
// - 0/undefined --> get the full value, without abbreviation (eg.: "Monday", "January", etc.)
// - 1 --> get the short value (eg.: "Mon", "Jan", etc.)
// - 2 --> get only the first char (eg.: "M", "J", etc.)
//
/**
* @param {int} i The index of the day of the week (0 = Sunday)
* @param {int} abbreviated
* @returns The localized name of the i-th day of the week
*/
exports.dow = (i, abbreviated) => {
var dow = require("locale").dow({getDay:()=>(i|0)%7}, abbreviated).slice(0, (abbreviated == 2) ? 1 : 100);
return abbreviated == 2 ? dow.toUpperCase() : dow;
}
/**
* @param {int} firstDayOfWeek 0/undefined -> Sunday,
* 1 -> Monday
* @param {int} abbreviated
* @returns All 7 days of the week (localized) as an array
*/
exports.dows = (firstDayOfWeek, abbreviated) => {
var dows = [];
var locale = require("locale");
for (var i = 0; i < 7; i++) {
dows.push(exports.dow(i + (firstDayOfWeek || 0), abbreviated))
}
return abbreviated == 2 ? dows.map(dow => dow.toUpperCase()) : dows;
};
/**
* @param {int} i The index of the month (1 = January)
* @param {int} abbreviated
* @returns The localized name of the i-th month
*/
exports.month = (i, abbreviated) => {
var month = require("locale").month({getMonth:()=>(11+(i|0))%12}, abbreviated).slice(0, (abbreviated == 2) ? 1 : 100);
return abbreviated == 2 ? month.toUpperCase() : month;
}
/**
* @param {int} abbreviated
* @returns All 12 months (localized) as an array
*/
exports.months = (abbreviated) => {
var months = [];
var locale = require("locale");
for (var i = 0; i < 12; i++)
months.push(locale.month({getMonth:()=>i}, abbreviated).slice(0, (abbreviated == 2) ? 1 : 100));
return abbreviated == 2 ? months.map(month => month.toUpperCase()) : months;
};