2013-05-28 88 views
1

如何在JavaScript中生成月份名稱(例如:10月/ 10月),還要操作它,以便提前15天實際的日期?使用JavaScript從日期獲取月份名稱,但使日期比實際日期提前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月01日

+0

你到底在哪裏卡住了?你必須從'd'獲得月份值,所以'month [d.getMonth()]''。沒有什麼特別複雜的,除非你自己不明白每個腳本。在這種情況下,您應該再次閱讀一些JS基礎知識:http://eloquentjavascript.net/。 –

回答

6

以下是您正在查找的樣本,

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/

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 
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(); 
} 
0

的toDateString()一致函數的格式,其中我們可以得到短月份名稱(六月,八月,九月...)通過使用SUBSTR返回日期()。

var date_obj = new Date(); 
date_obj.toDateString().substr(4, 3); 
相關問題