国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

JavaScript 獲取時(shí)間范圍(當(dāng)天,本月(30天間隔),momentJS基本使用)

這篇具有很好參考價(jià)值的文章主要介紹了JavaScript 獲取時(shí)間范圍(當(dāng)天,本月(30天間隔),momentJS基本使用)。希望對(duì)大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請(qǐng)大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問(wèn)。

獲取從今天0點(diǎn)到現(xiàn)在的時(shí)間范圍

這個(gè)比較簡(jiǎn)單,直接把同一天的時(shí)間數(shù)都換成0即可

const getNowDate = (isGetDate = false) => {
  let now = new Date();
  let y = now.getFullYear();
  let m = now.getMonth() + 1;
  let d = now.getDate();
  let h = now.getHours();
  let min = now.getMinutes();
  let s = now.getSeconds();
  m = m >= 10 ? m : '0' + m
  d = d >= 10 ? d : '0' + d
  h = h >= 10 ? h : '0' + h
  min = min >= 10 ? min : '0' + min
  s = s >= 10 ? s : '0' + s
  if (isGetDate) {
    return `${y}-${m}-${d} 00:00:00`;
  } else {
    return `${y}-${m}-${d} ${h}:${min}:${s}`;
  }
};
console.log(getTodayDuration(true))
console.log(getTodayDuration())

獲取今天到前一個(gè)月的時(shí)間范圍

這里也簡(jiǎn)單的,直接用時(shí)間戳來(lái)轉(zhuǎn)換就可以了

const getMonthDuration = (isToday = false) => {
  let now = new Date();
  let y = now.getFullYear();
  let m = now.getMonth() + 1;
  let d = now.getDate();
  let h = now.getHours();
  let min = now.getMinutes();
  let s = now.getSeconds();
  m = m >= 10 ? m : '0' + m
  d = d >= 10 ? d : '0' + d
  h = h >= 10 ? h : '0' + h
  min = min >= 10 ? min : '0' + min
  s = s >= 10 ? s : '0' + s
  let today = `${y}-${m}-${d} ${h}:${min}:${s}`;
  if (isToday) {
    return today;
  } else {
    let stampTime = new Date(today).getTime() - (30 * 24 * 3600 * 1000)
    let prveStamp = new Date(stampTime);
    let prveMonth = prveStamp.getMonth() + 1;
    let prveDate = prveStamp.getDate();
    let year = prveStamp.getFullYear();
    prveMonth = prveMonth >= 10 ? prveMonth : '0' + prveMonth
    prveDate = prveDate >= 10 ? prveDate : '0' + prveDate
    return `${year}-${prveMonth}-${prveDate} ${h}:${min}:${s}`
  }
}
console.log(getMonthDuration(true));
console.log(getMonthDuration());

momentJS(第三方庫(kù))

以上兩個(gè)方式是自己寫(xiě)的,如果需要也可以使用第三方庫(kù),項(xiàng)目中采用的也比較多

momentJS官方API地址:http://momentjs.cn/

安裝

npm install moment --save   # npm
yarn add moment             # Yarn

使用

以下示例代碼來(lái)自官方,可直接在官方地址查看文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-401446.html

日期格式化

moment().format('MMMM Do YYYY, h:mm:ss a'); // 三月 29日 2023, 3:30:46 下午
moment().format('dddd');                    // 星期三
moment().format("MMM Do YY");               // 3月 29日 23
moment().format('YYYY [escaped] YYYY');     // 2023 escaped 2023
moment().format();  

相對(duì)時(shí)間

moment("20111031", "YYYYMMDD").fromNow(); // 11 年前
moment("20120620", "YYYYMMDD").fromNow(); // 11 年前
moment().startOf('day').fromNow();        // 16 小時(shí)前
moment().endOf('day').fromNow();          // 8 小時(shí)內(nèi)
moment().startOf('hour').fromNow();       // 31 分鐘前

日歷時(shí)間

moment().subtract(10, 'days').calendar(); // 2023/03/19
moment().subtract(6, 'days').calendar();  // 上星期四15:30
moment().subtract(3, 'days').calendar();  // 上星期日15:30
moment().subtract(1, 'days').calendar();  // 昨天15:30
moment().calendar();                      // 今天15:30
moment().add(1, 'days').calendar();       // 明天15:30
moment().add(3, 'days').calendar();       // 下星期六15:30
moment().add(10, 'days').calendar();      // 2023/04/08

多語(yǔ)言支持

moment.locale();         // zh-cn
moment().format('LT');   // 15:30
moment().format('LTS');  // 15:30:46
moment().format('L');    // 2023/03/29
moment().format('l');    // 2023/3/29
moment().format('LL');   // 2023年3月29日
moment().format('ll');   // 2023年3月29日
moment().format('LLL');  // 2023年3月29日下午3點(diǎn)30分
moment().format('lll');  // 2023年3月29日 15:30
moment().format('LLLL'); // 2023年3月29日星期三下午3點(diǎn)30分
moment().format('llll'); // 2023年3月29日星期三 15:30

到了這里,關(guān)于JavaScript 獲取時(shí)間范圍(當(dāng)天,本月(30天間隔),momentJS基本使用)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來(lái)自互聯(lián)網(wǎng)用戶(hù)投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場(chǎng)。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請(qǐng)注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請(qǐng)點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

覺(jué)得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請(qǐng)作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包