var m={};
/**
* 时间戳转时间
* @param {timeNumber} {number} 时间戳,为空或0,则返回当前时间
* @return array
*/
m.timeToString=function(timeNumber) {
if (!timeNumber) timeNumber=Date.parse(newDate());
var now=newDate(timeNumber);
var year=now.getFullYear();
var month=(now.getMonth()+1).toString();
if (month.length==1) {month='0'+month;}
var day=now.getDate().toString();
if (day.length==1) {day='0'+day;}
var hour=now.getHours().toString();
if (hour.length==1) {hour='0'+hour;}
var minute=now.getMinutes().toString();
if (minute.length==1) {minute='0'+minute;}
var second=now.getSeconds().toString();
if (second.length==1) {second='0'+second;}
return year+"-"+month+"-"+day+" "+hour+":"+minute+":"+second;
}
/**
* 时间转时间戳
* @param {timeString}{string}日期字符串,如:2020-12-12 12:00:00
* @return array
*/
m.timeToNumber=function(timeString) {
if (!timeString) timeString=m.timeToString('');
timeString=timeString.replace(/\-/gim,'/');/*时间格式转斜杠,否则不ios不兼容*/var timestamp = Date.parse(newDate(timeString));
return timestamp;
}
module.exports = m;
虽然没用过,但听你这么一说,感觉挺蛋疼的
还是自己查找资料转一个靠谱点
var m={}; /** * 时间戳转时间 * @param {timeNumber} {number} 时间戳,为空或0,则返回当前时间 * @return array */ m.timeToString=function(timeNumber) { if (!timeNumber) timeNumber=Date.parse(new Date()); var now=new Date(timeNumber); var year=now.getFullYear(); var month=(now.getMonth()+1).toString(); if (month.length==1) {month='0'+month;} var day=now.getDate().toString(); if (day.length==1) {day='0'+day;} var hour=now.getHours().toString(); if (hour.length==1) {hour='0'+hour;} var minute=now.getMinutes().toString(); if (minute.length==1) {minute='0'+minute;} var second=now.getSeconds().toString(); if (second.length==1) {second='0'+second;} return year+"-"+month+"-"+day+" "+hour+":"+minute+":"+second; } /** * 时间转时间戳 * @param {timeString} {string} 日期字符串,如:2020-12-12 12:00:00 * @return array */ m.timeToNumber=function(timeString) { if (!timeString) timeString=m.timeToString(''); timeString=timeString.replace(/\-/gim,'/');/*时间格式转斜杠,否则不ios不兼容*/ var timestamp = Date.parse(new Date(timeString)); return timestamp; } module.exports = m;