如何在Javascript或JQuery中获取上个月的最后一天

时间:2012-02-01 17:51:15

标签: javascript jquery date

我有以下代码来获取当天:

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

由于

7 个答案:

答案 0 :(得分:20)

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。见this

答案 1 :(得分:18)

这对我很有用:

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

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

答案 2 :(得分:1)

从日期中减去一个:

 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();

这对每个月的任何一天都有用。

答案 3 :(得分:0)

var lastdayoflastmonth = new Date();
lastdayoflastmonth.setMonth(lastdayoflastmonth.getMonth(), 0);
var firstdayoflastmonth = new Date();
firstdayoflastmonth.setDate(1);
firstdayoflastmonth.setMonth(firstdayoflastmonth.getMonth()-1);

答案 4 :(得分:0)

尝试

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

答案 5 :(得分:0)

从我得到的答案来看,他们中的大多数人都未能在 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

答案 6 :(得分:-2)

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;