Parameters are unfortunately positional... the first for .format() method - instance or class - is always the date (or falsy for the default, which is now), and the second is the formatting pattern (or falsy for the default, which is the default in the module or the formatting pattern passed on construction). falsy values - values that evaluate to false (see MDN Glossary) - will 'provoke' the default. Therefore, for the last example: xxy.format(0,'MM/dd/yy'); is the shortest form with an instance.
Adding... some more examples based on the description of items 7. through 9. of Notes of previous post.
Since I know that not all users are inclined to use objects (constructed from classes), I provided also the class (or static) method usage, and a plain function usage.
For the class (or static) method usage, you assign the module to a variable: var DF = require("DateFormatter"); and do formatting with DF.format();, just the same way as with objects. With the class (or static) method usage you can have only one default format: the class default, which of course, you can change with DF.updateDefaults({pattern:"yyyy-MM-dd"}); (the formatting pattern is just an example).
For the functional usage, you assign the module to a variable: var fd = require("DateFormatter"); and do formatting with fd(parm1,parm2);. Note that it is formatdate() versus DF for DateFormatter class and df for instance of DateFormatter class. With the functional usage, you always have to provide two (2) parameters, or in other words, always two (2) parameters have to be present. The javascript functions knows based on the number of parms whether to behave as plain function or as constructor. parm1 one is either a date or falsy, and parm2 is a formatting pattern or falsy.
Btw, if you always provide the format, you need only one instance of formatter. If you always use the same format or have a prevailing format, create a formatter with that format: var df = new (require("DateFormatter"))("MM/dd/yy");, and use it then anywhere in the code.
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.
Some closing comments:
Parameters are unfortunately positional... the first for
.format()
method - instance or class - is always the date (or falsy for the default, which is now), and the second is the formatting pattern (or falsy for the default, which is the default in the module or the formatting pattern passed on construction). falsy values - values that evaluate to false (see MDN Glossary) - will 'provoke' the default. Therefore, for the last example:xxy.format(0,'MM/dd/yy');
is the shortest form with an instance.Adding... some more examples based on the description of items 7. through 9. of Notes of previous post.
Since I know that not all users are inclined to use objects (constructed from classes), I provided also the class (or static) method usage, and a plain function usage.
For the class (or static) method usage, you assign the module to a variable:
var DF = require("DateFormatter");
and do formatting withDF.format();
, just the same way as with objects. With the class (or static) method usage you can have only one default format: the class default, which of course, you can change withDF.updateDefaults({pattern:"yyyy-MM-dd"});
(the formatting pattern is just an example).For the functional usage, you assign the module to a variable:
var fd = require("DateFormatter");
and do formatting withfd(parm1,parm2);
. Note that it isf
ormatd
ate()
versus DF for DateFormatter class and df for instance of DateFormatter class. With the functional usage, you always have to provide two (2) parameters, or in other words, always two (2) parameters have to be present. The javascript functions knows based on the number of parms whether to behave as plain function or as constructor. parm1 one is either a date or falsy, and parm2 is a formatting pattern or falsy.Btw, if you always provide the format, you need only one instance of formatter. If you always use the same format or have a prevailing format, create a formatter with that format:
var df = new (require("DateFormatter"))("MM/dd/yy");
, and use it then anywhere in the code.