P
ROTOTYPE
I'm Not Only The Prototype, I'm Also A Member.
home
▪
stats
▪
search
▪
linkback
▪
about
▪
FAQ
| user: guest,
login
,
register
Date
:
Parse string with date according to JavaSimpleDateFormat
author:
frog
[+]
,
Submitted: 10.16.03 3p
/* String.parseFormatted() prototype by Peter Y. Sobolev http://www.enlight.ru/frog email: frog@enlight.ru Parse string with datetime according to given format (based on JavaSimpleDateFormat). Return unixtime. string:"21/03/2003 08:38 pm" format:"dd/MM/yyyy hh:mm aaa" ret: unixtime unixtime = ("04:03.0".parseFormatted("mm:ss.S")); in place of absent components of date/time current date/time components are used */ // ============= SOME UTILITY FUNCTIONS =============== // Returns index of the first char for which funcName returns true (false). // // example: "123qwe".indexOfChange("isCharDigitAt",100,true) 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 String.prototype.isCharDigitAt = function (i) { if ( (this.charCodeAt(i)>=48) and (this.charCodeAt(i)<=57) ) return true; else return false; }//isCharDigitAt String.prototype.isCharAnySlashAt = function (i) { if ( (this.charCodeAt(i)==47) or (this.charCodeAt(i)==92) ) return true; else return false; }//isCharDigitAt String.prototype.indexOfChange = function(funcName,startIndex,flag) { var params = Array(); if (flag) for(var i=startIndex;i<this.length;i++) { if (eval("this."+funcName).call(this,i)) return i; }//for else for(var i=startIndex;i<this.length;i++) { if (not eval("this."+funcName).call(this,i)) return i; }//for return this.length; // if index out of range and no change found }//indexOfChange // Returns index of the first char (scanning BACKWARD!) for which funcName returns true (false). // // example: "123qwe".lastIndexOfChange("isCharDigitAt",100,true) String.prototype.lastIndexOfChange = function(funcName,startIndex,flag) { var params = Array(); if (flag) for(var i=this.length-1-startIndex;i>=0;i--) { if (eval("this."+funcName).call(this,i)) return i; }//for else for(var i=this.length-1-startIndex;i>=0;i--) { if (not eval("this."+funcName).call(this,i)) return i; }//for return this.length; // if index out of range and no change found }//indexOfChange // 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 // Unix datetime -> flash datetime function UnixToFlash(date_unix) { var date_flash = new Date(); date_flash.setTime(date_unix); return date_flash; }//DateUnixToFlash // -================= MAIN PROTOTYPE =====================================-- String.prototype.parseFormatted = function(format) { var formatd = Array(); var stmp,len,ix,curpos; var pObj = new Object(); 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 into groups stmp = ""; for(var i=0;i<format.length;i++) { stmp+=format.charAt(i); if (format.charAt(i)!=format.charAt(i+1)) { formatd.push(stmp); stmp = ""; }//if }//for // trace(">>"+formatd); curpos=0; // Analyze each format group for(var i=0;i<formatd.length;i++) { switch(formatd[i].charAt(0)) { // y Year Year 1996; 96 +++++++++++++++++ case 'y': ix = this.indexOfChange("isCharDigitAt",curpos,false); len = ix-curpos; pObj.yL = this.substring(curpos,ix); // expand 1-2 digits year (-80, +20 years) if (formatd[i].length<=2) { curDate = new Date(); if ( (curDate.getFullYear()-Number("19"+pObj.yL.LeadingZero(2)))>80 ) pObj.yL = Number("20"+pObj.yL.LeadingZero(2)); else pObj.yL = Number("19"+pObj.yL.LeadingZero(2)) }//if else pObj.yL = Number(pObj.yL); curpos+=len; break; // M Month in year Month July; Jul; 07 ++++++++++++++++++++ case 'M' : if (formatd[i].length<3) // month parsed as a number { ix = this.indexOfChange("isCharDigitAt",curpos,false); len = ix-curpos; pObj.mU = Number(this.substring(curpos,ix)); curpos+=len; }//if else // month parsed as a string (Jul, July) { for (var e=0;e<monthF.length;e++) { if (monthF[e].toLowerCase()==this.substr(curpos,monthF[e].length).toLowerCase()) { var monthName = monthF[e]; break; }//if }//for if (monthName==void(1)) // if not found in monthF.. for (var e=0;e<monthS.length;e++) { if (monthS[e].toLowerCase()==this.substr(curpos,monthS[e].length).toLowerCase()) { var monthName = monthS[e]; break; }//if }//for pObj.mU = e+1; curpos+=monthName.length; }//else break; // d Day in month Number 10 +++++++++++++++++++++++++++++ case 'd' : ix = this.indexOfChange("isCharDigitAt",curpos,false); len = ix-curpos; pObj.dL = Number(this.substring(curpos,ix)); curpos+=len; break; // a Am/pm marker Text pm +++++++++++++++++++++ case 'a' : ix = this.indexOfChange("isCharLatinAt",curpos,false); len = ix-curpos; pObj.a = this.substring(curpos,ix).toLowerCase(); curpos+=len; break; // A Am/pm marker Text PM +++++++++++++++++++++ case 'A' : ix = this.indexOfChange("isCharLatinAt",curpos,false); len = ix-curpos; pObj.a = this.substring(curpos,ix).toLowerCase(); curpos+=len; break; // H Hour in day (0-23) Number 0 +++++++++++++++++++++++++ case 'H' : ix = this.indexOfChange("isCharDigitAt",curpos,false); len = ix-curpos; pObj.hU = Number(this.substring(curpos,ix)); curpos+=len; break; // k Hour in day (1-24) Number 24 case 'k' : ix = this.indexOfChange("isCharDigitAt",curpos,false); len = ix-curpos; pObj.kL = Number(this.substring(curpos,ix)); curpos+=len; break; // K Hour in am/pm (0-11) Number 0 case 'K' : ix = this.indexOfChange("isCharDigitAt",curpos,false); len = ix-curpos; pObj.kU = Number(this.substring(curpos,ix)); curpos+=len; break; // h Hour in am/pm (1-12) Number 12 case 'h' : ix = this.indexOfChange("isCharDigitAt",curpos,false); len = ix-curpos; pObj.hL = Number(this.substring(curpos,ix)); // trace("!!!!!!!!!"+pObj.hL); curpos+=len; break; // m Minute in hour Number 30 case 'm' : ix = this.indexOfChange("isCharDigitAt",curpos,false); len = ix-curpos; pObj.mL = Number(this.substring(curpos,ix)); curpos+=len; break; // s Second in minute Number 55 case 's' : ix = this.indexOfChange("isCharDigitAt",curpos,false); len = ix-curpos; pObj.sL = Number(this.substring(curpos,ix)); curpos+=len; break; // S Millisecond Number 978 case 'S' : ix = this.indexOfChange("isCharDigitAt",curpos,false); len = ix-curpos; pObj.sU = Number(this.substring(curpos,ix)); curpos+=len; break; // U seconds since the Unix Epoch (January 1 1970 00:00:00 GMT) <- my extension to the JavaSimpleDateDataFormat case 'U' : ix = this.indexOfChange("isCharDigitAt",curpos,false); len = ix-curpos; pObj.uU = Number(this.substring(curpos,ix)); curpos+=len; break; // z Time zone General time zone Pacific Standard Time; PST; GMT-08:00 case 'z' : break; // Z Time zone RFC 822 time zone -0800 case 'Z' : break; default: curpos+=formatd[i].length; //skip non-format chars break; }//switch }//for //for(name in pObj) { trace(name+"="+pObj[name]); }//for with (pObj) { // K Hour in am/pm (0-11) // h Hour in am/pm (1-12) // if 24h time not defined (only am/pm) if (hU==void(1)) { // if 0..11 hours time defined, convert it to 1..12 if (kU!=void(1)) if (kU==0) hL = 12; // trace("!!!" + pObj.hL); // if 1..12 hours time defined if (hL!=void(1)) { if (a=="am") { hU = hL; } if (a=="pm") { hU = hL+12; if (hU==24) hU=0; }//if }//if }//if rDate = new Date(); // if time defined in unix format, convert it if (uU!=void(1)) { rDate.setTime(uU); return rDate; }//if else { // if some items were skipped in date/time format, it will be replaced with current date/time items accordingly var now = new Date(); if (yL==void(1)) yL = now.getFullYear(); if (mU==void(1)) mU = now.getMonth()+1; if (dL==void(1)) dL = now.getDate(); if (hU==void(1)) hU = now.getHours(); //trace("hU=" + hU); if (mL==void(1)) mL = now.getMinutes(); if (sL==void(1)) sL = now.getSeconds(); if (sU==void(1)) sU = now.getMilliseconds(); // trace(yl+"/"+(mU-1)+"/"+dL+"/"+hU+"/"+mL+"/"+sL+"/"+sU); return ( (new Date(yL,(mU-1),dL,hU,mL,sL,sU)).getTime() ); // return value converted to Unixtime. } }//with }//String.parseFormatted
usage
// How to use String.parseFormatted() prototype... #include "string_parseformatted.as" // Parse string with datetime according to given format and print datetime converted from unix format trace(UnixToFlash("04:03.0".parseFormatted("mm:ss.S"))); //Thu Oct 16 00:04:03 GMT+0400 2003 - date/hour value is default (current) trace(UnixToFlash("21/Mar/03 12pm:30:04 473".parseFormatted("d/MMM/yy hhaa:mm:ss SSS"))); //Fri Mar 21 00:30:04 GMT+0300 2003 trace(UnixToFlash("2.10.2001 11:04:32".parseFormatted("dd.MM.yyyy HH:mm:ss"))); //Tue Oct 2 11:04:32 GMT+0400 2001
Add Comment
[+]
›opyleft 2001-2010. Layer51 is: Jaime Prado.
@