1字符串转化为日期 2 3let util = function(){ 4Date.prototype.Format = function(fmt) 5{ 6var o = { 7"M+" : this.getMonth()+1, //月份 8"d+" : this.getDate(), //日 9"h+" : this.getHours(), //小时 10"m+" : this.getMinutes(), //分 11"s+" : this.getSeconds(), //秒 12"q+" : Math.floor((this.getMonth()+3)/3), //季度 13"S" : this.getMilliseconds() //毫秒 14}; 15if(/(y+)/.test(fmt)) 16fmt=fmt.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length)); 17for(var k in o) 18if(new RegExp("("+ k +")").test(fmt)) 19fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length))); 20return fmt; 21}; 22 23return { 24} 25}(); 26 27日期转化为字符串 28 29【1】js毫秒时间转换成日期时间 30 31 32var oldTime = (new Date("2012/12/25 20:11:11")).getTime(); //得到毫秒数 33 34 35//不是上面格式的时间需要转换 36 37 38 //starttime ='2012-12-25 20:17:24'; 39 starttime = starttime.replace(new RegExp("-","gm"),"/"); 40 var starttimeHaoMiao = (new Date(starttime)).getTime(); //得到毫秒数 41 42 43【2】毫秒数转化为时间 44 45 46var oldTime = (new Date("2012/12/25 20:11:11")).getTime(); //得到毫秒数 47var newTime = new Date(oldTime); //就得到普通的时间了 48 49 50 51然后通过getHours,getFullYear等方法获取年月日,时分秒了 52 53 54 55<script type="text/javascript"> 56 57 58var d = new Date() 59document.write(d.getHours()) 60document.write(".") 61document.write(d.getMinutes()) 62document.write(".") 63document.write(d.getSeconds()) 64 65function timestampToTime(timestamp) {//转化成YYYYMMDDhhmmss格式的时间戳 66 var date = new Date(timestamp),//时间戳为10位需*1000,时间戳为13位的话不需乘1000 67 Y = date.getFullYear() + '', 68 M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1) + '', 69 D = date.getDate() + '', 70 h = date.getHours() + '', 71 m = date.getMinutes() + '', 72 s = date.getSeconds(); 73 return Y+M+D+h+m+s; 74}
JS 时间转换函数 字符串时间转换毫秒(互转)
Wesley13
2021-10-11
1137 0 0
点赞
收藏
评论区
加载中...