1

我如何在 JavaScript 中生成月份名称(例如:Oct/October),但还要对其进行操作,使其比实际日期提前 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 日

4

4 回答 4

7

这是您正在寻找的样本,

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/

于 2013-05-28T15:03:55.363 回答
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
于 2013-05-28T15:06:18.793 回答
1

toDateString() 函数以某种格式返回日期,我们可以通过使用 substr() 获得短月份名称(Jun, Aug, Sep ...)。

var date_obj = new Date();
date_obj.toDateString().substr(4, 3);
于 2017-05-31T11:19:02.900 回答
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();
}
于 2015-11-15T05:25:57.393 回答