zl程序教程

您现在的位置是:首页 >  后端

当前栏目

js时间格式与时间戳的相互转换示例代码

JS转换代码 时间 示例 格式 相互
2023-06-13 09:15:14 时间
一.时间转换时间戳
复制代码代码如下:

functiontransdate(endTime){
vardate=newDate();
date.setFullYear(endTime.substring(0,4));
date.setMonth(endTime.substring(5,7)-1);
date.setDate(endTime.substring(8,10));
date.setHours(endTime.substring(11,13));
date.setMinutes(endTime.substring(14,16));
date.setSeconds(endTime.substring(17,19));
returnDate.parse(date)/1000;
}

二.时间戳转换时间
(1):转换成2011-3-1616:50:43格式:
复制代码代码如下:

functiongetDate(tm){
vartt=newDate(parseInt(tm)*1000).toLocaleString().replace(/年|月/g,"-").replace(/日/g,"")
returntt;
}

(2):转换成2011年3月16日16:50:43:
复制代码代码如下:
functiongetDate(tm){
vartt=newDate(parseInt(tm)*1000).toLocaleString()
returntt;
}

(3):转换成2011年3月16日16:50
复制代码代码如下:
functiongetDate(tm){
vartt=newDate(parseInt(tm)*1000).toLocaleString().substr(0,16);
returntt;
}