看看这个。
//日期增加函数
function dateAdd(strInterval, NumDay, dtDate) {
var dtTmp = new Date(dtDate)
if (isNaN(dtTmp)) dtTmp = new Date()
switch (strInterval) {
case "s":return new Date(Date.parse(dtTmp) + (1000 * NumDay))
case "n":return new Date(Date.parse(dtTmp) + (60000 * NumDay))
case "h":return new Date(Date.parse(dtTmp) + (3600000 * NumDay))
case "d":return new Date(Date.parse(dtTmp) + (86400000 * NumDay))
case "w":return new Date(Date.parse(dtTmp) + ((86400000 * 7) * NumDay))
case "m":return new Date(dtTmp.getFullYear(), (dtTmp.getMonth()) + NumDay, dtTmp.getDate(), dtTmp.getHours(), dtTmp.getMinutes(), dtTmp.getSeconds())
case "y":return new Date((dtTmp.getFullYear() + NumDay), dtTmp.getMonth(), dtTmp.getDate(), dtTmp.getHours(), dtTmp.getMinutes(), dtTmp.getSeconds())
}
}
//将日期beginDate加上一天
//strDateList = dateAdd("d", 1, new Date()).toLocaleString()
strDateList = dateAdd("d", 1, '2012/09/28').toLocaleString()
另赠送你一个函数:
//| 求两个时间的天数差 日期格式为 YYYY-MM-dd
function daysBetween(DateOne,DateTwo)
{
var OneMonth = DateOne.substring(5,DateOne.lastIndexOf ('-'))
var OneDay = DateOne.substring(DateOne.length,DateOne.lastIndexOf ('-')+1)
var OneYear = DateOne.substring(0,DateOne.indexOf ('-'))
var TwoMonth = DateTwo.substring(5,DateTwo.lastIndexOf ('-'))
var TwoDay = DateTwo.substring(DateTwo.length,DateTwo.lastIndexOf ('-')+1)
var TwoYear = DateTwo.substring(0,DateTwo.indexOf ('-'))
var cha=((Date.parse(OneMonth+'/'+OneDay+'/'+OneYear)- Date.parse(TwoMonth+'/'+TwoDay+'/'+TwoYear))/86400000)
return Math.abs(cha)}
你好,
先说一年的月份,这个需要问吗?一年能出现13个月?所以:
var monthOfYear = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]// 或者中文,根据你的需求自己定
var monthOfYear = ['一月', '二月', '三月', '四月', '五月', '六月', '七月', '八月', '九月', '十月', '十一月', '十二月']
再来说获取最近三个月月份的事情:
// 先取到当前日期对象var date = new Date()
// 月份数组
var nearThree = []
// 当月
var month = date.getMonth()
// 现将本月推入数组
nearThree.push(monthOfYear[month])
// 最近一月逆推两个月
for (var i = 1 i <= 2 i++) {
date.setMonth(month - i)
nearThree.unshift(monthOfYear[date.getMonth()])
}
console.log(monthOfYear)
console.log(nearThree)
好了,希望能解决你的问题。