DateFormat.as

Go to the documentation of this file.
00001 
00004 class gugga.utils.DateFormat 
00005 {
00006         /* 
00007         Darron Schall (darron@alumni.lehigh.edu)
00008         August 27, 2003
00009         
00010         ActionScript port of ColdFusion's DateFormat function.  
00011         
00012         @param theDate - a Flash Date Object
00013         @param format - see http://livedocs.macromedia.com/coldfusion/6.1/htmldocs/functi59.htm for more information on valid formats
00014         
00015         Revision History:
00016         Rev Date                Who             Description
00017         1.0 8/27/03             darron  Initial Release
00018         
00019         --------------------------------------
00020         License For Use
00021         --------------------------------------
00022         Redistribution and use in source and binary forms, with or without modification,
00023         are permitted provided that the following conditions are met:
00024         
00025         1. Redistributions of source code must retain the above copyright notice, this
00026         list of conditions and the following disclaimer.
00027         
00028         2. Redistributions in binary form must reproduce the above copyright notice,
00029         this list of conditions and the following disclaimer in the documentation
00030         and/or other materials provided with the distribution.
00031         
00032         3. The name of the author may not be used to endorse or promote products derived
00033         from this software without specific prior written permission.
00034         
00035         THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR IMPLIED
00036         WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
00037         MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
00038         EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00039         EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
00040         OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00041         INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00042         CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
00043         IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
00044         OF SUCH DAMAGE.
00045         */
00046         public static function format(theDate:Date, format:String) : String 
00047         {
00048                 /*
00049                 if (!(theDate instanceof Date)) {
00050                         trace("Error in dateFormat - first parameter must be a date object");
00051                         return;
00052                 }
00053                 */
00054                 
00055                 var months = {};
00056                 months["Jan"] = {m:1, n:"January"};
00057                 months["Feb"] = {m:2, n:"February"};
00058                 months["Mar"] = {m:3, n:"March"};
00059                 months["Apr"] = {m:4, n:"April"};
00060                 months["May"] = {m:5, n:"May"};
00061                 months["Jun"] = {m:6, n:"June"};
00062                 months["Jul"] = {m:7, n:"July"};
00063                 months["Aug"] = {m:8, n:"August"};
00064                 months["Sep"] = {m:9, n:"September"};
00065                 months["Oct"] = {m:10, n:"October"};
00066                 months["Nov"] = {m:11, n:"November"};
00067                 months["Dec"] = {m:12, n:"December"};
00068                 var days = {};
00069                 days["Sun"] = {m:1, n:"Sunday"};
00070                 days["Mon"] = {m:1, n:"Monday"};
00071                 days["Tue"] = {m:1, n:"Tuesday"};
00072                 days["Wed"] = {m:1, n:"Wednesday"};
00073                 days["Thu"] = {m:1, n:"Thursday"};
00074                 days["Fri"] = {m:1, n:"Friday"};
00075                 days["Sat"] = {m:1, n:"Saturday"};
00076                 
00077                 var parts = theDate.toString().split(" ");
00078                 var dayOfWeek = parts[0];
00079                 var month = parts[1];
00080                 var day = parts[2];
00081                 var year = parts[5];
00082                 
00083                 var retString = "";
00084                         
00085                 // search for the mask words and replace with the appropriate mask
00086                 if (format.indexOf("short") != -1) {
00087                         format = format.split("short").join("m/d/y");
00088                 } else if (format.indexOf("medium") != -1) {
00089                         format = format.split("medium").join("mmm d, yyyy");
00090                 } else if (format.indexOf("long") != -1) {
00091                         format = format.split("long").join("mmmm d, yyyy");
00092                 } else if (format.indexOf("full") != -1) {
00093                         format = format.split("full").join("dddd, mmmm d, yyyy");
00094                 }
00095                 
00096                 
00097                 // used to store what character set we're processing
00098                 var currentlyProcessing = '';
00099                 // how many of the characters have we run into?
00100                 var count = 0;
00101                 
00102                 // loop through the format and replace values when we can
00103                 for (var i = 0; i < format.length; i++) {
00104                         switch (format.charAt(i)) {
00105                                 case 'd':
00106                                 case 'm':
00107                                 case 'y':
00108                                         // if we've been processing one set of characters and
00109                                         // stumbled across a new set, we need to update
00110                                         // the return string
00111                                         if (currentlyProcessing != '') {
00112                                                 if (currentlyProcessing != format.charAt(i)) {
00113                                                         // processing a new character, so figure out what we've 
00114                                                         // been processing, update the processing character,
00115                                                         // and re-set the count
00116                                                         retString += maskReplace(currentlyProcessing, count, dayOfWeek, month, day, year, months, days);
00117                                                         currentlyProcessing = format.charAt(i);
00118                                                         count = 0;
00119                                                 } 
00120                                         } else {
00121                                                 currentlyProcessing = format.charAt(i); 
00122                                         }
00123                                         count++;
00124                                         break;
00125                                         
00126                                 default:
00127                                         // we're stumbled upong a character thats not a mask character
00128                                         // so if we've been processing something, replace it
00129                                         // for the return string and reset what we've processing
00130                                         // to nothing
00131                                         if (currentlyProcessing != '') {
00132                                                 retString += maskReplace(currentlyProcessing, count, dayOfWeek, month, day, year, months, days);
00133                                                 count = 0;
00134                                                 currentlyProcessing = '';
00135                                         }
00136                                         // just add the character we've run into to the return string
00137                                         retString += format.charAt(i);
00138                         }
00139                 }
00140                 
00141                 // after we're done, we need to check to see if we're still processing anything
00142                 if (count > 0) {
00143                         retString += maskReplace(currentlyProcessing, count, dayOfWeek, month, day, year, months, days);
00144                 }
00145                 
00146                 // finally, return the string with the mask replace with actual values
00147                 return retString;       
00148         }
00149         
00150         /*
00151         Darron Schall
00152         August 27, 2003
00153         
00154         Private "helper" function for dateFormat. Not meant to be called directly.
00155         */
00156         private static function maskReplace(which, count, dayOfWeek, month, day, year, months, days) {
00157                 switch(which) {
00158                         case 'd':
00159                                 switch (count) {
00160                                         case 1: return day;
00161                                         case 2: return (day<10) ? "0"+day : day;
00162                                         case 3: return dayOfWeek;
00163                                         case 4: return days[dayOfWeek].n;
00164                                 }
00165                                 break;
00166                         case 'y':
00167                                 switch (count) {
00168                                         case 1: return parseInt(year.substr(2,2));
00169                                         case 2: return year.substr(2, 2);
00170                                         case 4: return year;
00171                                 }
00172                         
00173                         case 'm':
00174                                 switch (count) {
00175                                         case 1: return months[month].m;
00176                                         case 2: return (months[month].m<10) ? "0"+months[month].m : months[month].m;
00177                                         case 3: return month;
00178                                         case 4: return months[month].n;
00179                                 }
00180                 }
00181         }
00182 }

Generated on Fri May 11 17:12:42 2007 for GuggaFramework by  doxygen 1.5.2