Javascript to determine whether the time is today, this week, this month

foreword

  Javascript judges whether the timestamp/time date string is in the three time ranges of today, this week, and this month.
  The level is limited, and the logic may be more cumbersome. If there is a better way, welcome to enlighten me.

  1. Determine whether it is today
    Pass parameters: param is timestamp (default) / datetime string, type is timestamp / datetime
    Example:
     todayJudge(1669961510000)
     todayJudge('2023-04-16 21:45:00', 'datetime')
todayJudge(param, type = 'timestamp') {
    
    
	// 如果传入的时间戳是字符串,需要进行类型转换
	if ((typeof param === 'string') && type == 'timestamp') {
    
    
		param = Number(param)
	}
	if (type == 'datetime') {
    
    	// ios日期时间兼容
		param = param.replace(/-/g, "/")
	}
	var currentStamp = new Date().setHours(0,0,0,0)		// 当天日期,转换为时间部分为0的时间戳
	var paramStamp = new Date(param).setHours(0,0,0,0)	// 传入时间戳,将时间部分转换为0
	// 若两个时间戳相等,说明传入的时间戳即今天
	if (currentStamp == paramStamp) {
    
    
		return true
	}
	return false
},
  1. Determine whether it is in this week
    Pass parameters: param is timestamp (default) / datetime string, type is timestamp / datetime
    Example:
     weekJudge(1669961510000)
     weekJudge('2023-04-16 21:45:00', 'datetime')
weekJudge(param, type = 'timestamp') {
    
    
	// 如果传入的时间戳是字符串,需要进行类型转换
	if ((typeof param === 'string') && type == 'timestamp') {
    
    
		param = Number(param)
	}
	if (type == 'datetime') {
    
    	// ios日期时间兼容
		param = param.replace(/-/g, "/")
	}
	var paramStamp = new Date(param).setHours(0,0,0,0)				// 传入的时间戳,将时间部分转换为0
	var currentDate = new Date()									// 当天
	var currentStamp = currentDate.setHours(0,0,0,0)				// 当天日期,转换为时间部分为0的时间戳
	var currentDay = currentDate.getDay() == 0 ? 7 : currentDate.getDay()	// 当天周几,取值为(0-6),0为周日,转换为7
	console.log('当天周:', currentDay, currentDate)
	var dayTime = 24*60*60*1000										// 一天的毫秒数
	var MondayStamp = currentStamp - (currentDay - 1) * dayTime		// 本周周一初时间戳(本周一的0点)
	console.log('本周周一:', new Date(MondayStamp))
	var SundayStamp = currentStamp + (8 - currentDay) * dayTime		// 本周周日末时间戳(下周一的0点)
	console.log('下周周一:', new Date(SundayStamp))
	// 当传入的时间戳满足[本周一0点,下周一0点) ,说明传入的时间在本周内
	if (paramStamp >= MondayStamp && paramStamp < SundayStamp) {
    
    
		return true
	}
	return false
},
  1. Determine whether it is in the current month
    Pass parameters: param is timestamp (default) / datetime string, type is timestamp / datetime
    Example:
     monthJudge(1669961510000)
     monthJudge('2023-04-16 21:45:00', 'datetime')
monthJudge(param, type = 'timestamp') {
    
    
	// 如果传入的时间戳是字符串,需要进行类型转换
	if ((typeof param === 'string') && type == 'timestamp') {
    
    
		param = Number(param)
	}
	if (type == 'datetime') {
    
    	// ios日期时间兼容
		param = param.replace(/-/g, "/")
	}
	var paramDate = new Date(param)						// 传入的参数,转换为date类型
	var currentDate = new Date()						// 当天
	// 当传入的时间戳转换为日期后,此时的年和月与当天的年和月均相等,说明处于当月
	if (paramDate.getFullYear() == currentDate.getFullYear() && paramDate.getMonth() == currentDate.getMonth()) {
    
    
		return true
	}
	return false
},

Guess you like

Origin blog.csdn.net/qq_45580300/article/details/130188826