function log(s) { if (typeof window !== "undefined") { var n = document.getElementById("console");
n.innerHTML = n.innerHTML + "\n" + s;
} else { console.log(s); } }
function l(pattern, date, assertValue) { // for demo convenience
var formatter = new DateFormatter(pattern);
var formatted = formatter.format(date);
var ok = (assertValue) ? ((formatted === assertValue)
? ": ----} ok" : ": ----> *** NOK ***") : "";
log(formatted + " {---- " + ((pattern) ? '"' + pattern + '"' : "default") + ok);
}
function onInit(ts) { if (ts) setTime(ts);
log("- yMNdHhmsSxXDZ are date and time pattern chars, escapable by ^ (default)");
log("- d is a date a day ago same time; m is 75 days, 12 hours and 30 mins ago.");
var ts = new Date().getTime();
var d = new Date(ts - 24*60*60*1000); // yesterday same time
var m = new Date(ts - 75*24*60*60*1000 - 12*60*60*1000 - 30*60*1000); // 75 d 12 h 30 m ago
log("----- normal invocation format ------");
l(); // now in default format
l("M/d/y"); // now (today) in / format
l("MM/dd/yyyy"); // now (today) in fixed format for numbers
l("MM/dd/yy"); // now (today) fixed format for numbers
l("^Mont^h b^y na^me: NNN(3) N(all)"); // now (this month)
l("D, N d, yyyy"); // now (today) in week day month by name and day, full year
l("D, N d, yyyy h:mm X"); // now in week day month by name and day, full year, AM/PM time
l("D, N d, yyyy h:mm:ss X",d); // yesterday same time in...
l("D, N d, yyyy h:mm:ss X",m); // about 75 days ago in...
l("ti^me: hh:mmx"); // now (hr) in am/pm
l("d.M.y"); // now (tdday) in . format
l("dd.MM.yyyy"); // fixed format for numbers
l("dd.MM.yy"); // fixed format for numbers
l("h X Z E^spruino ti^me zone ;-)"); // now in h format and time zone
log("----- short invocation format ------");
log(DateFormatter.format() + ' {-- DateFormatter.format()'); // today in default format
log(DateFormatter.format(null,"yyyyMMddHHmmss.SSS") + ' {-- ...(null,"yyy...")'); // timestamp in sortable format
log(DateFormatter.format(m) + ' {-- DateFormatter.format(m)'); // about 75 days ago
log("----- very short invocation format ------");
log(DateFormatter(null,null) + ' {-- DateFormatter(null,null)'); // today default
log(DateFormatter(m,null) + ' {-- DateFormatter(m,null)'); // about 75 days ago
log(DateFormatter(d,"D, N d, y h:mx") + ' {-- DateFormatter(d,"D, N d, y h:mx")');
log("----- update defaults (default: d.M.y, esc: ~, ... some or all) ------");
DateFormatter.updateDefaults({ pattern: "d.M.y", esc: "~", year: "yy", zone: "MEZ-1"
, dayLen:2, days: "SoMoDiMiDoFrSa"
, monLen:9, mons: "Januar Februar Maerz April Mai Juni "
+ "Juli August SeptemberOktober November Dezember " });
log("----- normal invocation format ------");
l("~Monat bei~m ~Na~men: NNN N");
l("D, d. N yyyy"); // jetzt in Wochentag Tag. Monat Jahr
l("D, d. N yyyy H:mm"); // jetzt in Wochentag Tag. Monat Jahr und Zeit
l("D, d. N yyyy H:mm:ss",d); // gestern um die gleiche Zeit in Wochentag...
l("D, d. N yyyy H:mm:ss",m); // ungefaehr vor 75 Tagen in...
l("~Zeit: HH:mm");
l("H~h Z"); // jetzt in H format mit Zeitzone
}
onInit(1462603718.328); // Sat, May 7, 2016 6:48:38 AM
// parameter derived from browser debugger: new Date().getTime() / 1000
// rerun without parameter - in console type: onInit();
Test output:
1v85 Copyright 2016 G.Williams
>echo(0);
- yMNdHhmsSxXDZ are date and time pattern chars, escapable by ^ (default)
- d is a date a day ago same time, m is 75 days, 12 hours and 30 mins ago.
----- normal invocation format ------
5/7/2016 {---- default
5/7/2016 {---- "M/d/y"
05/07/2016 {---- "MM/dd/yyyy"
05/07/2016 {---- "MM/dd/yy"
Month by name: May(3) May(all) {---- "^Mont^h b^y na^me: NNN(3) N(all)"
Sat, May 7, 2016 {---- "D, N d, yyyy"
Sat, May 7, 2016 6:48 AM {---- "D, N d, yyyy h:mm X"
Fri, May 6, 2016 6:48:38 AM {---- "D, N d, yyyy h:mm:ss X"
Sun, Feb 21, 2016 6:18:38 PM {---- "D, N d, yyyy h:mm:ss X"
time: 06:48am {---- "ti^me: hh:mmx"
7.5.2016 {---- "d.M.y"
07.05.2016 {---- "dd.MM.yyyy"
07.05.2016 {---- "dd.MM.yy"
6 AM ESP Espruino time zone ;-) {---- "h X Z E^spruino ti^me zone ;-)"
----- short invocation format ------
5/7/2016 {-- DateFormatter.format()
20160507064838.250 {-- ...(null,"yyy...")
2/21/2016 {-- DateFormatter.format(m)
----- very short invocation format ------
5/7/2016 {-- DateFormatter(null,null)
2/21/2016 {-- DateFormatter(m,null)
Fri, May 6, 2016 6:48am {-- DateFormatter(d,"D, N d, y h:mx")
----- update defaults (default: d.M.y, esc: ~, ... some or all) ------
----- normal invocation format ------
Monat beim Namen: Mai Mai {---- "~Monat bei~m ~Na~men: NNN N"
Sa, 7. Mai 2016 {---- "D, d. N yyyy"
Sa, 7. Mai 2016 6:48 {---- "D, d. N yyyy H:mm"
Fr, 6. Mai 2016 6:48:38 {---- "D, d. N yyyy H:mm:ss"
So, 21. Februar 2016 18:18:38 {---- "D, d. N yyyy H:mm:ss"
Zeit: 06:48 {---- "~Zeit: HH:mm"
6h MEZ-1 {---- "H~h Z"
=undefined
>
Attached html can be clicked and it runs right away same code in the browser.
Placed in a module named DateFormatter and pulled in with var DF = require("DateFormatter"); it is a bit less verbose...
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.
Test code:
Test output:
Attached html can be clicked and it runs right away same code in the browser.
Placed in a module named
DateFormatter
and pulled in withvar DF = require("DateFormatter");
it is a bit less verbose...To be continued in next post...
1 Attachment