簡體   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