日期,字符串,毫秒值的相互转换

匿名 (未验证) 提交于 2019-12-03 00:09:02
<script>                  Date.prototype.format = function(fmt) {               var o = {                  "M+" : this.getMonth()+1,                 //月份                  "d+" : this.getDate(),                    //日                  "h+" : this.getHours(),                   //小时                  "m+" : this.getMinutes(),                 //分                  "s+" : this.getSeconds(),                 //秒                  "q+" : Math.floor((this.getMonth()+3)/3), //季度                  "S"  : this.getMilliseconds()             //毫秒              };              if(/(y+)/.test(fmt)) {                     fmt=fmt.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length));              }              for(var k in o) {                 if(new RegExp("("+ k +")").test(fmt)){                      fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));                  }              }             return fmt;          }         //将日期对象格式化为字符串         console.log(new Date().format("yyyy-MM-dd hh:mm:ss"));                  //将字符串转化为日期对象         //2015-12-15 20:11:11         //2018.08.16 12:15:11         //2018/08/16 12:15:11  可以接受的格式有很多         var str = "2018/08/16 12:15:11";         console.log(new Date(str));                  //日期转化为毫秒值         console.log(new Date().getTime());         //毫秒值转化为日期         console.log(new Date(1568962985134));      </script>

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!