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

标签 javascript date

我如何在 JavaScript 中生成月份名称(例如:Oct/October)并对其进行操作,使其比实际日期提前 15 天?

我找到了 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 日

最佳答案

这是您正在寻找的样本,

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

http://jsfiddle.net/UXy8V/1/

关于javascript - 使用 JavaScript 从 Date 获取月份名称,但日期比实际日期提前 15 天,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16795277/

相关文章:

javascript - 为什么这个 JS for-in 循环只记录属性的键,而不是整个属性?

javascript - 如何使用 PHP 或 JS 通过 HttpOnly 删除 cookie

R:为一天中的时间数据分配正确的日期

java - 在 java 中将日期格式化为特定格式的字符串时,时间会发生变化

JavaScript 到 JQuery; MySQL 的格式化日期

javascript - QueryDocumentSnapshot 似乎缺少其父类的方法

javascript - 如何在 JavaScript 中检测互联网连接是否已关闭或域/URL 是否不存在?

mysql - 在 laravel 5.0 中使用 FLUENT 的 datetime 列的日期值

Java比较日期以检查是否在范围内

php - 我无法使用日期获取 PHP mysql select