成人国产在线小视频_日韩寡妇人妻调教在线播放_色成人www永久在线观看_2018国产精品久久_亚洲欧美高清在线30p_亚洲少妇综合一区_黄色在线播放国产_亚洲另类技巧小说校园_国产主播xx日韩_a级毛片在线免费

資訊專欄INFORMATION COLUMN

JS根據(jù)時(shí)間戳 或 yyyy-MM-dd HH:mm:ss來(lái)獲取前天、今天、明天、后天等

寵來(lái)也 / 2176人閱讀

摘要:時(shí)間戳或由于項(xiàng)目有多個(gè)后端人員,難免會(huì)出現(xiàn)返回時(shí)間樣式不統(tǒng)一現(xiàn)象,但前端顯示又要統(tǒng)一,先上代碼毫秒只需調(diào)用此函數(shù)判斷是否是時(shí)間戳昨天今天明天后天昨天今天明天后天時(shí)間戳轉(zhuǎn)化成時(shí)間格式保證不出現(xiàn)個(gè)位數(shù)情況

時(shí)間戳或yyyy-MM-dd HH:mm:ss

由于項(xiàng)目有多個(gè)后端人員,難免會(huì)出現(xiàn)返回時(shí)間樣式不統(tǒng)一現(xiàn)象,但前端顯示又要統(tǒng)一,先上代碼

// params => "2019-04-02 17:11:55" OR 1554257391000 (毫秒) 只需調(diào)用此函數(shù)
function formatRecentTime(params) {
  var date = ""
  if (isNaN(params)) { // 判斷是否是時(shí)間戳
    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
  }
}

// 時(shí)間戳轉(zhuǎn)化成時(shí)間格式
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)
}

// 保證不出現(xiàn)個(gè)位數(shù)情況
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
}

文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/103160.html

相關(guān)文章

  • js獲取當(dāng)前時(shí)間(昨天、今天、明天)

    摘要:更新今天又發(fā)現(xiàn)了一種簡(jiǎn)單的方法可以直接對(duì)年月日時(shí)分秒進(jìn)行操作,假如今天那么所得昨天為昨天的時(shí)間前天的時(shí)間其中,函數(shù)為擴(kuò)展函數(shù)。 1、時(shí)間格式化 1 //昨天的時(shí)間 2 var day1 = new Date(); 3 day1.setTime(day1.getTime()-24*60*60*1000); 4 var s1 = day1.getFullYear()+- + (da...

    rainyang 評(píng)論0 收藏0
  • 前端時(shí)間處理小結(jié)

    摘要:相信,幾乎每個(gè)前端項(xiàng)目都不可避免地要接觸到時(shí)間處理,最最常見(jiàn)的就是時(shí)間格式化。中,內(nèi)置對(duì)象封裝了時(shí)間處理方法。 相信,幾乎每個(gè)前端項(xiàng)目都不可避免地要接觸到時(shí)間處理,最最常見(jiàn)的就是時(shí)間格式化。JS中,內(nèi)置對(duì)象Date封裝了時(shí)間處理方法。但說(shuō)實(shí)話,這個(gè)對(duì)象方法太多,而且平時(shí)業(yè)務(wù)開(kāi)發(fā)中也很少會(huì)直接用到這些方法,所以我總是對(duì)Date對(duì)象感覺(jué)到陌生!最近對(duì)時(shí)間處理作了下小結(jié),用此文來(lái)記錄一下。 ...

    Hydrogen 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

最新活動(dòng)
閱讀需要支付1元查看
<