js系列:时间格式转成时间戳和比较某个时段是否在另一个时间段内
1.将时间格式转成时间戳,将 时间戳转为时间格式。 JS中没有类似PHP那样简便的函数可以直接将时间戳与日期类型格式相互转换。需要手写一个 function datetime_to_unix(datetime){//需要传入的时间格式2012-11-16 10:36:50精确到秒,如果没有,删除arr[5] var tmp_datetime = datetime.replace(/:/g,'-'); tmp_datetime = tmp_datetime.replace(/ /g,'-'); var arr = tmp_datetime.split("-"); var now = new Date(Date.UTC(arr[0],arr[1]-1,arr[2],arr[3]-8,arr[4],arr[5]));//,arr[5] return parseInt(now.getTime()/1000); } function unix_to_datetime(unix) {//调整格式 var now = new Date(parseInt(unix) * 1000); return now.toLocaleString().replace(/年|月/g, "-").replace(/日/g, " "); } 2.判断比较某个时间时段是否在另一个时间段内,例子中时间段是json的数组