前端近一周近一个月近三个月近半年近一年js代码

本文介绍了五个JavaScript函数,用于获取近一周、一个月、三个月、半年和一年的日期范围,帮助开发者处理时间相关的需求。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

// 近一周

export function getOneWeek() {

    var now = new Date();

    var year = now.getFullYear();

    var month = now.getMonth() + 1;//0-11表示1-12月

    var day = now.getDate();

    var dateObj = {};

    dateObj.now = year + '-' + month + '-' + day;

    if (day - 7 <= 0) {   //如果在当月7日之前

        var lastMonthDay = new Date(year, (parseInt(month) - 1), 0).getDate();    //1周前所在月的总天数

        if (month - 1 <= 0) { //如果在当年的1月份

            dateObj.last = (year - 1) + '-' + 12 + '-' + (31 - (7 - day));

        } else {

            dateObj.last = year + '-' + (month - 1) + '-' + (lastMonthDay - (7 - day));

        }

    } else {

        dateObj.last = year + '-' + month + '-' + (day - 7);

    }

    return dateObj;

}

// 近一个月

export function getOneMonth() {

    var end = new Date();

    var year = end.getFullYear();

    var month = end.getMonth() + 1 < 10 ? `0${end.getMonth() + 1}` : end.getMonth() + 1

    var day = end.getDate() > 9 ? end.getDate() : '0' + end.getDate();

    var dateObj = {};

    dateObj.now = year + '-' + month + '-' + day;

    var endMonthDay = new Date(year, month, 0).getDate();

    if (month - 1 <= 0) {

        dateObj.last = (year - 1) + '-' + 12 + '-' + day;

    } else {

        var startMonthDay = new Date(year, (parseInt(month) - 1), 0).getDate();

        if (startMonthDay < day) {

            if (day < endMonthDay) {

                dateObj.last = year + '-' + (month - 1) + '-' + (startMonthDay - (endMonthDay - day));

            } else {

                dateObj.last = year + '-' + (month - 1) + '-' + startMonthDay;

            }

        } else {

            dateObj.last = year + '-' + ((month - 1) < 10 ? `0${(month - 1)}` : (month - 1)) + '-' + day;

        }

    }

    return dateObj

}

// 近三个月

export function getThreeMounth() {

    var end = new Date();

    var year = end.getFullYear();

    var month = end.getMonth() + 1 < 10 ? `0${end.getMonth() + 1}` : end.getMonth() + 1

    var day = end.getDate() > 9 ? end.getDate() : '0' + end.getDate();

    var dateObj = {};

    dateObj.now = year + '-' + month + '-' + day;

    var endMonthDay = new Date(year, month, 0).getDate();    //当前月的总天数

    if (month - 3 <= 0) { //如果是1、2、3月,年数往前推一年

        var start3MonthDay = new Date((year - 1), (12 - (3 - parseInt(month))), 0).getDate();    //3个月前所在月的总天数

        if (start3MonthDay < day) {    //3个月前所在月的总天数小于现在的天日期

            dateObj.last = (year - 1) + '-' + (12 - (3 - month)) + '-' + start3MonthDay;

        } else {

            dateObj.last = (year - 1) + '-' + (12 - (3 - month)) + '-' + day;

        }

    } else {

        var start3MonthDay = new Date(year, (parseInt(month) - 3), 0).getDate();    //3个月前所在月的总天数

        if (start3MonthDay < day) {    //3个月前所在月的总天数小于现在的天日期

            if (day < endMonthDay) {        //当前天日期小于当前月总天数,2月份比较特殊的月份

                dateObj.last = year + '-' + (month - 3) + '-' + (start3MonthDay - (endMonthDay - day));

            } else {

                dateObj.last = year + '-' + (month - 3) + '-' + start3MonthDay;

            }

        } else {

            dateObj.last = year + '-' + ((month - 3) < 10 ? `0${(month - 3)}` : (month - 3)) + '-' + day;

        }

    }

    return dateObj

}

// 近半年

