JS和jQuery用了一段时间,最近发现他们没有自带的时间戳格式化函数,于是综合网上相关的时间戳格式化函数,自己写了一个时间戳格式化函数DateToTime,这个函数提供了多种格式化样式:
Y-m-d,Y-m-d H:i:s,Y/m/d,Y/m/d H:i:s,Y年m月d日,Y年m月d日 H:i:s
Y-m-d,Y-m-d H:i:s,Y/m/d,Y/m/d H:i:s,Y年m月d日,Y年m月d日 H:i:s这里的时间有时仅输入Y-m-d H:i也是可以使用的
/**
 * [TimeToDate时间戳转换为日期]
 * @param {[type]} unixTime [时间戳]
 * @param {String} type
[Y-m-d,Y-m-d H:i:s,Y/m/d,Y/m/d H:i:s,Y年m月d日,Y年m月d日 H:i:s]
 */
function TimeToDate(unixTime,type="Y-M-D H:i:s"){

var date = new Date(unixTime * 1000);//时间戳为10位需*1000,时间戳为13位的话不需乘1000

var datetime = "";

datetime += date.getFullYear() + type.substring(1,2);

datetime += (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1) + type.substring(3,4);

datetime += (date.getDate() < 10 ? '0'+(date.getDate()) : date.getDate());

if (type.substring(5,6)) {

if (type.substring(5,6).charCodeAt() > 255) {

datetime += type.substring(5,6);

if (type.substring(7,8)) {

datetime += " " + (date.getHours() < 10 ? '0'+(date.getHours()) : date.getHours());

if (type.substring(9,10)) {

datetime += type.substring(8,9) + (date.getMinutes() < 10 ? '0'+(date.getMinutes()) : date.getMinutes());

if (type.substring(11,12)) {

datetime += type.substring(10,11) + (date.getSeconds() < 10 ? '0'+(date.getSeconds()) : date.getSeconds());

};

};

};

}else{

datetime += " " + (date.getHours() < 10 ? '0'+(date.getHours()) : date.getHours());

if (type.substring(8,9)) {

datetime += type.substring(7,8) + (date.getMinutes() < 10 ? '0'+(date.getMinutes()) : date.getMinutes());

if (type.substring(10,11)) {

datetime += type.substring(9,10) + (date.getSeconds() < 10 ? '0'+(date.getSeconds()) : date.getSeconds());

};

};

};

};

return datetime;
}
/**
 * [TimeToDate时间戳转换为日期]
 * @param {[type]} unixTime [时间戳]
 * @param {String} type
[Y-m-d,Y-m-d H:i:s,Y/m/d,Y/m/d H:i:s,Y年m月d日,Y年m月d日 H:i:s]
 */
function TimeToDate(unixTime,type="Y-M-D H:i:s"){

var date = new Date(unixTime * 1000);//时间戳为10位需*1000,时间戳为13位的话不需乘1000

var datetime = "";

datetime += date.getFullYear() + type.substring(1,2);

datetime += (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1) + type.substring(3,4);

datetime += (date.getDate() < 10 ? '0'+(date.getDate()) : date.getDate());

if (type.substring(5,6)) {

if (type.substring(5,6).charCodeAt() > 255) {

datetime += type.substring(5,6);

if (type.substring(7,8)) {

datetime += " " + (date.getHours() < 10 ? '0'+(date.getHours()) : date.getHours());

if (type.substring(9,10)) {

datetime += type.substring(8,9) + (date.getMinutes() < 10 ? '0'+(date.getMinutes()) : date.getMinutes());

if (type.substring(11,12)) {

datetime += type.substring(10,11) + (date.getSeconds() < 10 ? '0'+(date.getSeconds()) : date.getSeconds());

};

};

};

}else{

datetime += " " + (date.getHours() < 10 ? '0'+(date.getHours()) : date.getHours());

if (type.substring(8,9)) {

datetime += type.substring(7,8) + (date.getMinutes() < 10 ? '0'+(date.getMinutes()) : date.getMinutes());

if (type.substring(10,11)) {

datetime += type.substring(9,10) + (date.getSeconds() < 10 ? '0'+(date.getSeconds()) : date.getSeconds());

};

};

};

};

return datetime;
}
TimeToDate("1515640111");
//2021-01-11 11:08:31
TimeToDate("1515640111","Y-m-d");
//2021-01-11
TimeToDate("1515640111","Y-m-d H:i");
//2021-01-11 11:08
TimeToDate("1515640111","Y-m-d H:i:s");
//2021-01-11 11:08:31
TimeToDate("1515640111","Y/m/d");
//2021/01/11
TimeToDate("1515640111","Y/m/d H:i:s");
//2021/01/11 11:08:31
TimeToDate("1515640111","Y年m月d日");
//2021年01月11日
TimeToDate("1515640111","Y年m月d日 H:i:s");
//2021年01月11日 11:08:31
TimeToDate("1515640111");
//2021-01-11 11:08:31
TimeToDate("1515640111","Y-m-d");
//2021-01-11
TimeToDate("1515640111","Y-m-d H:i");
//2021-01-11 11:08
TimeToDate("1515640111","Y-m-d H:i:s");
//2021-01-11 11:08:31
TimeToDate("1515640111","Y/m/d");
//2021/01/11
TimeToDate("1515640111","Y/m/d H:i:s");
//2021/01/11 11:08:31
TimeToDate("1515640111","Y年m月d日");
//2021年01月11日
TimeToDate("1515640111","Y年m月d日 H:i:s");
//2021年01月11日 11:08:31
/**
 * [DateToTime 日期转换时间戳]
 * @param {[type]} day [日期格式,仅支持标准格式]
 */
