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
使用JavaScript从日期获取月份名称,但要使日期比实际日期提前15天_Javascript_Date - Fatal编程技术网

使用JavaScript从日期获取月份名称,但要使日期比实际日期提前15天

使用JavaScript从日期获取月份名称,但要使日期比实际日期提前15天,javascript,date,Javascript,Date,我如何在JavaScript中生成月份名称,例如:Oct/十月,但同时还要对其进行操作,使其比实际日期提前15天 我找到了两个不错的脚本,但无法将这两个脚本组合在一起 <html> <head> <title>Combine Date Values</title> </head> <body> <script language="JavaScript" type="text/javascript"> <!-

我如何在JavaScript中生成月份名称,例如:Oct/十月,但同时还要对其进行操作,使其比实际日期提前15天

我找到了两个不错的脚本,但无法将这两个脚本组合在一起

<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日

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);

这是你要找的样品

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);

请参阅代码中的注释以了解发生的情况

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

请参阅代码中的注释以了解发生的情况

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

toDateString函数以一种格式返回日期,我们可以从中获取短月份名称Jun、Aug、Sep。。。通过使用substr

var date_obj = new Date();
date_obj.toDateString().substr(4, 3);

toDateString函数以一种格式返回日期,我们可以从中获取短月份名称Jun、Aug、Sep。。。通过使用substr

var date_obj = new Date();
date_obj.toDateString().substr(4, 3);

你到底被困在哪里?您必须从d获取月份值,因此months[d.getMonth]。它没有什么特别复杂的地方,除非你不理解每个脚本本身。在这种情况下,您应该再次阅读一些JS基础知识:。您到底被困在哪里?您必须从d获取月份值,因此months[d.getMonth]。它没有什么特别复杂的地方,除非你不理解每个脚本本身。在这种情况下,您应该再次阅读一些JS基础知识:。