From 2e2275c262ca24d436c79e2f202bb25458400661 Mon Sep 17 00:00:00 2001 From: Gordon Williams Date: Tue, 26 Apr 2022 10:27:23 +0100 Subject: [PATCH] Add date_utils lib (https://github.com/espruino/BangleApps/pull/1753) --- modules/date_utils.js | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 modules/date_utils.js diff --git a/modules/date_utils.js b/modules/date_utils.js new file mode 100644 index 000000000..da0ed24d9 --- /dev/null +++ b/modules/date_utils.js @@ -0,0 +1,39 @@ +/* Utility functions that use the 'locale' module so can produce text +in the currently selected language. */ + +/** Return the day of the week (0=Sunday) + short==0/undefined -> "Sunday" + short==1 -> "Sun" +*/ +exports.getDOW = (dow, short) => require("locale").dow({getDay:()=>dow},short); + +/** Return the month (1=January) + short==0/undefined -> "January" + short==1 -> "Jan" +*/ +exports.getMonth = (month, short) => require("locale").month({getMonth:()=>month-1},short); + +/** Return all 7 days of the week as an array ["Sunday","Monday",...]. + short==0/undefined -> ["Sunday",... + short==1 -> ["Sun",... + short==2 -> ["S",... +*/ +exports.getDOWs = (short) => { + var locale = require("locale"); + var days = []; + for (var i=0;i<7;i++) + days.push(locale.dow({getDay:()=>i},short).slice(0,(short==2)?1:100)); + return days; +} + +/** Return all 12 months as an array ["January","February",...] + short==0/undefined -> ["January",... + short==1 -> ["Jan",... +*/ +exports.getMonths = (short) => { + var locale = require("locale"); + var months = []; + for (var i=0;i<12;i++) + months.push(locale.month({getMonth:()=>i},short)); + return months; +}