/** 转换日期格式
* @param date : 日期格式|String类型 (如:'2012-12-12' | '2012年12月12日' | new Date())
* @param format : String类型 (如: 'yyyy年MM月dd日'或'yyyy年MM月dd日 hh时mm分ss秒',默认'yyyy-MM-dd')
* @example C.parseDateFormat(new Date(), 'yyyy年MM月dd日') 输出:"2014年04月29日"
* @example C.parseDateFormat(new Date()) 输出:"2014-04-29"
* @exmaple C.parseDateFormat("2014-05-07 16:09:47","yyyy年MM月dd日 hh时mm分ss秒")
* 输出:"2014年05月07日 16时09分47秒"
**/
parseDateFormat: function(date, format) {
if (!date) {
return date;
}
if (!isNaN(date) && String(date).length === 8) {
date = (date + '').replace(/^(\d{4})(\d{2})(\d{2})$/, '$1/$2/$3');
}
var addZero = function (val) {
return /^\d{1}$/.test(val) ? '0' + val : val;
};
format = format || 'yyyy-MM-dd';
var year = '';
var month = '';
var day = '';
var hours = '';
var minutes = '';
var seconds = '';
if (typeof date === 'string') {
var dateReg = /\b(\d{4})\b[^\d]+(\d{1,2})\b[^\d]+(\d{1,2})\b(\s(\d{1,2}):(\d{1,2}):(\d{1,2}))?[^\d]?/;
var dateMatch = date.match(dateReg);
if (dateMatch) {
year = dateMatch[1];
month = dateMatch[2];
day = dateMatch[3];
hours = dateMatch[5];
minutes = dateMatch[6];
seconds = dateMatch[7];
}
} else {
year = date.getFullYear();
month = date.getMonth() + 1;
day = date.getDate();
hours = date.getHours();
minutes = date.getMinutes();
seconds = date.getSeconds();
}
month = addZero(month);
day = addZero(day);
hours = addZero(hours);
minutes = addZero(minutes);
seconds = addZero(seconds);
return format.replace('yyyy', year).replace('MM', month).replace('dd', day).replace('hh', hours).replace('mm', minutes).replace('ss', seconds);
},