gpt4 book ai didi

javascript - 如何在javascript中检查日期是在本周还是当月还是下个月?

转载 作者:行者123 更新时间:2023-11-29 20:07:36 25 4
gpt4 key购买 nike

我有一些 friend 的生日,想把他们分开如下:

  • 本周内的生日(从当天开始的本周剩余天数内)。
  • 生日在当月(从当天开始的当月剩余天数内)。
  • 下个月的生日。

所以我只想知道如何在 javascript 中测试每个日期,看看它是否在本周/当前月份/下个月的剩余天数内。

注意:假设我有这些日期,格式为 m/d/Y(06/29/1990)。

谢谢

最佳答案

将您的日期和当前时间转换为 Date对象并将其用于比较。一些干编码:

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 - 如何在javascript中检查日期是在本周还是当月还是下个月?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11392186/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com