var dt2 = "2009-11-8 9:20"
var regTime = /(\d{4})-(\d{1,2})-(\d{1,2})( \d{1,2}:\d{1,2})/g
//alert(dt1.replace(regTime, "$2-$3-$1$4"))
var interval = Math.abs(Date.parse(dt1.replace(regTime, "$2-$3-$1$4")) - Date.parse(dt2.replace(regTime, "$2-$3-$1$4")))/1000
var h = Math.floor(interval / 3600)
var m = Math.floor(interval % 3600 / 60)
alert(h + " 小时 " + m + " 分")
在IOS机型上遇到一个获取日期中的小时数莫名其妙多了8个小时的BUG,最后原因为new Date()传入的日期格式不对。原日期格式:2019-07-10T14:00:00;但在IOS上面需要的格式:2019/07/10T14:00:00;
这时把date用在new Date(date)里面,再getHours()就不会再多8小时了。
var fn = function(){var now = new Date
var y = now.getFullYear()
var m = now.getMonth()
var d = now.getDate()
var h = now.getHours()
var mm = now.getMinutes()
var s = now.getSeconds()
var str
if(h>12) {
h -= 12
str = " PM"
}else{
str = " AM"
}
h = h < 10 ? "0" + h : h
d = d < 10 ? "0" + d : d
m = m < 10 ? "0" + m : m
mm = mm < 10 ? "0" + mm : mm
s = s < 10 ? "0" + s : s
var xy = y + "/" + m + "/" + d + "," + h + ":" + mm + ":" + s
xy += str
return xy
}
console.info(fn())