This is the 8th day of my participation in the August More text Challenge. For details, see: August More Text Challenge

Write in front:

  • This article as my study summary of the use, at the same time to share with you ~
  • Personal front-end blog site: zhangqiang.hk. Cn
  • Welcome to join the front-end learning QQ communication group of the blogger: : 706947563, focus on the front-end development, learning and progress together!

In daily development, the need to acquire time is essential, it can be said that often appear, this is not, recently in contact with several projects there are several places in the need to acquire time. In the following article, react+ ANTD will be used as the technology stack. The moment plug-in is mainly used for second encapsulation to show an encapsulated function. On the basis of the existing method of the moment plug-in, the methods of several time periods [last week, last month, last quarter and last year] will be added, so as to take and use:

Moment const areDataReckon = (type: string) => {let myData: any = [], year = moment().year(), month = moment().month(), quarter = moment().quarter(), firstDay = 0, lastDay = 0; /** * According to foreign calculation methods, obtain the time of last Monday and this Sunday, If (type === 'last week ') {myData * moment().year() get the current year return value number * moment().month() get the month return value number range 0-11 */ // last week if (type ===' last week ') {myData = [ moment(moment().day(-6).format('YYYY-MM-DD'), 'YYYY-MM-DD'), moment(moment().day(0).format('YYYY-MM-DD'), 'yyyY-mm-dd ')]} // last month else if (type ===' last month ') {// console.log("firstDay, lastDay:", firstDay, lastDay); // month = 2; switch (month) { case 0: year -= 1; month = 12; firstDay = 1; lastDay = 31; myData = [ moment(`${year}-${month}-0${firstDay}`, 'YYYY-MM-DD'), moment(`${year}-${month}-${lastDay}`, 'YYYY-MM-DD') ]; break; case 11: firstDay = 1; lastDay = 30; myData = [ moment(`${year}-${month}-0${firstDay}`, 'YYYY-MM-DD'), moment(`${year}-${month}-${lastDay}`, 'YYYY-MM-DD') ]; break; case 10: firstDay = 1; lastDay = moment(`${year}-${month}`, "YYYY-MM").daysInMonth(); / / get the current total number of days in myData = [moment (` ${year} - ${month} - 0 ${firstDay} `, '- DD YYYY - MM), moment (` ${year} - ${month} - ${lastDay} `, 'YYYY-MM-DD') ]; break; default: firstDay = 1; lastDay = moment(`${year}-0${month}`, "YYYY-MM").daysInMonth(); / / get the current total number of days in myData = [moment (` ${year} ${month} - 0 0 ${firstDay} `, '- DD YYYY - MM), moment (` ${year} ${month} - 0 - ${lastDay} `, 'YYYY-MM-DD') ]; break; Elseif (type === 'last quarter ') {switch (quarter) {case 1: year -= 1; firstDay = 1; lastDay = moment(`${year}-12`, "YYYY-MM").daysInMonth(); myData = [ moment(`${year}-09-0${firstDay}`, 'YYYY-MM-DD'), moment(`${year}-12-${lastDay}`, 'YYYY-MM-DD') ]; break; case 2: firstDay = 1; lastDay = moment(`${year}-03`, "YYYY-MM").daysInMonth(); myData = [ moment(`${year}-01-0${firstDay}`, 'YYYY-MM-DD'), moment(`${year}-03-${lastDay}`, 'YYYY-MM-DD') ]; break; case 3: firstDay = 1; lastDay = moment(`${year}-06`, "YYYY-MM").daysInMonth(); myData = [ moment(`${year}-04-0${firstDay}`, 'YYYY-MM-DD'), moment(`${year}-06-${lastDay}`, 'YYYY-MM-DD') ]; break; case 4: firstDay = 1; lastDay = moment(`${year}-09`, "YYYY-MM").daysInMonth(); myData = [ moment(`${year}-07-0${firstDay}`, 'YYYY-MM-DD'), moment(`${year}-09-${lastDay}`, 'YYYY-MM-DD') ]; break; }} else if (type = 'year') {myData = [moment (` ${my moment () year () - 1} - 01-01 `, '- DD YYYY - MM), moment(`${moment().year()-1}-12-31`, 'YYYY-MM-DD') ]; } // console.log(" string time: ", moment(myData[0]). Format ('YYYY MM DD')); return myData }Copy the code

At this point, if you want to retrieve the time period of last week (from the first day of last week to the last day of last week), you can call it as follows (if you want to call anything else, you can just replace ‘last week’ with ‘last week’) :

The console. The log (areDataReckon (' last week)); AreDataReckon (' last week ') will return directly to last week's time slot.Copy the code

Moment.js documentation http://momentjs.c…

Thanks for seeing the end! Code word is not easy, like is the biggest support oh!

\