function DateToTime(day){

// re = /(\d{4})(?:-(\d{1,2})(?:-(\d{1,2}))?)?(?:\s+(\d{1,2}):(\d{1,2}):(\d{1,2}))?/.exec(day); // 原

re = /(\d{4})(?:\D?(\d{1,2})(?:\D?(\d{1,2}))?[^\d\s]?)?(?:\s+(\d{1,2})\D?(\d{1,2})\D?(\d{1,2}))?/.exec(day);

return new Date(re[1],(re[2]||1)-1,re[3]||1,re[4]||0,re[5]||0,re[6]||0).getTime()/1000;
}
DateToTime("2021-01-11 11:08:31");
DateToTime("2021-01-11");
DateToTime("2021年01月11日 11时08分31秒");
DateToTime("2021");
/**
 * [DateToTime 日期转换时间戳]
 * @param {[type]} day [日期格式,仅支持标准格式]
 */
function DateToTime(day){

// re = /(\d{4})(?:-(\d{1,2})(?:-(\d{1,2}))?)?(?:\s+(\d{1,2}):(\d{1,2}):(\d{1,2}))?/.exec(day); // 原

re = /(\d{4})(?:\D?(\d{1,2})(?:\D?(\d{1,2}))?[^\d\s]?)?(?:\s+(\d{1,2})\D?(\d{1,2})\D?(\d{1,2}))?/.exec(day);

return new Date(re[1],(re[2]||1)-1,re[3]||1,re[4]||0,re[5]||0,re[6]||0).getTime()/1000;
}
DateToTime("2021-01-11 11:08:31");
DateToTime("2021-01-11");
DateToTime("2021年01月11日 11时08分31秒");
DateToTime("2021");补充时间戳与日期相互转换:
function TimeToDate(date,format) {

format = format || 'YYYY-MM-DD hh:mm:ss';

var dateTest = (/^(-)?\d{1,10}$/.test(date) || /^(-)?\d{1,13}$/.test(date));

if(/^[1-9]*[1-9][0-9]*$/.test(date) && dateTest){

var vdate = parseInt(date);

if (/^(-)?\d{1,10}$/.test(vdate)) {

vdate = vdate * 1000;

} else if (/^(-)?\d{1,13}$/.test(vdate)) {

vdate = vdate * 1000;

} else if (/^(-)?\d{1,14}$/.test(vdate)) {

vdate = vdate * 100;

} else {

alert("时间戳格式不正确");

return;

}

var setdate = new Date(vdate);

return parse({YYYY:setdate.getFullYear(), MM:digit(setdate.getMonth()+1), DD:digit(setdate.getDate()) , hh:digit(setdate.getHours()), mm:digit(setdate.getMinutes()), ss:digit(setdate.getSeconds()) }, format);

}else {

//将日期转换成时间戳

var arrs = date.match(/\w+|d+/g),

newdate = new Date(arrs[0],parseInt(arrs[1])-1,arrs[2],arrs[3]||0,arrs[4]||0,arrs[5]||0),

timeStr = Math.round(newdate.getTime() / 1000);

return timeStr;

}


function parse(ymdhms, format) {

var regymdzz = "YYYY|MM|DD|hh|mm|ss|zz";

return format.replace(new RegExp(regymdzz,"g"), function(str, index) {

return str == "zz" ? "00":digit(ymdhms[str]);

});

};

function digit(num) {

return num < 10 ? "0" + (num | 0) :num;

};
};
function TimeToDate(date,format) {

format = format || 'YYYY-MM-DD hh:mm:ss';

var dateTest = (/^(-)?\d{1,10}$/.test(date) || /^(-)?\d{1,13}$/.test(date));

if(/^[1-9]*[1-9][0-9]*$/.test(date) && dateTest){

var vdate = parseInt(date);

if (/^(-)?\d{1,10}$/.test(vdate)) {

vdate = vdate * 1000;

} else if (/^(-)?\d{1,13}$/.test(vdate)) {

vdate = vdate * 1000;

} else if (/^(-)?\d{1,14}$/.test(vdate)) {

vdate = vdate * 100;

} else {

alert("时间戳格式不正确");

return;

}

var setdate = new Date(vdate);

return parse({YYYY:setdate.getFullYear(), MM:digit(setdate.getMonth()+1), DD:digit(setdate.getDate()) , hh:digit(setdate.getHours()), mm:digit(setdate.getMinutes()), ss:digit(setdate.getSeconds()) }, format);

}else {

//将日期转换成时间戳

var arrs = date.match(/\w+|d+/g),

newdate = new Date(arrs[0],parseInt(arrs[1])-1,arrs[2],arrs[3]||0,arrs[4]||0,arrs[5]||0),

timeStr = Math.round(newdate.getTime() / 1000);

return timeStr;

}


function parse(ymdhms, format) {

var regymdzz = "YYYY|MM|DD|hh|mm|ss|zz";

return format.replace(new RegExp(regymdzz,"g"), function(str, index) {

return str == "zz" ? "00":digit(ymdhms[str]);

});

};

function digit(num) {

return num < 10 ? "0" + (num | 0) :num;

};
};总结总结