som*_*ool 10 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);
}
Run Code Online (Sandbox Code Playgroud)
这对于获得当天来说非常有用,但现在我需要在上个月的最后一天获得它,如果它是新月的开始.
现在这会产生:
Feb12012 2012_2_1
Run Code Online (Sandbox Code Playgroud)
但我需要的输出是:
Jan312012 2012_1_31
Run Code Online (Sandbox Code Playgroud)
谢谢
Shi*_*dim 25
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.
Run Code Online (Sandbox Code Playgroud)
现在d包含上个月的最后一个日期.d.getMonth()并且d.getDate()将反映它.
这应该适用于包装c的任何日期api time.h.看到这个
小智 22
这对我很有用:
var date = new Date();
date.setDate(0);
Run Code Online (Sandbox Code Playgroud)
date现在包含上个月的最后一天.