使用JavaScript从“日期"获取月份名称,但日期要比实际日期早15天 [英] Get month name from Date using JavaScript but so that the date is 15 days ahead of actual date
问题描述
我如何在JavaScript中生成月份名称(例如:十月/十月),又如何对其进行操作,使其比实际日期提前15天?
How can i generate the Month name (e.g: Oct/October) in JavaScript but also manipulate it so that it is 15 days ahead of the actual date?
我找到了2个不错的脚本,但无法将2个脚本结合在一起.
I have found 2 nice scripts but cannot combine the 2 together.
<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>
^^^^上面将显示当前日期^^^^
^^^^ the above will display the current date ^^^^
<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天的日期^^^^
^^^^ this will now display a date which is 15 days ahead^^^^
所以最终结果应该是这样的.示例1 实际日期:2013年5月28日显示日期:2013年6月12日
So the end result should be something like this. EXAMPLE 1 Actual Date: 28 MAY 2013 Displayed Date: 12 JUNE 2013
示例2 实际日期:2013年5月15日显示日期:2013年5月30日
EXAMPLE 2 Actual Date: 15 MAY 2013 Displayed Date: 30 MAY 2013
示例3 实际日期:2013年5月16日显示日期:2013年6月1日
EXAMPLE 3 Actual Date: 16 MAY 2013 Displayed Date: 01 JUNE 2013
推荐答案
这是您要查找的示例,
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);
这篇关于使用JavaScript从“日期"获取月份名称,但日期要比实际日期早15天的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!