/** * SimpledateFormat javascript class * * var reverseDateFormat = new SimpleDateFormat("YYYYMMdd"); * var reverseDate = reverseDateFormat.format(new Date()); * http://developer.android.com/reference/java/text/SimpleDateFormat.html * **/ Date.prototype.getTimeZoneLong = function() { var currentTimezone = this.getTimezoneOffset(); currentTimezone = (currentTimezone/60) * -1; var gmt = ''; if (currentTimezone !== 0) { gmt += currentTimezone > 0 ? '+' : '-'; gmt += (currentTimezone + 100).toString().substring(1) + ':00'; } return gmt; } Date.prototype.getTimeZoneShort = function() { var currentTimezone = this.getTimezoneOffset(); currentTimezone = (currentTimezone/60) * -1; var gmt = ''; if (currentTimezone !== 0) { gmt += currentTimezone > 0 ? '+' : '-'; gmt += (currentTimezone + 100).toString().substring(1) + '00'; } return gmt; } Date.prototype.getTimeZoneGMT = function() { var currentTimezone = this.getTimezoneOffset(); currentTimezone = (currentTimezone/60) * -1; var gmt = 'GMT'; if (currentTimezone !== 0) { gmt += currentTimezone > 0 ? '+' : '-'; gmt += (currentTimezone + 100).toString().substring(1) + ':00'; } return gmt; } Date.prototype.getDayInYear = function() { var ngayDauNam = new Date(this.getFullYear(),0,1); return Math.floor((this - ngayDauNam)/(24*60*60*1000)); } Date.prototype.getWeekInMonth = function() { var ngayDauThang = new Date(this.getFullYear(),this.getMonth(),1); var dayNum = (this - ngayDauThang)/(24*60*60*1000); return Math.floor(dayNum/7); } Date.prototype.getWeekInYear = function() { var ngayDauNam = new Date(this.getFullYear(),0,1); var dayNum = (this - ngayDauNam)/(24*60*60*1000); return Math.floor(dayNum/7); } Date.prototype.getTZAbbreFull = function() { return (new Date()).toString().split('(')[1].replace(')',''); } Date.prototype.getTZAbbreShort = function() { var longName = (new Date()).toString().split('(')[1].replace(')','').split(' '); var shortName = ''; for ( var t1 = 0; t1 < longName.length; t1++) { shortName += longName[t1].charAt(0); } return shortName; } function SimpleDateFormat(formatString) { this.formatString = formatString; this.monthNames = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]; this.dayNames = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]; this.ampm = ["am", "am", "am", "am", "am", "am", "am", "am", "am", "am", "am", "am", "pm", "pm", "pm", "pm", "pm", "pm", "pm", "pm", "pm", "pm", "pm", "pm"]; this.format = function(aDate) { var localFormatString = formatString; var index = 0; var v = []; // The order is significant /*D day in year*/ /* D */ localFormatString = localFormatString.replace(/D{1,}/g, "\" + v[" + index + "] + \""); v[index] = aDate.getDayInYear(); index += 1; /*E day of week (Text) E/EE/EEE:Tue, EEEE:Tuesday, EEEEE:T*/ /* EEEEE */ localFormatString = localFormatString.replace(/E{5,}/g, "\" + v[" + index + "] + \""); v[index] = ((this.dayNames[aDate.getDay()]).substring(0,1)); index += 1; /* EEEE */ localFormatString = localFormatString.replace(/E{4}/g, "\" + v[" + index + "] + \""); v[index] = (this.dayNames[aDate.getDay()]); index += 1; /* EEE */ localFormatString = localFormatString.replace(/E{3}/g, "\" + v[" + index + "] + \""); v[index] = ((this.dayNames[aDate.getDay()]).substring(0,3)); index += 1; /* EE */ localFormatString = localFormatString.replace(/E{2}/g, "\" + v[" + index + "] + \""); v[index] = ((this.dayNames[aDate.getDay()]).substring(0,3)); index += 1; /* E */ localFormatString = localFormatString.replace(/E/g, "\" + v[" + index + "] + \""); v[index] = ((this.dayNames[aDate.getDay()]).substring(0,3)); index += 1; /*F day of week in month*/ /* F */ localFormatString = localFormatString.replace(/F,/g, "\" + v[" + index + "] + \""); v[index] = (aDate.getDay()+100).toString().substring(1); index += 1; /*G era designator*/ localFormatString = localFormatString.replace(/G/g, "\" + v[" + index + "] + \""); v[index] = 'AD'; index += 1; /*H hour in day (0-23)*/ /* HH */ localFormatString = localFormatString.replace(/H{2,}/g, "\" + v[" + index + "] + \""); v[index] = (aDate.getHours()+100).toString().substring(1); index += 1; /* H */ localFormatString = localFormatString.replace(/H/g, "\" + v[" + index + "] + \""); v[index] = aDate.getHours(); index += 1; /*K hour in am/pm (0-11)*/ /* K */ localFormatString = localFormatString.replace(/K{1,}/g, "\" + v[" + index + "] + \""); v[index] = this.ampm[aDate.getHours()]; index += 1; /*L stand-alone month L:1 LL:01 LLL:Jan LLLL:January LLLLL:J*/ /* LLLLL */ localFormatString = localFormatString.replace(/L{5,}/g, "\" + v[" + index + "] + \""); v[index] = (this.monthNames[aDate.getMonth()]).substring(0,1); index += 1; /* LLLL */ localFormatString = localFormatString.replace(/L{4}/g, "\" + v[" + index + "] + \""); v[index] = (this.monthNames[aDate.getMonth()]); index += 1; /* LLL */ localFormatString = localFormatString.replace(/L{3}/g, "\" + v[" + index + "] + \""); v[index] = ((this.monthNames[aDate.getMonth()]).substring(0,3)); index += 1; /* LL */ localFormatString = localFormatString.replace(/L{2}/g, "\" + v[" + index + "] + \""); v[index] = (aDate.getMonth()+101).toString().substring(1); index += 1; /* L */ localFormatString = localFormatString.replace(/L/g, "\" + v[" + index + "] + \""); v[index] = aDate.getMonth(); index += 1; /*M month in year M:1 MM:01 MMM:Jan MMMM:January MMMMM:J*/ /* MMMMM */ localFormatString = localFormatString.replace(/M{5,}/g, "\" + v[" + index + "] + \""); v[index] = (this.monthNames[aDate.getMonth()]).substring(0,1); index += 1; /* MMMM */ localFormatString = localFormatString.replace(/M{4}/g, "\" + v[" + index + "] + \""); v[index] = (this.monthNames[aDate.getMonth()]); index += 1; /* MMM */ localFormatString = localFormatString.replace(/M{3}/g, "\" + v[" + index + "] + \""); v[index] = ((this.monthNames[aDate.getMonth()]).substring(0,3)); index += 1; /* MM */ localFormatString = localFormatString.replace(/M{2}/g, "\" + v[" + index + "] + \""); v[index] = (aDate.getMonth()+101).toString().substring(1); index += 1; /* M */ localFormatString = localFormatString.replace(/M/g, "\" + v[" + index + "] + \""); v[index] = aDate.getMonth() + 1; index += 1; /*S fractional seconds*/ /* SSS */ localFormatString = localFormatString.replace(/S{3,}/g, "\" + v[" + index + "] + \""); v[index] = (aDate.getMilliseconds()+1000).toString().substring(1); index += 1; /* SS */ localFormatString = localFormatString.replace(/S{2}/g, "\" + v[" + index + "] + \""); v[index] = aDate.getMilliseconds().toString().substring(0, 2); index += 1; /* S */ localFormatString = localFormatString.replace(/S/g, "\" + v[" + index + "] + \""); v[index] = aDate.getMilliseconds().toString().substring(0, 1); index += 1; /*W week in month*/ localFormatString = localFormatString.replace(/W/g, "\" + v[" + index + "] + \""); v[index] = aDate.getWeekInMonth(); index += 1; /*Z time zone (RFC 822) Z/ZZ/ZZZ:-0800 ZZZZ:GMT-08:00 ZZZZZ:-08:00*/ /* ZZZZZ */ localFormatString = localFormatString.replace(/Z{5,}/g, "\" + v[" + index + "] + \""); v[index] = aDate.getTimeZoneLong(); index += 1; /* ZZZZ */ localFormatString = localFormatString.replace(/Z{4}/g, "\" + v[" + index + "] + \""); v[index] = aDate.getTimeZoneGMT(); index += 1; /* ZZZ */ localFormatString = localFormatString.replace(/Z{3}/g, "\" + v[" + index + "] + \""); v[index] = aDate.getTimeZoneShort(); index += 1; /* ZZ */ localFormatString = localFormatString.replace(/Z{2}/g, "\" + v[" + index + "] + \""); v[index] = aDate.getTimeZoneShort(); index += 1; /* Z */ localFormatString = localFormatString.replace(/Z/g, "\" + v[" + index + "] + \""); v[index] = aDate.getTimeZoneShort(); index += 1; /*a am/pm marker*/ /* a */ localFormatString = localFormatString.replace(/a{1,}/g, "\" + v[" + index + "] + \""); v[index] = this.ampm[aDate.getHours()]; index += 1; /*c stand-alone day of week c/cc/ccc:Tue, cccc:Tuesday, ccccc:T*/ /* ccccc */ localFormatString = localFormatString.replace(/c{5,}/g, "\" + v[" + index + "] + \""); v[index] = ((this.dayNames[aDate.getDay()]).substring(0,1)); index += 1; /* cccc */ localFormatString = localFormatString.replace(/c{4}/g, "\" + v[" + index + "] + \""); v[index] = (this.dayNames[aDate.getDay()]); index += 1; /* ccc */ localFormatString = localFormatString.replace(/c{3}/g, "\" + v[" + index + "] + \""); v[index] = ((this.dayNames[aDate.getDay()]).substring(0,3)); index += 1; /* cc */ localFormatString = localFormatString.replace(/c{2}/g, "\" + v[" + index + "] + \""); v[index] = ((this.dayNames[aDate.getDay()]).substring(0,3)); index += 1; /* c */ localFormatString = localFormatString.replace(/c/g, "\" + v[" + index + "] + \""); v[index] = ((this.dayNames[aDate.getDay()]).substring(0,3)); index += 1; /*d day in month*/ /* dd */ localFormatString = localFormatString.replace(/d{2,}/g, "\" + v[" + index + "] + \""); v[index] = (aDate.getDate()+100).toString().substring(1); index += 1; /* d */ localFormatString = localFormatString.replace(/d/g, "\" + v[" + index + "] + \""); v[index] = aDate.getDate(); index += 1; /*h hour in am/pm (1-12)*/ /* hh */ localFormatString = localFormatString.replace(/h{2,}/g, "\" + v[" + index + "] + \""); var hours = aDate.getHours()>12?aDate.getHours()-12:aDate.getHours(); v[index] = (hours+100).toString().substring(1); index += 1; /* h */ localFormatString = localFormatString.replace(/h/g, "\" + v[" + index + "] + \""); v[index] = hours; index += 1; /*k hour in day (1-24)*/ /* kk */ localFormatString = localFormatString.replace(/k{2,}/g, "\" + v[" + index + "] + \""); v[index] = (aDate.getHours()+101).toString().substring(1); index += 1; /* k */ localFormatString = localFormatString.replace(/k/g, "\" + v[" + index + "] + \""); v[index] = aDate.getHours()+1; index += 1; /*m minute in hour*/ /* mm */ localFormatString = localFormatString.replace(/m{2,}/g, "\" + v[" + index + "] + \""); v[index] = (aDate.getMinutes()+100).toString().substring(1); index += 1; /* m */ localFormatString = localFormatString.replace(/m/g, "\" + v[" + index + "] + \""); v[index] = aDate.getMinutes(); index += 1; /*s second in minute*/ /* ss */ localFormatString = localFormatString.replace(/s{2,}/g, "\" + v[" + index + "] + \""); v[index] = (aDate.getSeconds()+100).toString().substring(1); index += 1; /* s */ localFormatString = localFormatString.replace(/s/g, "\" + v[" + index + "] + \""); v[index] = aDate.getSeconds(); index += 1; /*w week in year*/ localFormatString = localFormatString.replace(/w/g, "\" + v[" + index + "] + \""); v[index] = aDate.getWeekInYear(); index += 1; /*y year yy:10 y/yyy/yyyy:2010*/ /* yyyy */ localFormatString = localFormatString.replace(/y{4,}/g, "\" + v[" + index + "] + \""); v[index] = (aDate.getFullYear()); index += 1; /* yyy */ localFormatString = localFormatString.replace(/y{3}/g, "\" + v[" + index + "] + \""); v[index] = (aDate.getFullYear()); index += 1; /* yy */ localFormatString = localFormatString.replace(/y{2}/g, "\" + v[" + index + "] + \""); v[index] = ((aDate.getFullYear()).toString().substring(2)); index += 1; /* y */ localFormatString = localFormatString.replace(/y/g, "\" + v[" + index + "] + \""); v[index] = (aDate.getFullYear()); index += 1; /*z time zone*/ /* zzzz */ localFormatString = localFormatString.replace(/z{4,}/g, "\" + v[" + index + "] + \""); v[index] = aDate.getTZAbbreFull(); index += 1; /* zzz */ localFormatString = localFormatString.replace(/z{3}/g, "\" + v[" + index + "] + \""); v[index] = aDate.getTZAbbreShort(); index += 1; /* zz */ localFormatString = localFormatString.replace(/z{2}/g, "\" + v[" + index + "] + \""); v[index] = aDate.getTZAbbreShort(); index += 1; /* z */ localFormatString = localFormatString.replace(/z/g, "\" + v[" + index + "] + \""); v[index] = aDate.getTZAbbreShort(); index += 1; localFormatString = "\"" + localFormatString + "\""; //prompt("localFormatString", localFormatString); var formatedDate = eval(localFormatString); return (formatedDate); } }