nguyenvanduocit
9/16/2015 - 6:54 AM

Javascript Format Date

formatDate: function(date, format){
					if (!date)
						return '';
					if (typeof format === 'string')
						format = this.parseFormat(format);
					var dateObj =  new Date(date);
					var dates = $.fn.datepicker.dates = {
						days: ae_globals.dates.days,
						daysShort: ae_globals.dates.daysShort,
						daysMin: ae_globals.dates.daysMin,
						months: ae_globals.dates.months,
						monthsShort: ae_globals.dates.monthsShort
					};
					var val = {
						d: dateObj.getUTCDate()+1,
						D: dates.daysShort[dateObj.getUTCDay()],
						DD: dates.days[dateObj.getUTCDay()],
						m: dateObj.getUTCMonth() + 1,
						M: dates.monthsShort[dateObj.getUTCMonth()],
						MM: dates.months[dateObj.getUTCMonth()],
						yy: dateObj.getUTCFullYear().toString().substring(2),
						yyyy: dateObj.getUTCFullYear()
					};
					val.dd = (val.d < 10 ? '0' : '') + val.d;
					val.mm = (val.m < 10 ? '0' : '') + val.m;
					date = [];
					var seps = $.extend([], format.separators);
					for (var i=0, cnt = format.parts.length; i <= cnt; i++){
						if (seps.length)
							date.push(seps.shift());
						date.push(val[format.parts[i]]);
					}
					return date.join('');
				},
				validParts: /dd?|DD?|mm?|MM?|yy(?:yy)?/g,
				nonpunctuation: /[^ -\/:-@\[\u3400-\u9fff-`{-~\t\n\r]+/g,
				parseFormat: function(format){
					// IE treats \0 as a string end in inputs (truncating the value),
					// so it's a bad format delimiter, anyway
					var separators = format.replace(this.validParts, '\0').split('\0'),
							parts = format.match(this.validParts);
					if (!separators || !separators.length || !parts || parts.length === 0){
						throw new Error("Invalid date format.");
					}
					return {separators: separators, parts: parts};
				},