如何将JavaScript时间戳转换成日期格式,又将日期格式转换成时间戳?

2026-04-02 10:151阅读0评论SEO基础
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计964个文字,预计阅读时间需要4分钟。

如何将JavaScript时间戳转换成日期格式,又将日期格式转换成时间戳?

前端时间格式转换的方法如下:

如何将JavaScript时间戳转换成日期格式,又将日期格式转换成时间戳?

javascript

前端时间格式转时间戳的几种方法

<script type="text/javascript"> var date=new Date(); console.log(date);//Wed Feb 13 2019 11:40:45 GMT+0800 (中国标准时间) // 1:不推荐这种办法,毫秒级别的数值被转化为000 var timeStamp1=Date.parse(date); console.log(timeStamp1);//1550029245000 // 2:通过valueOf()函数返回指定对象的原始值获得准确的时间戳值 var timeStamp2=date.valueOf(); console.log(timeStamp2);//1550029245434 // 3:通过原型方法直接获得当前时间的毫秒值,准确 var timeStamp3=date.getTime(); console.log(timeStamp3);//1550029245434 // 4:将时间转化为一个number类型的数值,即时间戳 var timeStamp4=Number(date); console.log(timeStamp4);//1550029245434 </script>

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 H:i也是可以使用的

/** *[TimeToDate时间戳转换为日期] *@param{[type]}unixTime[时间戳] *@param{String}type[Y-m-d,Y-m-dH:i:s,Y/m/d,Y/m/dH:i:s,Y年m月d日,Y年m月d日H:i:s] */ functionTimeToDate(unixTime,type="Y-M-DH:i:s"){ vardate=newDate(unixTime*1000);//时间戳为10位需*1000,时间戳为13位的话不需乘1000 vardatetime=""; 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()); }; }; }; }; returndatetime; }

TimeToDate("1515640111");//2018-01-1111:08:31 TimeToDate("1515640111","Y-m-d");//2018-01-11 TimeToDate("1515640111","Y-m-dH:i");//2018-01-1111:08 TimeToDate("1515640111","Y-m-dH:i:s");//2018-01-1111:08:31 TimeToDate("1515640111","Y/m/d");//2018/01/11 TimeToDate("1515640111","Y/m/dH:i:s");//2018/01/1111:08:31 TimeToDate("1515640111","Y年m月d日");//2018年01月11日 TimeToDate("1515640111","Y年m月d日H:i:s");//2018年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("2018-01-1111:08:31"); DateToTime("2018-01-11"); DateToTime("2018年01月11日11时08分31秒"); DateToTime("2018");

补充时间戳与日期相互转换:

functionTimeToDate(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; }; };

总结

到此这篇关于JS时间戳与日期格式互相转换的文章就介绍到这了,更多相关JS时间戳与日期格式互相转换内容请搜索易盾网络以前的文章或继续浏览下面的相关文章希望大家以后多多支持易盾网络!

标签:简单

本文共计964个文字,预计阅读时间需要4分钟。

如何将JavaScript时间戳转换成日期格式,又将日期格式转换成时间戳?

前端时间格式转换的方法如下:

如何将JavaScript时间戳转换成日期格式,又将日期格式转换成时间戳?

javascript

前端时间格式转时间戳的几种方法

<script type="text/javascript"> var date=new Date(); console.log(date);//Wed Feb 13 2019 11:40:45 GMT+0800 (中国标准时间) // 1:不推荐这种办法,毫秒级别的数值被转化为000 var timeStamp1=Date.parse(date); console.log(timeStamp1);//1550029245000 // 2:通过valueOf()函数返回指定对象的原始值获得准确的时间戳值 var timeStamp2=date.valueOf(); console.log(timeStamp2);//1550029245434 // 3:通过原型方法直接获得当前时间的毫秒值,准确 var timeStamp3=date.getTime(); console.log(timeStamp3);//1550029245434 // 4:将时间转化为一个number类型的数值,即时间戳 var timeStamp4=Number(date); console.log(timeStamp4);//1550029245434 </script>

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 H:i也是可以使用的

/** *[TimeToDate时间戳转换为日期] *@param{[type]}unixTime[时间戳] *@param{String}type[Y-m-d,Y-m-dH:i:s,Y/m/d,Y/m/dH:i:s,Y年m月d日,Y年m月d日H:i:s] */ functionTimeToDate(unixTime,type="Y-M-DH:i:s"){ vardate=newDate(unixTime*1000);//时间戳为10位需*1000,时间戳为13位的话不需乘1000 vardatetime=""; 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()); }; }; }; }; returndatetime; }

TimeToDate("1515640111");//2018-01-1111:08:31 TimeToDate("1515640111","Y-m-d");//2018-01-11 TimeToDate("1515640111","Y-m-dH:i");//2018-01-1111:08 TimeToDate("1515640111","Y-m-dH:i:s");//2018-01-1111:08:31 TimeToDate("1515640111","Y/m/d");//2018/01/11 TimeToDate("1515640111","Y/m/dH:i:s");//2018/01/1111:08:31 TimeToDate("1515640111","Y年m月d日");//2018年01月11日 TimeToDate("1515640111","Y年m月d日H:i:s");//2018年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("2018-01-1111:08:31"); DateToTime("2018-01-11"); DateToTime("2018年01月11日11时08分31秒"); DateToTime("2018");

补充时间戳与日期相互转换:

functionTimeToDate(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; }; };

总结

到此这篇关于JS时间戳与日期格式互相转换的文章就介绍到这了,更多相关JS时间戳与日期格式互相转换内容请搜索易盾网络以前的文章或继续浏览下面的相关文章希望大家以后多多支持易盾网络!

标签:简单