欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

js獲取今天、昨天、明天的日期函數(shù)代碼

 更新時(shí)間:2023年05月13日 08:30:21   作者:辰漪  
這篇文章主要介紹了js獲取今天、昨天、明天的日期函數(shù)代碼,需要的朋友可以參考下

今天發(fā)現(xiàn)的一個(gè)比較好的函數(shù)

/* 
  * @params date 日期
  * @params type 日期 prev/current/next 昨天/今天/明天
  * @params fmt 日期拼接符
*/
function getDays(date, type, fmt) {
    let currentDate = new Date(date)
    let y = currentDate.getFullYear()
    let m = currentDate.getMonth() + 1
    let d = currentDate.getDate()
    function dateFormat(date, fmt) {
        let y = new Date(date).getFullYear()
        let m = new Date(date).getMonth() + 1
        let d = new Date(date).getDate()
        return `${y}${fmt}${m}${fmt}$vvxyksv9kd`
    }
    switch (type) {
        case "prev":
            if (d - 1 < 1) {
                if (m - 1 < 1) {
                    y = y - 1
                    m = 12
                } else {
                    m = m - 1
                }
                d = new Date(y, m, 0).getDate()
            } else {
                d = d - 1
            }
            break
        case "current":
            break
        case "next":
            if (d + 1 > new Date(y, m, 0).getDate()) {
                if (m + 1 > 12) {
                    y = y + 1
                    m = 1
                    d = 1
                } else {
                    m = m + 1
                    d = 1
                }
            } else {
                d = d + 1
            }
            break;
    default:
      break;
    }
    return dateFormat(new Date(`${y}-${m}-$vvxyksv9kd`), fmt)
}
console.log(getDays(new Date('2023-5-13'), "prev", "-"));
console.log(getDays(new Date('2023-5-30'), "next", "-"));
console.log(getDays(new Date('2023-5-31'), "next", "-"));

再補(bǔ)充一個(gè)js 日期 獲取今天、昨天、明天的函數(shù)

  function getDay(day){
    var today = new Date()
    // 獲取時(shí)間戳(毫秒級(jí))
    /*
      day為1,則是,明天的時(shí)間戳
      day為-1,則是,昨天的時(shí)間戳
      day為-2,則是,前天的時(shí)間戳
    */
    var targetday_milliseconds = today.getTime() + 1000 * 60 * 60 * 24 * day
    // Date.setTime(時(shí)間戳):設(shè)置當(dāng)前日期的時(shí)間
    today.setTime(targetday_milliseconds)
    console.log('today=', today) // today= Sun Mar 05 2023 16:14:56 GMT+0800 (中國標(biāo)準(zhǔn)時(shí)間)
    var tYear = today.getFullYear() // 年
    var tMonth = today.getMonth() // 月
    var tDate = today.getDate() // 日
    tMonth = this.doHandleMonth(tMonth + 1)
    tDate = this.doHandleMonth(tDate)
    console.log('返回年月日=', tYear + '-' + tMonth + '-' + tDate)
    return tYear + '-' + tMonth + '-' + tDate
  }
  function doHandleMonth(month) {
    var m = month
    if (month.toString().length == 1) {
      m = '0' + month
    }
    return m
  }

到此這篇關(guān)于js獲取今天、昨天、明天的日期函數(shù)代碼的文章就介紹到這了,更多相關(guān)js獲取明天的日期內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論