时间戳格式转换
function jsonDateFormat(jsonDate) { //json日期格式转换为正常格式 var jsonDateStr = jsonDate.toString();//此处用到toString()是为了让传入的值为字符串类型,目的是为了避免传入的数据类型不支持.replace()方法 try { var k = parseInt(jsonDateStr.replace("/Date(", "").replace(")/", ""), 10); if (k < 0) return null; var date = new Date(parseInt(jsonDateStr.replace("/Date(", "").replace(")/", ""), 10)); var month = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1; var day = date.getDate() < 10 ? "0" + date.getDate() : date.getDate(); var hours = date.getHours() < 10 ? "0" + date.getHours() : date.getHours(); var minutes = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes(); var seconds = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds(); var milliseconds = date.getMilliseconds(); return date.getFullYear() + "-" + month + "-" + day + " " + hours + ":" + minutes + ":" + seconds; } catch (ex) { return "时间格式转换错误"; } }
获取当前时间转换
function dateTimeDiff(diff) { var myDate = new DateTimeAdd(‘d‘, diff); //获取当前年 var year = myDate.getFullYear(); //获取当前月 var month = myDate.getMonth() + 1; //获取当前日 var date = myDate.getDate(); var h = myDate.getHours(); //获取当前小时数(0-23) var m = myDate.getMinutes(); //获取当前分钟数(0-59) var s = myDate.getSeconds(); var now = year + ‘-‘ + p(month) + "-" + p(date) + " " + p(h) + ‘:‘ + p(m) + ":" + p(s); return now; } /** * 获取当前时间 */ function p(s) { return s < 10 ? ‘0‘ + s : s; } function DateTimeAdd(strInterval, Number) { var dtTmp = new Date(); switch (strInterval) { case ‘s‘: return new Date(Date.parse(dtTmp) + (1000 * Number)); case ‘n‘: return new Date(Date.parse(dtTmp) + (60000 * Number)); case ‘h‘: return new Date(Date.parse(dtTmp) + (3600000 * Number)); case ‘d‘: return new Date(Date.parse(dtTmp) + (86400000 * Number)); case ‘w‘: return new Date(Date.parse(dtTmp) + ((86400000 * 7) * Number)); case ‘q‘: return new Date(dtTmp.getFullYear(), (dtTmp.getMonth()) + Number * 3, dtTmp.getDate(), dtTmp.getHours(), dtTmp.getMinutes(), dtTmp.getSeconds()); case ‘m‘: return new Date(dtTmp.getFullYear(), (dtTmp.getMonth()) + Number, dtTmp.getDate(), dtTmp.getHours(), dtTmp.getMinutes(), dtTmp.getSeconds()); case ‘y‘: return new Date((dtTmp.getFullYear() + Number), dtTmp.getMonth(), dtTmp.getDate(), dtTmp.getHours(), dtTmp.getMinutes(), dtTmp.getSeconds()); } }
时间: 2024-10-06 07:44:08