摘要:因?yàn)殚L(zhǎng)期的項(xiàng)目積累自己寫了一些計(jì)算時(shí)間的插件相比較我覺得比它好用補(bǔ)全時(shí)間月往後推幾個(gè)月格式化日期把時(shí)間戳只取日期部分精確到天把時(shí)間戳只取時(shí)間部分精確到秒把時(shí)間戳轉(zhuǎn)化為字符串形式精確到秒取月份第一天精確到天
因?yàn)殚L(zhǎng)期的項(xiàng)目積累,自己寫了一些計(jì)算時(shí)間的插件,相比較momoent,我覺得比它好用
const ONE_DAY = 86400000 const ensureLength = function (num) { // 補(bǔ)全時(shí)間YYY-MM-dd HH:MM:ss let nlen = num.toString().length num = nlen < 2 ? "0" + num : num return num } const isUndefined = function (s) { return s === undefined || s === null || s === "" } const _DateUtils = { parse (dateStr) { if (dateStr instanceof Date) { return dateStr } else if (/^d+$/.test(dateStr)) { return new Date(parseInt(dateStr, 10)) } else if (dateStr) { let dateString = dateStr.replace(/[-:]/g, ",").replace(/s+/g, ",") let dateArr = dateString.split(",") return new Date(dateArr[0], dateArr[1] - 1, dateArr[2], dateArr[3] || 0, dateArr[4] || 0, dateArr[5] || 0) } else if (!dateStr) { return dateStr } }, toDateString (date, tpl) { let locals if (isUndefined(date)) { date = new Date() } if (isUndefined(tpl)) { tpl = "yyyy-MM-dd" } date = this.parse(date) locals = { yyyy: date.getFullYear(), YYYY: date.getFullYear(), MM: ensureLength(date.getMonth() + 1), dd: ensureLength(date.getDate()), HH: ensureLength(date.getHours()), mm: ensureLength(date.getMinutes()), ss: ensureLength(date.getSeconds()) } return tpl.replace(/YYYY|yyyy|MM|dd|HH|mm|ss/g, function ($1) { return locals[$1] }) }, toTimeString (date, tpl) { let locals if (isUndefined(date)) { date = new Date() } if (isUndefined(tpl)) { tpl = "HH:mm:ss" } locals = { HH: ensureLength(date.getHours()), mm: ensureLength(date.getMinutes()), ss: ensureLength(date.getSeconds()) } return tpl.replace(/HH|mm|ss/g, function ($1) { return locals[$1] }) }, toDateTimeString (date) { date = date || new Date() date = this.parse(date) return this.toDateString(date) + " " + this.toTimeString(date) }, getFirstDate (date) { date = date || new Date() date = this.parse(date) let fitsrDate = new Date(date.getFullYear(), date.getMonth(), 1) return this.toDateString(fitsrDate) }, getLastDate (date) { date = this.parse(date) let nextMonthFirstDate = (new Date(date.getFullYear(), date.getMonth() + 1, 1)).getTime() let lastDate = new Date(nextMonthFirstDate - ONE_DAY) return this.toDateString(lastDate) }, toMonthString (date) { let month if (isUndefined(date)) { date = new Date() } date = this.parse(date) month = ensureLength(date.getMonth() + 1) return (date.getFullYear()) + "-" + month }, getPreMonth (date) { let month, year if (isUndefined(date)) { date = new Date() } date = this.parse(date) year = date.getFullYear() month = ensureLength(date.getMonth()) year = month === "00" ? year - 1 : year month = month === "00" ? "12" : month return year + "-" + month }, getPreMonthDay (today) { if (isUndefined(today)) { today = new Date() } return this.toDateString(today.setMonth(today.getMonth() - 1)) }, ago (cnt) { let day = new Date().getDate() return new Date().setDate(day - cnt) }, getCurrentWeek () { let now = new Date() let currentDay = now.getDay() let start = this.ago(currentDay - 1) let end = now return [start, end] }, getRecentMonths (cnt) { /* params{ cnt : "月",往後推幾個(gè)月 } */ if (isUndefined(cnt)) { cnt = 0 } cnt = parseInt(cnt) let now = new Date() let currentMonth = now.getMonth() let year = now.getFullYear() let start let agoMonth = currentMonth - cnt > 0 ? currentMonth - cnt : currentMonth - cnt + 12 let agoYear = currentMonth - cnt > 0 ? year : year - 1 start = new Date(agoYear, agoMonth, 1) let end = new Date() return [start, end] }, getCurrentQ () { let now = new Date() let currentMonth = now.getMonth() let start if (currentMonth < 3) { start = now.setMonth(0) } else if (currentMonth > 3 && currentMonth < 6) { start = now.setMonth(3) } else if (currentMonth > 2 && currentMonth < 6) { start = now.setMonth(6) } else if (currentMonth > 8 && currentMonth < 11) { start = now.setMonth(9) } start = new Date(start).setDate(1) let end = new Date() return [start, end] } } /* params:{ parse(格式化日期), toDateString(把時(shí)間戳只取日期部分,精確到天), toTimeString(把時(shí)間戳只取時(shí)間部分,精確到秒), toDateTimeString(把時(shí)間戳轉(zhuǎn)化為字符串形式,精確到秒), getFirstDate(取月份第一天,精確到天), getLastDate(取月份最后一天,精確到天), toMonthString(月份轉(zhuǎn)化為字符串), getPreMonth(取上月份,精確到月), getPreMonthDay(取上一月,精確到天), ago(前第n天,精確到天), getRecentMonths(最近幾個(gè)月,從1號(hào)開始) getCurrentQ(最近一個(gè)季度,從1號(hào)開始) } */ export default _DateUtils
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/94880.html
摘要:我相信這是很多人共同的感受,所以今天推薦的款書簽收藏夾插件印象筆記剪藏插件下載地址印象筆記推出的一款剪藏插件,可以一鍵收藏各類網(wǎng)頁圖文,并永久保存進(jìn)。 今天有個(gè)人問我chrome瀏覽器器上有沒有可以稍后閱讀的插件?。克鋵?shí)想問的就是書簽收藏夾插件,因?yàn)槲覀冊(cè)诨ヂ?lián)網(wǎng)上一不小心就會(huì)看到很多感興趣的內(nèi)容,但是時(shí)間有限暫時(shí)無法閱讀,以后保存下來有時(shí)間的時(shí)候再看。我相信這是很多人共同的感受,所以...
摘要:是一款緩存插件,可以為方法擴(kuò)展緩存功能。緩存清理插件本身會(huì)自動(dòng)清理過期緩存對(duì)于不想繼續(xù)使用緩存的接口可以為方法傳入清理當(dāng)前接口的緩存并返回最新數(shù)據(jù)也可以調(diào)用清理所有插件產(chǎn)生的緩存。 原文鏈接 AJAX-Cache是什么 Ajax是前端開發(fā)必不可少的數(shù)據(jù)獲取手段,在頻繁的異步請(qǐng)求業(yè)務(wù)中,我們往往需要利用緩存提升界面響應(yīng)速度,減少網(wǎng)絡(luò)資源占用。AJAX-Cache是一款jQuery緩存插件...
摘要:推薦插件安裝打開,按或者菜單打開命令窗口。配置中找到修改,可以額外添加注釋的內(nèi)容,我習(xí)慣使用某某使用方法回車即可代碼提示,不解釋效率快了很多插件代碼高亮代碼補(bǔ)全暫時(shí)先介紹這些,再有好玩的插件之后再來分享 sublime text3 推薦插件 Package Controller安裝 1、打開sublime text 3,按ctrl+~或者菜單View > Show Console打開命...
摘要:今天就來跟大家分享一下工作中用到的幾款插件。是一款功能強(qiáng)大的網(wǎng)頁調(diào)試與發(fā)送網(wǎng)頁請(qǐng)求的插件。俗稱油猴子,是一款功能非常強(qiáng)大的插件,他包含方便的腳本管理腳本概覽設(shè)置多樣性腳本自動(dòng)更新安全兼容性同步編輯器語法檢查快速開發(fā)卸載等功能。 ‘工欲善其事,必先利其器’。優(yōu)秀的開發(fā)者不僅體現(xiàn)在其在技術(shù)方面的精通,還體現(xiàn)在其對(duì)各種開發(fā)工具的充分了解與使用,這會(huì)讓其開發(fā)效率事半功倍。作為一個(gè)前端開發(fā)者,平...
閱讀 3050·2021-09-08 10:43
閱讀 1038·2019-08-30 15:53
閱讀 987·2019-08-30 13:51
閱讀 847·2019-08-29 14:03
閱讀 810·2019-08-26 18:35
閱讀 1241·2019-08-26 13:38
閱讀 1589·2019-08-26 10:34
閱讀 3505·2019-08-26 10:21