P
ROTOTYPE
I'm Not Only The Prototype, I'm Also A Member.
home
▪
stats
▪
search
▪
linkback
▪
about
▪
FAQ
| user: guest,
login
,
register
Date
:
Returns formatted date using JavaSimpleDateFormat
author:
frog
[+]
,
Submitted: 10.16.03 3p
• Last Edit: 10.16.03 3p
/* Date.getFormatted() prototype by Peter Y. Sobolev http://www.enlight.ru/frog email: frog@enlight.ru Returns formatted date (string) using JavaSimpleDateFormat (format) Unsupported features (in compare with JavaSimpleDateFormat): w Week in year Number 27 W Week in month Number 2 'any_letters' Extensions: A (AM/PM with big chars), U (unix time), L (leap year flag) */ // =================== some utility functions ===================== String.prototype.isCharLatinAt = function (i) { if ( (this.charCodeAt(i)>=65 and this.charCodeAt(i)<=90) or (this.charCodeAt(i)>=97 and this.charCodeAt(i)<=122) ) return true; else return false; }//isCharLatinAt // Complete number (string) up to n digits (with zeroes) String.prototype.LeadingZero = function (n) { var z; for (var k=0;k<(n-this.length);k++) z+="0"; return (z+this); }//LeadingZero // Is this year leap? (true or false) Date.prototype.isLeapYear = function() { var y = this.getFullYear(); return (y%4 ==0) && !((y%100 == 0) && (y%400 !=0)); }//isLeapYear // ================= MAIN PROTOTYPE ===================== Date.prototype.getFormatted = function(format) { //trace("=========="+format+"/"+this); var formatd = Array(); var stmp,sout; dayS = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"]; dayF = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]; monthS = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]; monthF = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]; // Split format string by groups stmp = ""; for(i=0;i<format.length;i++) { stmp+=format.charAt(i); if (format.charAt(i)!=format.charAt(i+1)) { formatd.push(stmp); stmp = ""; }//if }//for // Analyze each group for(i=0;i<formatd.length;i++) { // pass non-format chars directly to output string if (not formatd[i].isCharLatinAt(0)) sout+=formatd[i]; // replace format chars with date/time else switch(formatd[i].charAt(0)) { // G Era designator Text AD case 'G' : sout += "AD"; // ? break; // y Year Year 1996; 96 case 'y' : if (formatd[i].length>=4) sout+=this.getFullYear().toString().leadingZero(formatd[i].length); // full form else sout+=string(this.getFullYear()).substr(this.getFullYear().length-2,2); // short form break; // L Leap Year flag (0,1) case 'L': sout += this.isLeapYear() ? 1 : 0; break; // M Month in year Month July; Jul; 07 case 'M' : if (formatd[i].length<=2) sout+=(this.getMonth()+1).toString().leadingZero(formatd[i].length); // 1 or 2 digits if (formatd[i].length==3) sout+=monthS[this.getMonth()]; if (formatd[i].length>3) sout+=monthF[this.getMonth()]; break; // w Week in year Number 27 case 'w' : break; // W Week in month Number 2 case 'W' : break; // D Day in year Number 189 case 'D' : var dy = this.getFullYear(); var dd = new Date(this.getTime()); stmp = -1; while (dd.getFullYear() == dy) { dd.setDate(dd.getDate()-1); stmp++; }//while sout += stmp.toString().LeadingZero(formatd[i].length); break; // d Day in month Number 10 case 'd' : stmp = this.getDate(); sout += stmp.toString().LeadingZero(formatd[i].length); break; // F Day of week in month Number 2 case 'F' : stmp = this.getDay(); sout += stmp.toString().LeadingZero(formatd[i].length); break; // E Day in week Text Tuesday; Tue case 'E' : if (formatd[i].length>3) sout+=dayF[this.getDay()]; else sout+=dayS[this.getDay()]; break; // a Am/pm marker Text pm case 'a' : sout += (this.getHours() < 12) ? "am" : "pm"; break; // A Am/pm marker Text PM <- my extension to the JavaSimpleDateDataFormat case 'A' : sout += (this.getHours() < 12) ? "AM" : "PM"; break; // H Hour in day (0-23) Number 0 case 'H' : stmp = this.getHours(); stmp = stmp.toString().LeadingZero(formatd[i].length); sout += stmp; break; // k Hour in day (1-24) Number 24 case 'k' : stmp = (this.getHours()==0) ? "24" : this.getHours(); sout += stmp.toString().LeadingZero(formatd[i].length); break; // K Hour in am/pm (0-11) Number 0 case 'K' : stmp = this.getHours()%12; sout += stmp.toString().LeadingZero(formatd[i].length); break; // h Hour in am/pm (1-12) Number 12 case 'h' : stmp = (this.getHours()%12 == 0) ? "12" : this.getHours()%12; sout += stmp.toString().LeadingZero(formatd[i].length); break; // m Minute in hour Number 30 case 'm' : stmp = this.getMinutes(); sout += stmp.toString().LeadingZero(formatd[i].length); break; // s Second in minute Number 55 case 's' : stmp = this.getSeconds(); sout += stmp.toString().LeadingZero(formatd[i].length); break; // S Millisecond Number 978 case 'S' : stmp = this.getMilliseconds(); sout += stmp.toString().LeadingZero(formatd[i].length); break; // U seconds since the Unix Epoch (January 1 1970 00:00:00 GMT) <- my extension to the JavaSimpleDateDataFormat case 'U' : sout += this.getTime(); break; // z Time zone General time zone Pacific Standard Time; PST; GMT-08:00 case 'z' : // this part of code was done by Charlie Cordova var off = this.getTimezoneOffset(); var pos = off<=0; //in Flash, offsets are positive west of GMT var mins = (Math.abs(off%60)).toString(); if (mins.length == 1) mins = "0" + mins; var hours = Math.floor(Math.abs(off/60)).toString(); if (hours.length == 1) hours = "0" + hours; sout += "GMT" + (pos ? "+" : "-") + hours + ":" + mins; break; // Z Time zone RFC 822 time zone -0800 case 'Z' : sout += this.getTimezoneOffset() * (-60); break; }//switch }//for // trace(sout); return sout; }//Date.getFormatted
usage
// How to use Date.getFormatted() prototype... #include "date_getformatted.as" current_datetime = new Date(); // get current datetime sample_datetime = new Date(1066227645124); // unixtime sample to Flash Date() datetime // datetime in Flash Date() format to formatted string: trace(current_datetime.getFormatted("dd.MM.yy HH:mm:ss")); trace(sample_datetime.getFormatted("dd.MM.yy")); //15.10.03 trace(sample_datetime.getFormatted("yyyy.MMMMM.dd GGG hh:mm aaa")); //2003.October.15 AD 06:20 pm trace(sample_datetime.getFormatted("K:mm a,z")); //6:20 pm,GMT+04:00 trace(sample_datetime.getFormatted("H:mm:ss:SSS")); //18:20:45:124 trace(sample_datetime.getFormatted("yyyy.MM.dd G hh:mm:ss")); //2003.10.15 AD 06:20:45 GMT+04:00 trace(sample_datetime.getFormatted("M yy d E H s")); //10 03 15 Wed 18 45 trace(sample_datetime.getFormatted("MM yyyy dd EEEE HH ss")); //10 2003 15 Wednesday 18 45 trace(sample_datetime.getFormatted("MMM a")); //Oct trace(sample_datetime.getFormatted("MMMM A")); //October trace(sample_datetime.getFormatted("E, dd MMM yyyy HH:mm:ss Z")); //Wed, 15 Oct 2003 18:20:45 14400 trace(sample_datetime.getFormatted("U")); //1066227645124 trace(sample_datetime.getFormatted("yyyy L")); //2003 0
Add Comment
[+]
›opyleft 2001-2010. Layer51 is: Jaime Prado.
@