If you do a lot of web programming, it can save you a lot of time and effort to have a library of JavaScript functions. This article presents some useful, reusable client side code, for calendars and more.
Useful Web Widgets - The Function in Depth (Page 3 of 5 )
The function accepts three arguments. The first argument is the format string, the second is the character to use as a separator between each value and the third is a boolean value to determine if we want to display the time.
function getFormattedDate(format,separator,time) { //get a date object var curr_dt = new Date(); //make sure the format string is lower case format = format.toLowerCase(); //make an array of the format var tArr = format.split(","); //create a default if the separator is not specified if(separator == "") { separator = " "; } //a default format object var formatObj = new Object(); formatObj.monthd = true; formatObj.monthLong = true; formatObj.dayd = true; formatObj.dayLong = true; formatObj.yearLong = true;
The format string can be optional, where you would simply pass an empty string and use the default format which is "dl,dl,l" but could be anything you like. if you wanted to make troubleshooting the function easier, you could certainly provide some validation for the format string using regular expressions at this point. If we aren't using the default, then we need to change default values before producing any date values.
Now it's time to start generating the date string. First up is the month. We add leading zeros if the format is "long" or simply use the month number as is + 1 (because the output of getMonth() is 0-11). When the month format is text we can use the number derived from getMonth as an index for an array that contains the text versions.
Next is the day which is treated in a similar fashion. We also need to set up a text version of the day to be placed at the beginning of the date string when the day format is text.
//format day var ddy = ""; var dy = ""; if(formatObj.dayd) { if(formatObj.dayLong) { ddy = curr_dt.getDate(); if(ddy < 10) { ddy = " 0" + ddy.toString() + " "; } } else { ddy = " " + curr_dt.getDate() + " "; } } else { if(formatObj.dayLong) { var dayArr = new Array ("Monday","Tuesday","Wednesday","Thursday", "Friday","Saturday","Sunday"); ddy = " " + curr_dt.getDate() + " "; } else { var dayArr = new Array("Mon","Tues","Wed","Thurs","Fri","Sat","Sun"); ddy = " " + curr_dt.getDate() + " "; } var daytmp = curr_dt.getDate() - 1; dy = dayArr[daytmp] }
The next task is to get the year. This one is much simpler.
//format year var yr = ""; if(formatObj.yearLong) { yr = curr_dt.getFullYear().toString(); } else { yr = curr_dt.getFullYear().toString().substr(2,2); }
The last part of the date string to generate is the time. This takes the most processing. This section could be modified to support more display formats in the same way the date does.
if(time) { var tm = ""; var hours = curr_dt.getHours(); var minutes = curr_dt.getMinutes(); //if minutes are 1-9 we need a leading zero if(minutes < 10) { minutes = "0" + minutes; } //new day starts unless you are a programmer if(hours == 0) { tm = 12 + ":" + minutes + " AM"; } //the noon hour is here so tack on a PM else if(hours == 12) { tm = hours + ":" + minutes + " PM"; } //we are past noon. subtract 12 to start at 1 and go up. else if(hours > 12 ) { tm = (hours - 12) + ":" + minutes + " PM"; } else { tm = hours + ":" + minutes + " AM"; } }