獲取從今天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/文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-401446.html
安裝
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)!