js日期范围:本周/上周/本月/上月

用于日期范围选择器快捷项,忽略具体时间,包含今日/昨日/本周/上周/本月/上月。

/*** 今日* @returns {Date[]}*/
function today () {const now = new Date()return [now, now]
}/*** 昨日* @returns {Date[]}*/
function yesterday () {const now = new Date()const yesterday = new Date()yesterday.setTime(now.getTime() - 24 * 60 * 60 * 1000)return [yesterday, yesterday]
}/*** 本周* @returns {Date[]}*/
function thisWeek () {const monday = new Date()const now = new Date()const todayOfWeek = now.getDay() || 7monday.setTime(now.getTime() - (todayOfWeek - 1) * 24 * 60 * 60 * 1000)return [monday, now]
}/*** 上周* @returns {Date[]}*/
function lastWeek () {const monday = new Date()const sunday = new Date()const now = new Date()const todayOfWeek = now.getDay() || 7sunday.setTime(now.getTime() - todayOfWeek * 24 * 60 * 60 * 1000)monday.setTime(sunday.getTime() - 6 * 24 * 60 * 60 * 1000)return [monday, sunday]
}/*** 本月* @returns {Date[]}*/
function thisMonth () {const firstDay = new Date()const now = new Date()firstDay.setDate(1)return [firstDay, now]
}/*** 上月* @returns {Date[]}*/
function lastMonth () {const lastDay = new Date()lastDay.setDate(0)const firstDay = new Date(lastDay)firstDay.setDate(1)return [firstDay, lastDay]
}


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部