.net或js判断所给时间是不是月末

JavaScript06

.net或js判断所给时间是不是月末,第1张

是不是月末很简单,不需要搞的那么复杂,把给出的时间加上一天,然后再获取这个新时间的月份,如果等于之前那个时间的月份,就不是月末,以当前时间为例

string str=DateTime.Now.AddDays(1).Month == DateTime.Now.Month ? "不是月末" : "是月末"

JS应该有相同的做法吧

思路:

相加的日期先分别转换成毫秒数,然后再相加

将加得的毫秒数再转换成日期

下面是简单的代码,仅供参考:

<script>

    var date1 = new Date('2015-10-10')

    var date2 = new Date('2015-10-11')

    alert(new Date(date1.getTime() + date2.getTime())) //Mon Jul 18 2061 16:00:00 GMT+0800 (中国标准时间)

</script>

【代码】

// 计算距离指定日期的日期

function date_diff(date, diff)

{

if(typeof date=='object' &&date.constructor==Date)

{

if(isNaN(diff=parseInt(diff)))

return false

var MS_OF_A_DAY=24*60*60*1000,

time_start=Date.UTC(date.getFullYear(), date.getMonth(), date.getDate()),

time_diff=time_start+diff*MS_OF_A_DAY,

date=new Date()

date.setTime(time_diff)

return date

}

return false

}

【示例】

// 今天

// Thu Feb 28 2013 08:00:00 GMT+0800 (中国标准时间)

// 明天

var tomorrow=date_diff(new Date(), 1)

alert(tomorrow)// Fri Mar 01 2013 08:00:00 GMT+0800 (中国标准时间)

// 昨天

var yesterday=date_diff(new Date(), -1)

alert(yesterday)// Wed Feb 27 2013 08:00:00 GMT+0800 (中国标准时间)