AndrewGSW
09-17-2012, 06:39 PM
While I'm on a roll ;) someone may also be interested in formatting dates for display - using format codes such as 'dd mmm yyyy'.
<!DOCTYPE html>
<html>
<head>
<title>Formatting Dates</title>
<script type="text/javascript">
var monthNames = ["January", "February", "March", "April", "May", "June", "July",
"August", "September", "October", "November", "December"];
var dayNames = [ "Sunday", "Monday", "Tueday", "Wednesday", "Thursday",
"Friday", "Saturday" ];
var shortMths = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug",
"Sep", "Oct", "Nov", "Dec"];
var shortDays = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
function formatDate(sFormat, aDate) {
// Example use: FormatDate('ddd, dd mmm YYYY hh:nn'); - use 'nn' for mins
// If aDate is not supplied the current date is assumed.
var currDT = aDate || new Date();
sFormat = sFormat.toUpperCase();
function replaceDTChars(match) {
switch (match) {
case 'D': return currDT.getDate();
case 'DD':
var currDate = currDT.getDate();
return ( currDate < 10 ? "0" : "" ) + currDate;
case 'DDD': return dayNames[currDT.getDay()].substring(0,3);
case 'DDDD': return dayNames[currDT.getDay()];
case 'M': return currDT.getMonth()+1;
case 'MM':
var currMonth = currDT.getMonth()+1;
return ( currMonth < 10 ? "0" : "" ) + currMonth;
case 'MMM': return monthNames[currDT.getMonth()].substring(0,3);
case 'MMMM': return monthNames[currDT.getMonth()];
case 'Y':
case 'YY': return ('' + currDT.getFullYear()).substring(2);
case 'YYY':
case 'YYYY': return '' + currDT.getFullYear();
case 'H': return currDT.getHours();
case 'HH':
var currHr = currDT.getHours();
return ( currHr < 10 ? "0" : "" ) + currHr;
case 'N': return currDT.getMinutes();
case 'NN':
var currMin = currDT.getMinutes();
return ( currMin < 10 ? "0" : "" ) + currMin;
case 'S': return currDT.getSeconds();
case 'SS':
var currSecs = currDT.getSeconds();
return ( currSecs < 10 ? "0" : "" ) + currSecs;
default: return match;
}
}
return sFormat.replace(/D{1,4}|M{1,4}|Y{1,4}|H{1,2}|N{1,2}|S{1,2}/g,
function(m){return replaceDTChars(m)});
}
function formatIt() {
var thedate = document.getElementById('thedate');
var asdate = new Date(thedate.value);
var theformat = document.getElementById('theformat').value;
document.getElementById('theresult').innerHTML = formatDate(theformat, asdate);
}
</script>
</head>
<body>
<h1>Formatting Dates</h1>
<label>Enter a Date in the standard format YYYY/MMM/DD HH:MM:
<input type="text" id="thedate" name="thedate" value="">
</label><br>
<label>Enter the required format e.g. 'dd/mmm/yy':
<input type="text" id="theformat" name="theformat" value="dd/mmm/yyyy">
</label>
<button onclick="formatIt()">Format</button>
<p id="theresult">Result will appear here..</p>
</body>
</html>
It doesn't handle 12 hour clocks (am/pm) but I could leave this as an exercise for you :D.
<!DOCTYPE html>
<html>
<head>
<title>Formatting Dates</title>
<script type="text/javascript">
var monthNames = ["January", "February", "March", "April", "May", "June", "July",
"August", "September", "October", "November", "December"];
var dayNames = [ "Sunday", "Monday", "Tueday", "Wednesday", "Thursday",
"Friday", "Saturday" ];
var shortMths = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug",
"Sep", "Oct", "Nov", "Dec"];
var shortDays = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
function formatDate(sFormat, aDate) {
// Example use: FormatDate('ddd, dd mmm YYYY hh:nn'); - use 'nn' for mins
// If aDate is not supplied the current date is assumed.
var currDT = aDate || new Date();
sFormat = sFormat.toUpperCase();
function replaceDTChars(match) {
switch (match) {
case 'D': return currDT.getDate();
case 'DD':
var currDate = currDT.getDate();
return ( currDate < 10 ? "0" : "" ) + currDate;
case 'DDD': return dayNames[currDT.getDay()].substring(0,3);
case 'DDDD': return dayNames[currDT.getDay()];
case 'M': return currDT.getMonth()+1;
case 'MM':
var currMonth = currDT.getMonth()+1;
return ( currMonth < 10 ? "0" : "" ) + currMonth;
case 'MMM': return monthNames[currDT.getMonth()].substring(0,3);
case 'MMMM': return monthNames[currDT.getMonth()];
case 'Y':
case 'YY': return ('' + currDT.getFullYear()).substring(2);
case 'YYY':
case 'YYYY': return '' + currDT.getFullYear();
case 'H': return currDT.getHours();
case 'HH':
var currHr = currDT.getHours();
return ( currHr < 10 ? "0" : "" ) + currHr;
case 'N': return currDT.getMinutes();
case 'NN':
var currMin = currDT.getMinutes();
return ( currMin < 10 ? "0" : "" ) + currMin;
case 'S': return currDT.getSeconds();
case 'SS':
var currSecs = currDT.getSeconds();
return ( currSecs < 10 ? "0" : "" ) + currSecs;
default: return match;
}
}
return sFormat.replace(/D{1,4}|M{1,4}|Y{1,4}|H{1,2}|N{1,2}|S{1,2}/g,
function(m){return replaceDTChars(m)});
}
function formatIt() {
var thedate = document.getElementById('thedate');
var asdate = new Date(thedate.value);
var theformat = document.getElementById('theformat').value;
document.getElementById('theresult').innerHTML = formatDate(theformat, asdate);
}
</script>
</head>
<body>
<h1>Formatting Dates</h1>
<label>Enter a Date in the standard format YYYY/MMM/DD HH:MM:
<input type="text" id="thedate" name="thedate" value="">
</label><br>
<label>Enter the required format e.g. 'dd/mmm/yy':
<input type="text" id="theformat" name="theformat" value="dd/mmm/yyyy">
</label>
<button onclick="formatIt()">Format</button>
<p id="theresult">Result will appear here..</p>
</body>
</html>
It doesn't handle 12 hour clocks (am/pm) but I could leave this as an exercise for you :D.