摘要:时间戳或由于项目有多个后端人员,难免会出现返回时间样式不统一现象,但前端显示又要统一,先上代码毫秒只需调用此函数判断是否是时间戳昨天今天明天后天昨天今天明天后天时间戳转化成时间格式保证不出现个位数情况
时间戳或yyyy-MM-dd HH:mm:ss
由于项目有多个后端人员,难免会出现返回时间样式不统一现象,但前端显示又要统一,先上代码
// params => "2019-04-02 17:11:55" OR 1554257391000 (毫秒) 只需调用此函数 function formatRecentTime(params) { var date = "" if (isNaN(params)) { // 判断是否是时间戳 date = params.toString() } else { date = timeFormat(params) } var year = date.substring(0, 4) var month = date.substring(5, 7) var day = date.substring(8, 10) var hour = date.substring(11, 13) var minute = date.substring(14, 16) var seconds = date.substring(17, 19) var orignDate = year + "-" + month + "-" + day + " " + hour + ":" + minute + ":" + seconds var yesterday = GetDateStr(-1)// 昨天 var yesterdayStr = yesterday.split("-") yesterdayStr[1] = yesterdayStr[1].length === 1 ? "0" + yesterdayStr[1] : yesterdayStr[1] yesterdayStr[2] = yesterdayStr[2].length === 1 ? "0" + yesterdayStr[2] : yesterdayStr[2] var today = GetDateStr(0)// 今天 var todayStr = today.split("-") todayStr[1] = todayStr[1].length === 1 ? "0" + todayStr[1] : todayStr[1] todayStr[2] = todayStr[2].length === 1 ? "0" + todayStr[2] : todayStr[2] var tomorrow = GetDateStr(1)// 明天 var tomorrowStr = tomorrow.split("-") tomorrowStr[1] = tomorrowStr[1].length === 1 ? "0" + tomorrowStr[1] : tomorrowStr[1] tomorrowStr[2] = tomorrowStr[2].length === 1 ? "0" + tomorrowStr[2] : tomorrowStr[2] var afterTomorrow = GetDateStr(2)// 后天 var afterTomorrowStr = afterTomorrow.split("-") afterTomorrowStr[1] = afterTomorrowStr[1].length === 1 ? "0" + afterTomorrowStr[1] : afterTomorrowStr[1] afterTomorrowStr[2] = afterTomorrowStr[2].length === 1 ? "0" + afterTomorrowStr[2] : afterTomorrowStr[2] if (year === yesterdayStr[0] && month === yesterdayStr[1] && day === yesterdayStr[2]) { return "昨天" + " " + hour + ":" + minute } else if (year === todayStr[0] && month === todayStr[1] && day === todayStr[2]) { return "今天" + " " + hour + ":" + minute } else if (year === tomorrowStr[0] && month === tomorrowStr[1] && day === tomorrowStr[2]) { return "明天" + " " + hour + ":" + minute } else if (year === afterTomorrowStr[0] && month === afterTomorrowStr[1] && day === afterTomorrowStr[2]) { return "后天" + " " + hour + ":" + minute } else { return orignDate } } // 时间戳转化成时间格式 function timeFormat(timestamp) { var time = new Date(timestamp) var year = time.getFullYear() var month = time.getMonth() + 1 var date = time.getDate() var hours = time.getHours() var minutes = time.getMinutes() var seconds = time.getSeconds() return year + "-" + add0(month) + "-" + add0(date) + " " + add0(hours) + ":" + add0(minutes) + ":" + add0(seconds) } // 保证不出现个位数情况 function add0(m) { return m < 10 ? "0" + m : m } function GetDateStr(AddDayCount) { var dd = new Date() dd.setDate(dd.getDate() + AddDayCount) var y = dd.getFullYear() var m = dd.getMonth() + 1 var d = dd.getDate() return y + "-" + m + "-" + d }
文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。
转载请注明本文地址:https://www.ucloud.cn/yun/103160.html
摘要:更新今天又发现了一种简单的方法可以直接对年月日时分秒进行操作,假如今天那么所得昨天为昨天的时间前天的时间其中,函数为扩展函数。 1、时间格式化 1 //昨天的时间 2 var day1 = new Date(); 3 day1.setTime(day1.getTime()-24*60*60*1000); 4 var s1 = day1.getFullYear()+- + (da...
阅读 3172·2021-09-30 09:48
阅读 3448·2021-09-22 16:00
阅读 1034·2019-08-30 13:08
阅读 3080·2019-08-30 10:53
阅读 2393·2019-08-29 18:33
阅读 1558·2019-08-29 12:47
阅读 856·2019-08-29 12:16
阅读 1911·2019-08-26 12:02