Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
<head>
<script type="text/javascript">
function getDayName(index,long,firstDay) {
// returns name of day
// index = day code 0 to 6 : REQUIRED
// long = true: return long name (Sunday): false = return short (Sun)
// firstDay = first day of the week 0 = Sunday (default); 1 - 5 = Monday; 6 = Saturday
if (firstDay == undefined) {
firstDay = 0;
}
if (long == undefined) {
long = true;
}
var tmp = ""
var Name = new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");
var shortName = new Array("Sun","Mon","Tues","Wed","Thurs","Fri","Sat");
if (firstDay > 0 && firstDay < 6) {
tmp = Name.shift();
Name.push(tmp);
tmp = shortName.shift();
shortName.push(tmp);
} else if (firstDay == 6) {
tmp = Name.pop();
Name.unshift(tmp);
tmp = shortName.pop();
shortName.unshift(tmp);
}
if (long) {
return Name[index];
} else {
return shortName[index];
}
}
function addDateSuffix(day) {
if (day > 0 && day < 32) {
if (day == 1 | day == 21 | day == 31) {
return day.toString() + "st";
} else if (day == 2 | day == 22) {
return day.toString() + "nd";
} else if (day == 3 | day == 23) {
return day.toString() + "rd";
} else {
return day.toString() + "th";
}
} else {
return "";
}
}
function getMonthName(index,long) {
if (long == undefined) {
long = true;
}
var Name=new Array("January","February","March","April","May","June","July","August","September","October","November","December");
var shortName=new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
if (long) {
return Name[index];
} else {
return shortName[index];
}
}
function dateFormat(formatString,dateValue) {
var retVal = formatString;
var date = new Date(dateValue);
var fmt = formatString;
var day = date.getDay();
var d = date.getDate().toString();
var m = (date.getMonth()+1).toString();
if (fmt.indexOf("YYYY") > -1 | fmt.indexOf("yyyy") > -1 ) {
retVal = retVal.replace(/YYYY/i,date.getFullYear().toString());
} else if (fmt.indexOf("YY") > -1 | fmt.indexOf("yy") > -1) {
retVal = retVal.replace(/yy/i,date.getFullYear().toString());
}
if (fmt.indexOf("DD") > -1) {
retVal = retVal.replace(/DD/,getDayName(date.getDay()) + " " + addDateSuffix(date.getDate()));
} else if (fmt.indexOf("D") > -1) {
retVal = retVal.replace(/D\b/,getDayName(date.getDay(),false) + " " + addDateSuffix(date.getDate()));
} else if (fmt.indexOf("dd") > -1) {
if (d.length == 1) {
d = "0" + d;
}
retVal = retVal.replace(/dd/,d);
} else if (fmt.indexOf("d") > -1) {
retVal = retVal.replace(/d\b/,d);
}
if (fmt.indexOf("MM") > -1) {
retVal = retVal.replace(/MM/,getMonthName(date.getMonth()) + " ") ;
} else if (fmt.indexOf("M") > -1) {
retVal = retVal.replace(/M\b/,getMonthName(date.getMonth(),false) + " " );
} else if (fmt.indexOf("mm") > -1) {
if (m.length == 1) {
m = "0" + m;
}
retVal = retVal.replace(/mm/,m);
} else if (fmt.indexOf("m") > -1) {
retVal = retVal.replace(/m/,m);
}
return retVal;
}
</script>
</head>
<h2 class="italic">Javascript dateFormat Function</h2>
<script type="text/javascript">
document.write(dateFormat("DD MM YYYY",Date()));
document.write("<br>");
document.write(dateFormat("dd MM yy",Date()));
document.write("<br>");
document.write(dateFormat("d/m/yy",Date()));
document.write("<br>");
document.write(dateFormat("dd mm yy",Date()));
document.write("<br>");
document.write(dateFormat("D M yy",Date()));
document.write("<br>");
document.write(getDayName(2,6,true));
document.write("<br>");
document.write(addDateSuffix(31));
document.write("<br>");
</script>