如何在Javascript或JQuery中获取上个月的最后一天 [英] How to get the last day of the previous month in Javascript or JQuery
本文介绍了如何在Javascript或JQuery中获取上个月的最后一天的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下代码来获取当天:
I have the following code to get the current day:
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);
}
这非常适合当天,但现在我需要得到上个月的最后一天,如果它是新月的开始。
This is great for getting the current day, but now i need to get the last day of the last month if it is the beginning of a new month.
现在这将产生:
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()
将反映出来。
now d
contains the last date of the previous month. d.getMonth()
and d.getDate()
will reflect it.
这应该适用于包装c time.h
的任何日期api。请参见此
This should work on any date api that wraps c time.h
. See this
这篇关于如何在Javascript或JQuery中获取上个月的最后一天的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文