使用 JavaScript 从 Date 获取月份名称,但使日期比实际日期早 15 天
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16795277/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get month name from Date using JavaScript but so that the date is 15 days ahead of actual date
提问by user2429067
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?
我如何在 JavaScript 中生成月份名称(例如:Oct/October),但还要对其进行操作,使其比实际日期早 15 天?
I have found 2 nice scripts but cannot combine the 2 together.
我找到了 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>
^^^^ 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>
^^^^ this will now display a date which is 15 days ahead^^^^
^^^^ 这将显示提前 15 天的日期^^^^
So the end result should be something like this. EXAMPLE 1Actual Date: 28 MAY 2013 Displayed Date: 12 JUNE 2013
所以最终的结果应该是这样的。 示例 1实际日期:2013 年 5 月 28 日显示日期:2013 年 6 月 12 日
EXAMPLE 2Actual Date: 15 MAY 2013 Displayed Date: 30 MAY 2013
示例 2实际日期:2013 年 5 月 15 日显示日期:2013 年 5 月 30 日
EXAMPLE 3Actual Date: 16 MAY 2013 Displayed Date: 01 JUNE 2013
示例 3实际日期:2013 年 5 月 16 日显示日期:2013 年 6 月 1 日
回答by Chamika Sandamal
Here is the sample you are looking for,
这是您正在寻找的样品,
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);
回答by George Reith
See comments in code for what's going on.
请参阅代码中的注释以了解正在发生的事情。
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
回答by M.A.K. Ripon
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();
}
回答by GarryOne
The toDateString() function returns date in a format, of which we can get the short month name (Jun, Aug, Sep ...) by using substr().
toDateString() 函数以某种格式返回日期,我们可以通过使用 substr() 获取短月份名称(Jun、Aug、Sep ...)。
var date_obj = new Date();
date_obj.toDateString().substr(4, 3);