争渡,争渡,惊起一滩鸥鹭

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
  124 随笔 :: 0 文章 :: 0 评论 :: 97547 阅读

 

复制代码
let currentDate = "2019-04";
currentDate = new Date(currentDate); // 将日期格式转换为 Mon Apr 01 2019 08:00:00 GMT+0800 (中国标准时间)
 
// 月份加一
let lastDate = currentDate.setMonth(currentDate.getMonth() - 1); // 输出日期格式为毫秒形式1551398400000
 
lastDate = new Date(lastDate);
let lastYear = lastDate.getFullYear();
let lastMonth = checkMonth(lastDate.getMonth() + 1); // 因日期中的月份表示为0-11,所以要显示正确的月份,需要 + 1
 
lastDate = lastYear + '-' + lastMonth; // "2019-03"
 
 
// 月份减一
let nextDate = currentDate.setMonth(currentDate.getMonth() + 1); // 输出日期格式为毫秒形式1556668800000
 
nextDate = new Date(nextDate);
let nextYear = nextDate.getFullYear();
let nextMonth = checkMonth(nextDate.getMonth() + 1); // 因日期中的月份表示为0-11,所以要显示正确的月份,需要 + 1
 
nextDate = nextYear + '-' + nextMonth; // "2019-05"
复制代码

 

其中,checkMonth 函数的作用是将单位数的月份前面加 ‘0’,比如:‘7’ 变成 ‘07’,代码如下所示:

 

checkMonth (i) {
    if (i<10){
        i="0" + i;
    }
    return i;
}

 

参考文档:https://blog.csdn.net/Dora_5537/article/details/89308860

 

posted on   争渡~  阅读(7659)  评论(0编辑  收藏  举报
编辑推荐:
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
阅读排行:
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
点击右上角即可分享
微信分享提示