繁体   English   中英

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

[英]How to get the last day of the previous month in Javascript or JQuery

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

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

谢谢

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。

这对我来说非常有效:

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

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

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

试试这个

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

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

只需从月份中减去一个:

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

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

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; 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM