【问题标题】:how to get first date and last date from month and year in javascript如何在javascript中获取月份和年份的第一个日期和最后一个日期
【发布时间】:2021-12-07 21:07:17
【问题描述】:

谁能帮我找到月份和年份的第一个日期和最后一个日期。 我有 2 个参数月和年,例如月 = 10 和年 = 2021

我尝试了一些代码,但没有答案

var prevMonthLastDay = new Date(2021, 10, 0);
var thisMonthFirstDay = new Date(2021, 10, 1);
console.log(prevMonthLastDay);
console.log(thisMonthFirstDay);

以上代码给出以下输出:

2021-10-30T17:00:00.000Z
2021-10-31T17:00:00.000Z 

我的预期答案是第一次约会2021-10-01 和最后一次约会2021-10-31

【问题讨论】:

  • 月份索引为零,因此第一个表达式将给出 11 月的第 0 天,即 10 月的最后一天。但是因为您的时区似乎是 -7,UTC 日期(注意时间戳末尾的 Z)是前一天的 17:00(下午 5 点),所以您得到的是 10 月 30 日。第二个表达式只返回 11 月 1 日(因为月份是零索引),所以再次减去 7 小时得到 UTC,你会得到 10 月 31 日的 17:00。

标签: javascript date


【解决方案1】:

尝试以下操作以获得预期的输出:

function GetFirstAndLastDate(year, month)
{
var firstDayOfMonth = new Date(year, month-1, 2);
var lastDayOfMonth = new Date(year, month, 1);

console.log('First Day: ' + firstDayOfMonth.toISOString().substring(0, 10));
console.log('Last Day: ' + lastDayOfMonth.toISOString().substring(0, 10));

}

GetFirstAndLastDate(2021, 10);

【讨论】:

    【解决方案2】:

    你可以试试这个方法

    var date = new Date();
    var firstDay = new Date(date.getFullYear(), date.getMonth(), 1);
    var lastDay = new Date(date.getFullYear(), date.getMonth() + 1, 0);
    

    【讨论】:

    • 你能解释一下我有参数月份和年份示例月份 = 10 和 2021 年吗
    • 如果答案是在另一个链接中给出的,那么这是重复的,您应该将其标记为重复,而不是回答。
    【解决方案3】:

    我对您真正想要引用变量名的内容有点困惑,因此我将它们全部包含在内:

    const currentYear = 2021;
    const currentMonth = 10;
    
    const prevMonthFirstDay = new Date(currentYear, currentMonth - 2, 1);
    const prevMonthLastDay = new Date(currentYear, currentMonth - 1, 0);
    
    const currentMonthFirstDay = new Date(currentYear, currentMonth - 1, 1);
    const currentMonthLastDay = new Date(currentYear, currentMonth, 0);
    
    const nextMonthFirstDay = new Date(currentYear, currentMonth, 1);
    const nextMonthLastDay = new Date(currentYear, currentMonth + 1, 0);
    
    console.log('prev month first day:', prevMonthFirstDay);
    console.log('prev month last day:', prevMonthLastDay);
    console.log('current month first day:', currentMonthFirstDay);
    console.log('current month last day:', currentMonthLastDay);
    console.log('next month first day:', nextMonthFirstDay);
    console.log('next month last day:', nextMonthLastDay);
    

    在我的系统上,这将给出:

    prev month first day: Wed Sep 01 2021 00:00:00 GMT+0700 (Western Indonesia Time)
    prev month last day: Thu Sep 30 2021 00:00:00 GMT+0700 (Western Indonesia Time)
    current month first day: Fri Oct 01 2021 00:00:00 GMT+0700 (Western Indonesia Time)
    current month last day: Sun Oct 31 2021 00:00:00 GMT+0700 (Western Indonesia Time)
    next month first day: Mon Nov 01 2021 00:00:00 GMT+0700 (Western Indonesia Time)
    next month last day: Tue Nov 30 2021 00:00:00 GMT+0700 (Western Indonesia Time)
    

    所以现在只需要格式化:

    console.log(currentMonthFirstDay.toLocaleDateString('en-GB').split('/').reverse().join('-'));
    

    这会给你:

    2021-10-01
    

    【讨论】:

      【解决方案4】:

      你可以关注这个。

      //first and last of the current month
      var current_month = "April 2017";
      var arrMonth = current_month.split(" ");
      var first_day = new Date(arrMonth[0] + " 1 " + arrMonth[1]);
      
      //even though I already have the values, I'm using date functions to get year and month
      //because month is zero-based
      var last_day = new Date(first_day.getFullYear(), first_day.getMonth() + 1, 0, 23, 59, 59);
      
      //use moment,js to format       
      var start = moment(first_day).format("YYYY-MM-DD");
      var end = moment(last_day).format("YYYY-MM-DD");
      

      答案在以下链接中: How to get last day of the month

      【讨论】:

        【解决方案5】:

        你可以试试这个referrence

        var date = new Date(), y = date.getFullYear(), m = date.getMonth();
        var firstDay = new Date(y, m, 1);
        var lastDay = new Date(y, m + 1, 0);
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2013-01-06
          • 2014-06-26
          • 1970-01-01
          • 1970-01-01
          • 2016-10-10
          • 1970-01-01
          • 2016-06-17
          相关资源
          最近更新 更多