I just ran across this great function I had saved to Evernote some time ago.
If you want to do date formatting in JavaScript, this might be one very good way.
This is a strong candidate to add to your JavaScript Library.
Although JavaScript provides a bunch of methods for getting and setting parts of a date object, it lacks a simple way to format dates and times according to a user-specified mask.
There are a few scripts out there which provide this functionality, but I've never seen one that worked well for me… Most are needlessly bulky or slow, tie in unrelated functionality . . .
When choosing which special mask characters to use for my JavaScript date formatter, I looked at PHP's date function and ColdFusion's discrete dateFormat and timeFormat functions. . . .
With my date formatter, I've tried to take the best features from both, and add some sugar of my own. It did end up a lot like the ColdFusion implementation though, since I've primarily used CF's mask syntax.
I don’t use them much myself (given the transience, lightness and shareability of scripts, I would normally rather avoid dependencies and include functions in them, rather than risk scripts getting broken by subsequent library updates), but one application is suggested by the repertoire of basic JS functions in iOS Drafts:
To reuse scripts in FoldingText, 1Writer, Script Editor etc, start with the following one-line header,
var drafts = this;
and use the drafts. prefix with the commands below
Note: the ranges expected & returned are [intStart, intLength]
alert()
drafts.getClipboard()
drafts.setClipboard()
drafts.markdown()
drafts.encodeHTMLEntities()
drafts.decodeHTMLEntities()
drafts.getText()
drafts.getSelectedText()
drafts.getSelectedRange()
drafts.getTextInRange()
drafts.getSelectedLineRange()
drafts.setText()
drafts.setSelectedText()
drafts.setTextInRange()
drafts.setSelectedRange()
Writing a library of functions with the same names and behaviours as these can allow one to write code once, and then use it in different JS-enabled editors and on different platforms.
(Did do this at one point, and have found it useful, but haven’t maintained it very actively).
I have been testing it, and it works great UNTIL you wrap your entire script in a function, like:
'use strict';
(function run() { // this will auto-run when script is executed
var drafts = this; // no longer refers to the script file.
// -- so this doesn't work:
drafts.getLibInfo("Test");
function getLibInfo(pMsg) {
var app = Application.currentApplication()
app.includeStandardAdditions = true
app.displayDialog("Function in MAIN script");
return ;
}
} // END of function run()
)();
####Gives this error:
###Take out the function run() and it works
'use strict';
//(function run() { // this will auto-run when script is executed
var drafts = this; // NOW IT WORKS!
drafts.getLibInfo("Test");
function getLibInfo(pMsg) {
var app = Application.currentApplication()
app.includeStandardAdditions = true
app.displayDialog("Function in MAIN script");
return ;
}
//} // END of function run()
//)();
I tried putting just the var drafts = this outside the function run, but then it doesn't find the function getLibInfo.
Any ideas on how to have both a top-level wrapping function, and use the approach of drafts = this ?
Mea culpa – wasn’t being clear enough, and pasting things too fast and loose
The drafts = this line is only needed for iOS drafts itself, which has these functions, without the drafts prefix, in the global namespace.
In other editors, the trick is to make each function a member of a drafts object.
There is very rough and preliminary code here,
which you would need to test and polish up (I was clumsily learning JS at the time, and I think there are probably a few unfixed edge cases, and perhaps some general style issues, in each implementation)