如何在JavaScript中生成月份名称(例如:10月/ 10月),还可以操作它以使其比实际日期提前15天?
我找到了2个漂亮的脚本,但无法将2个组合在一起。
<html>
<head>
<title>Combine Date Values</title>
</head>
<body>
<script language="JavaScript" type="text/javascript">
<!--
var months = new Array(12);
months[0] = "January";
months[1] = "February";
months[2] = "March";
months[3] = "April";
months[4] = "May";
months[5] = "June";
months[6] = "July";
months[7] = "August";
months[8] = "September";
months[9] = "October";
months[10] = "November";
months[11] = "December";
var current_date = new Date();
month_value = current_date.getMonth();
day_value = current_date.getDate();
year_value = current_date.getFullYear();
document.write("The current date is " + months[month_value] + " " +
day_value + ", " + year_value);
//-->
</script>
</body>
</html>
^^^^以上将显示当前日期^^^^
<script type="text/javascript">
Date.prototype.addDays = function(days) {
this.setDate(this.getDate()+days);
}
var d = new Date();
d.addDays(15);
var curr_date = d.getDate();
var curr_month = d.getMonth();
curr_month++;
var curr_year = d.getFullYear();
document.write(curr_month + "/" + curr_date + "/" + curr_year);
</script>
^^^^这将显示提前15天的日期^^^^
所以最终结果应该是这样的。 示例1 实际日期:2013年5月28日 显示日期:2013年6月12日
示例2 实际日期:2013年5月15日 显示日期:2013年5月30日
示例3 实际日期:2013年5月16日 显示日期:2013年6月1日
答案 0 :(得分:6)
以下是您要查找的样本,
var months = new Array(12);
months[0] = "January";
months[1] = "February";
months[2] = "March";
months[3] = "April";
months[4] = "May";
months[5] = "June";
months[6] = "July";
months[7] = "August";
months[8] = "September";
months[9] = "October";
months[10] = "November";
months[11] = "December";
var current_date = new Date();
current_date.setDate(current_date.getDate() + 15);
month_value = current_date.getMonth();
day_value = current_date.getDate();
year_value = current_date.getFullYear();
document.write("The current date is " + months[month_value] + " " + day_value + ", " + year_value);
答案 1 :(得分:3)
请参阅代码中的注释,了解正在发生的事情。
var months = [ "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" ]; // Store month names in array
var d = new Date(); // Create date object with current date
d.setDate(d.getDate() + 15); // Add 15 days to date object
var month = months[d.getMonth()]; // Get month value (Jan = 0, Feb = 1, .etc) then get month string from array
答案 2 :(得分:0)
var monthNames = ["January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"
];
var monthShortNames = ["Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
];
// d = your date
// a = your additional days
function dateFormat1(d, a){
var t = new Date(d);
t.setDate(t.getDate()+a);
return t.getDate()+' '+monthNames[t.getMonth()]+', '+t.getFullYear();
}
function dateFormat2(d, a){
var t = new Date(d);
t.setDate(t.getDate()+a);
return t.getDate()+' '+monthShortNames[t.getMonth()]+', '+t.getFullYear();
}
答案 3 :(得分:0)
toDateString()函数以一种格式返回日期,我们可以使用substr()获取短月份名称(Jun,Aug,Sep ...)。
var date_obj = new Date();
date_obj.toDateString().substr(4, 3);