Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
selenium ide javascript中的日期格式,如2013年9月2日_Javascript_Date - Fatal编程技术网

selenium ide javascript中的日期格式,如2013年9月2日

selenium ide javascript中的日期格式,如2013年9月2日,javascript,date,Javascript,Date,我有JavaScript以dd-mmm-yyyy格式返回日期 var d=new Date(); var my_month=new Date(); var month_name=new\nArray(12); month_name[0]="Jan";\nmonth_name[1]="Feb"; month_name[2]="Mar";\nmonth_name[3]="Apr"; month_name[4]="May";\nmonth_name[5]="Jun"; month_name[6]="J

我有JavaScript以dd-mmm-yyyy格式返回日期

var d=new Date();
var my_month=new Date();
var month_name=new\nArray(12);
month_name[0]="Jan";\nmonth_name[1]="Feb"; month_name[2]="Mar";\nmonth_name[3]="Apr"; month_name[4]="May";\nmonth_name[5]="Jun"; month_name[6]="Jul";\nmonth_name[7]="Aug"; month_name[8]="Sep";\nmonth_name[9]="Oct"; month_name[10]="Nov";\nmonth_name[11]="Dec";
d.getDate()+'-'+\n((month_name[my_month.getMonth()]))+'-'+d.getFullYear();
我在SeleniumIDE中使用它来格式化当前日期,它工作正常


但我想将当前日期格式化为2013年9月2日。如何为不同日期引入st、nd、rd和th以获得此格式???

我修改了您的代码如下:

var d=new Date();
var month_name=new Array(12);
month_name[0]="January";
month_name[1]="February";
month_name[2]="March";
month_name[3]="April";
month_name[4]="May";
month_name[5]="June";
month_name[6]="July";
month_name[7]="August";
month_name[8]="September";
month_name[9]="October";
month_name[10]="November";
month_name[11]="December";
var month_day = d.getDay() + 1;
var Nth = "th";
if(month_day === 1 || month_day === 21 || month_day === 31){
    Nth = "st";
}
else if(month_day === 2 || month_day === 22){
    Nth = "nd";
}
else if(month_day === 3 || month_day === 23){
    Nth = "rd";
}
alert(month_day+Nth+' day of '+(month_name[d.getMonth()])+' '+d.getFullYear());
要在JSFIDLE中查看代码,.

getDay()应该是getDate(),并丢失+1。getDay()为您提供一周中的第0-6天