1
edit
Changes
New page: ==Date Format Parser== This js code parses a terminal date string into a real date and time. <pre> // This file is a parser for terminal "date" command // format strings. Likely, you wil...
==Date Format Parser==
This js code parses a terminal date string into a real date and time.
<pre>
// This file is a parser for terminal "date" command
// format strings. Likely, you will want to use only
// timeParse(date, fmtstring)
// Copyright 2009 John Ford
// MPL-GPL-LGPL
/* This function pads a signle digit to a leading 0
as a string */
function pad(number){
if(number <= 9){
return "0" + "" + number;
} else {
return number;
}
}
/* Convert a specifer letter into a value based on date */
function specifier(date, letter){
switch(letter){
case 'Y':
return "" + (1900 + date.getYear());
case 'm':
//Whole numbering used for month
return pad(date.getMonth() + 1);
case 'd':
return pad(date.getDate());
case 'H':
return pad(date.getHours());
case 'M':
return pad(date.getMinutes());
case 'S':
return pad(date.getSeconds());
case 's':
return date.valueOf();
default:
return letter;
}
}
/* This will parse date format strings to give valid
output pased on POSIX date command when given a format
and a date object. Returns string */
function timeParse(date, fmt){
var output ="";
var input = "" + fmt;
input = input.replace(/\+/, "");
input = input.replace(/%*/g, "");
for each (letter in input){
if (letter == '%'){
continue;
} else {
output += specifier(date, "" + letter);
}
}
return output;
}
</pre>