js 获取据当前时间n天前的时间

<script type="text/javascript">
        function getLastDate() {
            var date = new Date();
            var month = date.getMonth() + 1;
            var day = date.getDate();
            var hh = date.getHours();
            var mm = date.getMinutes();
            var ss = date.getSeconds();
            var year = date.getFullYear();
            alert(year);
           
            var ym;
            var year1;
            day = day - 10;
            if (day <= 0) {
                day = mygetday(day, month, year);
                var months = mygetmonth(month);
                ym = months.split(",");
                year1 = ym[0];
                day = FormatDate(day);
                month = FormatDate(ym[1]);
                return year1 + "-" + month + "-" + day + "&nbsp;" + hh + ":" + mm + ":" + ss;
            }
            else {
                return year + "-" + month + "-" + day + "&nbsp;" + hh + ":" + mm + ":" + ss;
            }

        }

        // 获取某一天的日期
        function mygetday(day, month, year) {
            var daynum = 30;
            if (month == 2 || month == 4 || month == 6 || month == 8 || month == 9
            || month == 1 || month == 11) {
                daynum = 31;
            } else if (month == 3) {
                if (year % 4 == 0) {
                    daynum = 29;
                } else {
                    daynum = 28;
                }
            }
            day = daynum + day;
            return day;
        }
        // 获取某一天的月份
        function mygetmonth(month) {
            var year = new Date().getFullYear();
            if (month == 1) {
                month = 12;
                year = year - 1;
            } else {
                month = month - 1;
            }
            return year + "," + month;
        }
        function FormatDate(d) {
            if (d.toString().length == 1) {
                d = "0" + d;
            }
            return d;
        }
        $(function () {
            $('#birthday').datetimebox('setValue', getLastDate());
        });

    </script>


 <script type="text/javascript">
        function getLastDate() {
            var date = new Date(); //获取当前时间
            var month = date.getMonth() + 1;
            var day = date.getDate();
            var hh = date.getHours();
            var mm = date.getMinutes();
            var ss = date.getSeconds();
            var year = date.getFullYear();
            alert(year);
           
            var ym;
            var year1;
            day = day - 10; //获取十天前的时间
            if (day <= 0) {
                day = mygetday(day, month, year);
                var months = mygetmonth(month);
                ym = months.split(",");
                year1 = ym[0];
                day = FormatDate(day);
                month = FormatDate(ym[1]);
                return year1 + "-" + month + "-" + day + "&nbsp;" + hh + ":" + mm + ":" + ss;
            }
            else {
                return year + "-" + month + "-" + day + "&nbsp;" + hh + ":" + mm + ":" + ss;
            }

        }

        // 获取某一天的日期
        function mygetday(day, month, year) {
            var daynum = 30;
            if (month == 2 || month == 4 || month == 6 || month == 8 || month == 9
            || month == 1 || month == 11) {
                daynum = 31;
            } else if (month == 3) {
                if (year % 4 == 0) {
                    daynum = 29;
                } else {
                    daynum = 28;
                }
            }
            day = daynum + day;
            return day;
        }
        // 获取某一天的月份
        function mygetmonth(month) {
            var year = new Date().getFullYear();
            if (month == 1) {
                month = 12;
                year = year - 1;
            } else {
                month = month - 1;
            }
            return year + "," + month;
        }
        function FormatDate(d) {
            if (d.toString().length == 1) {
                d = "0" + d;
            }
            return d;
        }
//测试填充文本框
        $(function () {
            $('#birthday').datetimebox('setValue', getLastDate());
        });

    </script>
根据引用\[2\]中的代码,可以使用`new Date()`获取当前时间,并通过加减处理时间戳获取所需的时间。要获取当前时间时分秒再加15,可以使用以下代码: ```javascript let nowDate = new Date(); // 获取当前时间 nowDate.setDate(nowDate.getDate() + 15); // 加15 let hours = nowDate.getHours(); // 获取小时 let minutes = nowDate.getMinutes(); // 获取分钟 let seconds = nowDate.getSeconds(); // 获取秒钟 console.log(hours + ':' + minutes + ':' + seconds); // 输出当前时间时分秒再加15 ``` 这段代码会输出当前时间加上15后的时分秒。请注意,这里的时间是基于当前系统时间的。 #### 引用[.reference_title] - *1* [JS日期、年月日、时分秒](https://blog.csdn.net/weixin_43852916/article/details/115183132)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [【js获取当前时间后n后n个月(时分秒年月日都可)](https://blog.csdn.net/qq_39147299/article/details/125639376)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [elementUi时间选择器,开始时间不得大于结束时间限制到时分秒](https://blog.csdn.net/weixin_43936040/article/details/119645220)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值