【问题标题】:How to get the last day of the previous month in Javascript or JQuery如何在Javascript或JQuery中获取上个月的最后一天
【发布时间】:2012-02-01 17:51:15
【问题描述】:

我有以下代码来获取当前日期:

var month=new Array(12);
month[0]="January";
month[1]="February";
month[2]="March";
month[3]="April";
month[4]="May";
month[5]="June";
month[6]="July";
month[7]="August";
month[8]="September";
month[9]="October";
month[10]="November";
month[11]="December";

var d = new Date();
var curr_date;
if (d.getDate() == 1)
{
    curr_date = d.getDate();
}
else
{
    curr_date = d.getDate() - 1;
}

var curr_month = d.getMonth() + 1; //months are zero based
var curr_year = d.getFullYear();
var message_day = curr_year + "_" + curr_month + "_" + curr_date;
var sql_day = month[curr_month - 1].substring(0,3) + curr_date.toString() + curr_year.toString();

if (curr_date == "1")
    {
         document.write(sql_day + " " + message_day);
    }

这对于获取当前日期非常有用,但现在如果是新月份的开始,我需要获取上个月的最后一天。

现在这将产生:

Feb12012 2012_2_1

但我需要输出的是:

Jan312012 2012_1_31

谢谢

【问题讨论】:

    标签: javascript jquery date


    【解决方案1】:
    var d=new Date(); // current date
    d.setDate(1); // going to 1st of the month
    d.setHours(-1); // going to last hour before this date even started.
    

    现在d 包含上个月的最后日期。 d.getMonth()d.getDate() 会反映出来。

    这应该适用于包含 c time.h 的任何日期 api。

    【讨论】:

    • 比油还滑! :-)
    • 简单就是性感!
    • 我刚刚使用了这个解决方案,它运行良好,但如果您要使用 ISO 格式进行输出,则需要注意时区。我不得不将我所有的小时、分钟和秒都归零,以确保在转换为 ISO 格式时它显示正确的日期......
    • 设置当月的第 0 天你得到上个月的最后一天。
    【解决方案2】:

    这对我来说非常适合:

    var date = new Date();
    date.setDate(0);
    

    日期现在包含上个月的最后一天。

    【讨论】:

    • Missing d.setHours(-1); // 到这个日期开始前的最后一个小时。
    • 当上个月的最后一个日期是 31 时,这个解决方案对我来说失败了
    【解决方案3】:

    从我得到的答案中,他们中的大多数人在 5 月未能捕捉到 31 个,所以我尝试了几个选项,这些都是我想出的。希望,有些人可能会觉得这很有用。

    const today = new Date()
    const lastMonth = today.getMonth() === 0 ? 11 : today.getMonth() - 1
    const year = lastMonth === 0 ? today.getFullYear - 1 : today.getFullYear
    
    const lastDayOfLastMonth = new Date(year, lastMonth + 1, 0).getDate()
    const lastDateTimeOfLastMonth = new Date(year, lastMonth + 1, 1)
    const lastDateOfLastMonth = new Date(year, lastMonth + 1, 0).toLocaleDateString()
    
    console.log(lastDayOfLastMonth) // eg 31
    console.log(lastDateTimeOfLastMonth) // eg 2021-05-31T16:00:00.000Z
    console.log(lastDateOfLastMonth) // eg 31/05/2021
    

    【讨论】:

      【解决方案4】:
      var lastdayoflastmonth = new Date();
      lastdayoflastmonth.setMonth(lastdayoflastmonth.getMonth(), 0);
      var firstdayoflastmonth = new Date();
      firstdayoflastmonth.setDate(1);
      firstdayoflastmonth.setMonth(firstdayoflastmonth.getMonth()-1);
      

      【讨论】:

        【解决方案5】:
        const monthNames = ["January", "February", "March", "April", "May", "June","July", "August", "September", "October", "November", "December"];                                       
        let today = new Date();
        let dd = today.getDate();
        
        let mm = today.getMonth()+1;
        let yyyy = today.getFullYear();
        if(dd<10) 
        {
            dd='0'+dd; 
        } 
        if(mm<10) 
        {
            mm='0'+mm;
        } 
        dd = new Date(yyyy, mm-1, 0).getDate(); 
        
        let month = '';                     
        mm == 1 ? month = monthNames[monthNames.length - 1] : month = monthNames[mm-2];                                         
        mm == 1 ? yyyy = yyyy - 1 : '';                                                                                 
        
        let lastDay = month+'-'+dd+'-'+yyyy; 
        

        【讨论】:

          【解决方案6】:

          只需从月份中减去一个:

           var today = new Date();
           var yesterday = new Date().setDate(today.getDate() - 1);
          

          如果“日期”属性(日期)为 1,则将其设置为零将为您提供表示上个月最后一天的日期。你的代码已经很接近了;你可以构造一个新的 Date 实例:

           if (d.getDate() === 1)
              curr_date = new Date().setDate(0).getDate();
          

          事实上,你甚至不需要“if”语句:

           curr_date = new Date().setDate( d.getDate() - 1 ).getDate();
          

          这将适用于一个月中的任何一天。

          【讨论】:

          【解决方案7】:

          试试这个

          function dateLastMonth() {
                      var date = new Date();
                      date.setDate(0);
                      return date.getDate().toString() + '/' + date.getMonth().toString() + '/' + date.getFullYear().toString();
                  }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2022-12-17
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多