JSFiddle - React, Tailwind, and code Playground

by amrendra kumar

HTML

<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.4/moment.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.4/moment.min.js"></script>

JavaScript

//console.log('m', moment())

const getStartAndEndDate = (duration) => {
	let startTime;
  let endTime;
		if(duration === 'yesterday') {
    	const prevDayObj = moment().subtract(1, 'd');
    	startTime = moment(prevDayObj).startOf('day').format()
      endTime = moment(prevDayObj).endOf('day').format()
    }
    
    if(duration === 'thisWeek') {
    	startTime = moment().startOf('week').format()
      endTime = moment().endOf('week').format()
    }
    
    if(duration === 'lastWeek') {
    	const lastWeekObj = moment().subtract(1, 'week')
      startTime = moment(lastWeekObj).startOf('week').format()
      endTime = moment(lastWeekObj).endOf('week').format()
    }
    
    if(duration === 'thisMonth') {
    	startTime = moment().startOf('month').format()
      endTime = moment().endOf('month').format()
    }
    
     
    if(duration === 'lastMonth') {
    	//const lastWeekObj = moment().startOf('month').subtract(1, 'd')
      const lastMonthObj = moment().subtract(1, 'month')
      startTime = moment(lastMonthObj).startOf('month').format()
      endTime = moment(lastMonthObj).endOf('month').format()
    }
    
    console.log(startTime)
    console.log(endTime)
}


getStartAndEndDate('lastWeek')