Js中时间比较

1、用Date.parse(str)转化为时间戳比较适用:精确到秒

注:返回1970-1-1距指定日期的毫秒数

<scripttype="text/javascript">
varstarttime="2013-09-1400:00:00"

vararrstime=starttime.substr(0,10).split('-')
starttime=arrstime[1]+"/"+arrstime[2]+"/"+arrstime[0]+starttime.substr(10,9)
varendtime="2013-10-1423:59:59"
vararretime=endtime.substr(0,10).split('-')
endtime=arretime[1]+"/"+arretime[2]+"/"+arretime[0]+endtime.substr(10,9)

varnow=newDate()
varyear=now.getFullYear()
varmonth=now.getMonth()
varday=now.getDate()
varhour=now.getHours()

varminutes=now.getMinutes()

varseconds=now.getSeconds()
now=month+"/"+day+"/"+year+""+hour+":"+minutes+":"+seconds
vars=Date.parse(starttime)
vare=Date.parse(endtime)
varn=Date.parse(now)
alert(starttime)
alert(endtime)
alert(now)

if(s-n>0)
{
alert('s大')
}
else
{
alert('n大')
}
if(e-n>0)
{
alert('e大')
}
else
{
alert('n大')
}

</script>

2、用setFullYear(year,month,day)转化为时间类型比较后两个参数可缺省适用:精确到天

注:返回调整过的日期的毫秒表示

<scripttype="text/javascript">


varmyDate=newDate();

myDate.setFullYear(2013,9,13);


vartoday=newDate();

if(myDate>today)

{

alert("Todayisbefore17thOct2008");

}

if(myDate=today)

{

alert("相等")

}

if(myDate<today)

{

alert("Todayisafter17thOct2008");

}

</script>

3、同转化为时间类型比较

注:Date.parse()参数的格式支持2种:yyyy/MM/dd和MM/dd/yyyy

<scripttype="text/javascript">

varstrsTime="2013-10-14";
varsdate=newDate(Date.parse(strsTime.replace(/-/g,"/")));
varstreTime="2013-11-15";
varedate=newDate(Date.parse(streTime.replace(/-/g,"/")));

varnow=newDate(Date.parse(('2013-10-14').replace(/-/g,"/")));
alert(now)
if(sdate<=now&&edate>now)
{
alert('now介于2013-10-14与2013-11-14之间')
}
elseif(sdate>now)
{
alert('now小于2013-10-14')
}
else
{
alert('now大于2013-11-14')
}
</script>


你可能感兴趣的:(Date.parse())