2017-03-31 72 views
-2

我有以下代碼,我可以得到第二天,但問題是最好的例子,如果它是最後一天它不會跳到例如今天下月月31日將進入32我如何得到以下格式的一天後,月,日,年(3月31日)

下面是代碼我有:

var objToday = new Date(), 
      weekday = new Array('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'), 
      dayOfWeek = weekday[objToday.getDay()], 
      domEnder = function() { var a = objToday; if (/1/.test(parseInt((a + "").charAt(0)))) return ""; a = parseInt((a + "").charAt(1)); return 1 == a ? "" : 2 == a ? "" : 3 == a ? "" : "" }(), 
      dayOfMonth = today + (objToday.getDate() < 10) ? '' + objToday.getDate() + domEnder : objToday.getDate() + domEnder, 
      months = new Array('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'), 
      curMonth = months[objToday.getMonth()], 
      nextMonth = months[objToday.getMonth() + 1], 
      curYear = objToday.getFullYear(), 
      curHour = objToday.getHours() > 12 ? objToday.getHours() - 12 : (objToday.getHours() < 10 ? "0" + objToday.getHours() : objToday.getHours()), 
      curMinute = objToday.getMinutes() < 10 ? "0" + objToday.getMinutes() : objToday.getMinutes(), 
      curSeconds = objToday.getSeconds() < 10 ? "0" + objToday.getSeconds() : objToday.getSeconds(), 
      curMeridiem = objToday.getHours() > 12 ? "PM" : "AM"; 
var today = ab + ":" + curMinute + "." + curSeconds + curMeridiem + " " + dayOfWeek + " " + dayOfMonth + " of " + curMonth + ", " + curYear; 


dayOfMonth_formatted = parseInt(dayOfMonth); 

currentDay_formatted = curMonth + " " + dayOfMonth + 1 + "," + " " + curYear; 
tomorrow_formatted = curMonth + " " + dayOfMonth_tomorrow + "," + " " + curYear; 
+2

[JavaScript中,得到了第二天的日期(http://stackoverflow.com/questions/23081158/javascript-get-date-of-the-next-day)的可能的複製 –

+0

你爲什麼會嘗試在將**格式化爲字符串之後遞增日期**,而不是在**之前**。 –

+0

[在JavaScript中增加日期]的可能重複(http://stackoverflow.com/questions/3674539/incrementing-a-date-in-javascript) –

回答

0

我強烈建議使用https://momentjs.com/,它會做幾乎你想做的一切,更清潔,但更重要的是更易於管理。

moment(new Date()).add(1,'days'); 
相關問題