export function getHalfYear() {

    var now = new Date();

    var year = now.getFullYear();

    var month = now.getMonth() + 1;//0-11表示1-12月

    var day = now.getDate();

    var dateObj = {};

    dateObj.now = year + '-' + month + '-' + day;

    // 先获取当前时间

    var curDate = (new Date()).getTime()

    // 将半年的时间单位换算成毫秒

    var halfYear = 365 / 2 * 24 * 3600 * 1000

    // 半年前的时间(毫秒单位)

    var pastResult = curDate - halfYear

    // 日期函数,定义起点为半年前

    var pastDate = new Date(pastResult)

    var pastYear = pastDate.getFullYear()

    var pastMonth = pastDate.getMonth() + 1

    var pastDay = pastDate.getDate()

    if (pastMonth >= 1 && pastMonth <= 9) {

        pastMonth = '0' + pastMonth

    }

    if (pastDay >= 0 && pastDay <= 9) {

        pastDay = '0' + pastDay

    }

    dateObj.last = pastYear + '-' + pastMonth + '-' + pastDay

    return dateObj

}

// 近一年

export function getOneYear() {

    var nowDate = new Date()

    var dates = new Date(nowDate)

    var year = dates.getFullYear();

    var month = dates.getMonth() + 1;//0-11表示1-12月

    var day = dates.getDate();

    var dateObj = {};

    dateObj.now = year + '-' + month + '-' + day;

    dates.setDate(dates.getDate() - 365)

    var seperator1 = '-'

    var year = dates.getFullYear()

    var month = dates.getMonth() + 1

    var strDate = dates.getDate()

    if (month >= 1 && month <= 9) {

        month = '0' + month

    }

    if (strDate >= 0 && strDate <= 9) {

        strDate = '0' + strDate

    }

    dateObj.last = year + seperator1 + month + seperator1 + strDate

    return dateObj

}

### MYSQL 查询一年一周的数据 在 MySQL 中,可以通过日期函数 `CURDATE()` `DATE_SUB()` 来实现对时间范围的查询。以下是针对一年一周数据的具体查询方法: #### 1. 查询一年的数据 通过 `DATE_SUB()` 函数可以计算出当前日期的一年前日期,然后与目标字段进行比较: ```sql SELECT * FROM table_name WHERE date_column >= DATE_SUB(CURDATE(), INTERVAL 1 YEAR); ``` 此查询将返回所有在过去一年内的记录[^2]。 #### 2. 查询的数据 同样使用 `DATE_SUB()` 函数,但设置的时间间隔为一个月: ```sql SELECT * FROM table_name WHERE date_column >= DATE_SUB(CURDATE(), INTERVAL 1 MONTH); ``` 此查询将返回所有在过去一个月内的记录[^3]。 #### 3. 查询一周的数据 对于一周的数据,时间间隔设置为七天: ```sql SELECT * FROM table_name WHERE date_column >= DATE_SUB(CURDATE(), INTERVAL 7 DAY); ``` 此查询将返回所有在过去七天内的记录[^1]。 #### 注意事项 - 确保 `date_column` 是个有效的日期或时间类型字段。 - 如果需要包含当天的数据,可以将条件设置为 `date_column BETWEEN DATE_SUB(CURDATE(), INTERVAL n UNIT) AND CURDATE()`,其中 `n` 表示时间间隔,`UNIT` 可以是 `YEAR`, `MONTH`, `DAY` 等。 ### 示例代码 假设有个表 `orders`,其中包含个 `order_date` 字段,以下为具体查询示例: #### 查询一年的数据 ```sql SELECT * FROM orders WHERE order_date >= DATE_SUB(CURDATE(), INTERVAL 1 YEAR); ``` #### 查询的数据 ```sql SELECT * FROM orders WHERE order_date >= DATE_SUB(CURDATE(), INTERVAL 1 MONTH); ``` #### 查询一周的数据 ```sql SELECT * FROM orders WHERE order_date >= DATE_SUB(CURDATE(), INTERVAL 7 DAY); ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值