As much as I (still) enjoy reading thru the solution, I feel it is "too fat" for casually formatting a date (and time) on BangleJS... BUT: If an application has the subject of date(s) and time(s), then it is most likely already the smallest 'module' for it's comprehensiveness, flexibility and efficiency (w/ @MaBe 's shorter length checker/stretcher).
Here is another - cheap - Date Formatter:
// DF: BangleJS Date Formatter
let DF = // cheap Date Formatter
{ l: null // last used; for reuse pass -1 for d as date
, f: function(d,p) { // format(optDateOrMillis,optPattern) - 02/29/2020
var d=this.i(d),s=this.s(p=p||this.p),b;
return (p.split(s).map(v=>( ((b=((b=v.length)<2)?0:b)?"000":"")
+ this.x[v.charAt(0)](d) ).substr(-b)).join(s) ); }
, d: function(d,ds) { return this.t(d,"n",ds||this.ds); } // d name
, m: function(d,ms) { return this.t(d,"m",ms||this.ms); } // m name
, c: function(d) { return [DF.d(d),DF.m(-1),DF.f(-1,"d y")].join(" "); }
, t: function(d,x,ts){ return ts.split(",")[this.x[x](this.i(d))]; }
, ds: "Sun,Mon,Tue,Wed,Thu,Fri,Sat" // Monday,Tuesday ..."
, ms: ",Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec" // January,...
, p: "mm/dd/yy" // pattern
, x: {m:d=>d.getMonth()+1,d:d=>d.getDate(),y:d=>d.getFullYear()
,n:d=>d.getDay()}
, s: function(p) { var s,i=-1,l=p.length; while(++i<l) { // 1st sep
if ("mdy".indexOf(s=p.charAt(i))<0) return s; } return"/"; }
, i: function(d) { // ret Date as is, today, from millis or last used
return this.l = (isNaN(d)||d=="") ? d||new Date()
: (d<0) ? this.l : new Date(d); }
};
Usage:
02/15/20 - DF.f() today
2/15 - DF.f(-1,"m/d") from used
15.2. - DF.f("","m.d")+"." today
2020-02-15 - DF.f(null,"yyyy-mm-dd") today
Sat Feb 15, 2020 - DF.d(),DF.m(-1),DF.f(-1,"d")+",",DF.f(-1,"y")
Sat Feb 15 2020 - DF.d(),DF.m(-1),DF.f(-1,"d y")
Sat Feb 15 2020 - DF.c() - today, combined, like Date.toString()
Sat Feb 15 2020 - DF.l.toString().substr(0,15) - today from used
Sat Feb 15 2020 08:49:34 GMT-0800 (Pacific Standard Time) - DF.i(-1) - today from last used
Sat Feb 15 2020 08:49:34 GMT-0800 (Pacific Standard Time) - DF.l - today from used
Attached .html file can be directly executed to run formatter and example in browser. It can also be downloaded, modified, and played with.
The formatter can be modularized and then composed with a base module and extended with - for example - the week day and month names and combined formatting functions - and last but not lest - with the time formatting functions.
The base module with just the number formatter is very compact and looks then like this:
let DF = // cheap Date Formatter
{ l: null // last used; for reuse pass -1 for d as date
, f: function(d,p) { // format(optDateOrMillis,optPattern) - 02/29/2020
var d=this.i(d),s=this.s(p=p||this.p),b;
return (p.split(s).map(v=>( ((b=((b=v.length)<2)?0:b)?"000":"")
+ this.x[v.charAt(0)](d) ).substr(-b)).join(s) ); }
, p: "mm/dd/yy" // pattern
, x: {m:d=>d.getMonth()+1,d:d=>d.getDate(),y:d=>d.getFullYear() }
, s: function(p) { var s,i=-1,l=p.length; while(++i<l) { // 1st sep
if ("mdy".indexOf(s=p.charAt(i))<0) return s; } return"/"; }
, i: function(d) { // ret Date as is, today, from millis or last used
return this.l=(isNaN(d)||d=="")?d||new Date():(d<0)?this.l:new Date(d);}
};
Espruino is a JavaScript interpreter for low-power Microcontrollers. This site is both a support community for Espruino and a place to share what you are working on.
As much as I (still) enjoy reading thru the solution, I feel it is "too fat" for casually formatting a date (and time) on BangleJS... BUT: If an application has the subject of date(s) and time(s), then it is most likely already the smallest 'module' for it's comprehensiveness, flexibility and efficiency (w/ @MaBe 's shorter length checker/stretcher).
Here is another - cheap - Date Formatter:
Usage:
Attached .html file can be directly executed to run formatter and example in browser. It can also be downloaded, modified, and played with.
The formatter can be modularized and then composed with a base module and extended with - for example - the week day and month names and combined formatting functions - and last but not lest - with the time formatting functions.
The base module with just the number formatter is very compact and looks then like this:
1 Attachment