本文介绍了如何在javascript中检查日期在当前星期或当前月或下个月之内?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一些朋友的生日,想按如下方式分开他们:
I have some friends' birthdays and want to separate them as follows :
- 本周内的生日(从当日开始的本周剩余天数内).
- 当月内的生日(从当日开始的当月剩余天数内).
- 下个月的生日.
所以我想知道如何在javascript中测试每个日期,以查看它是否在当前一周/当前月份/下个月的剩余日期之内.
So all I want to know how to test each date in javascript to see if it falls within the remaining days of the current week/current month/next month.
N.B:说我有m/d/Y(1990年6月29日)格式的日期.
N.B: say I have those dates in m/d/Y(06/29/1990) format.
谢谢
推荐答案
将日期和当前时间转换为 Date
对象,并将其用于比较.一些干式编码:
Convert your date and current time to Date
object and use it for comparison. Some dry coding:
var now = new Date()
if (
(check.getFullYear() == now.getFullYear()) &&
(check.getMonth() == now.getMonth()) &&
(check.getDate() >= now.getDate())
) {
// remanining days in current month and today. Use > if you don't need today.
}
var nextMonth = now.getMonth() + 1
var nextYear = now.getFullYear()
if (nextMonth == 12) {
nextMonth = 0
nextYear++
}
if (
(check.getFullYear() == nextYear) &&
(check.getMonth() == nextMonth)
) {
// any day in next month. Doesn't include current month remaining days.
}
var now = new Date()
now.setHours(12)
now.setMinutes(0)
now.setSeconds(0)
now.setMilliseconds(0)
var end_of_week = new Date(now.getTime() + (6 - now.getDay()) * 24*60*60*1000 )
end_of_week.setHours(23)
end_of_week.setMinutes(59)
end_of_week.setSeconds(59) // gee, bye-bye leap second
if ( check >=now && check <= end_of_week) {
// between now and end of week
}
这篇关于如何在javascript中检查日期在当前星期或当前月或下个月之内?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!