jquery比较时间

gentlemanhua 2016-05-10

<script type="text/javascript">
 $(function () {

---------------------------------比较时间的大小-------------------------------------------------

 var cha = (Date.parse("2010/1/14 9:00:00") - Date.parse("2010/1/12 8:00:00")) / 86400000 * 24;
 if (cha > 0) {
 alert(cha);
 }

---------------------------------比较时间的大小-------------------------------------------------


alert(daysBetween("2010-1-13 8:00:00", "2010-1-12 6:00:00"));

---------------------------------得到当前时间-------------------------------------------------

var date = new Date();
now = date.getFullYear() + "/";
now = now + (date.getMonth() + 1) + "/";  //取月的时候取的是当前月-1如果想取当前月+1就可以了
now = now + date.getDate() + " ";
now = now + date.getHours() + ":";
now = now + date.getMinutes() + ":";
now = now + date.getSeconds() + "";
alert(now);

---------------------------------得到当前时间-------------------------------------------------

-----------得到一个区域的时间,然后计算出时间差,并显示到指定的位置---------------------------
$(".cuttime").each(function () {
var txt = $(this).text();
var endtime = txt.substring(0, txt.indexOf('至'));
var starttime = txt.substring(txt.lastIndexOf('至') + 1);
var cuttime = (Date.parse(starttime) - Date.parse(endtime)) / 86400000 * 24;
alert(cuttime);
$(this).next("td").text(cuttime);
});

-----------得到一个区域的时间,然后计算出时间差,并显示到指定的位置---------------------------

alert(changetime("2010-1-12 8:00:00"))

});

//计算两个时间的时间差,根据你需要的样式进行修改

//比如2011$1$1,把“-”改成“$”
function daysBetween(DateOne,DateTwo)  
{
var OneMonth = DateOne.substring(5,DateOne.lastIndexOf ('-'));

//计算两个时间相差几天时用的语句
//var OneDay = DateOne.substring(DateOne.length,DateOne.lastIndexOf ('-')+1);
var OneDay = DateOne.substring(DateOne.lastIndexOf('-') + 1, DateOne.indexOf(' '));
var OneYear = DateOne.substring(0, DateOne.indexOf('-'));
var OneHouse = DateOne.substring(DateOne.indexOf(' ') + 1, DateOne.indexOf(':'));  
  
var TwoMonth = DateTwo.substring(5,DateTwo.lastIndexOf ('-'));
var TwoDay = DateTwo.substring(DateTwo.lastIndexOf('-') + 1, DateTwo.indexOf(' '));
var TwoYear = DateTwo.substring(0, DateTwo.indexOf('-'));
var TwoHouse = DateTwo.substring(DateTwo.indexOf(' ') + 1, DateTwo.indexOf(':'));

//计算两个时间相差几个小时
var cha = ((Date.parse(OneMonth + '/' + OneDay + '/' + OneYear) - Date.parse(TwoMonth + '/' + TwoDay + '/' + TwoYear)) / 86400000 * 24 + (OneHouse - TwoHouse));

//计算两个时间相差几天
//var cha = Date.parse(OneMonth + '/' + OneDay + '/' + OneYear) - Date.parse(TwoMonth + '/' + TwoDay + '/' + TwoYear)/ 86400000;             
return Math.abs(cha);
}

//把2012-1-1 12:00:00转换成2012/1/1 12:00:00

function changetime(time) {
var Month = time.substring(5, time.lastIndexOf('-'));
var Day = time.substring(time.lastIndexOf('-') + 1, time.indexOf(' '));
var Year = time.substring(0, time.indexOf('-'));
var clock = time.substring(time.indexOf(' ') + 1);
return Year + '/' + Month + '/' + Day + " " + clock;
}

</script>

<body>
<table>
<tr><td class="cuttime">2010/1/14 12:00:00至2010/1/15 13:00:00</td><td></td></tr>
<tr><td class="cuttime">2010/1/14 9:00:00至2010/1/15 8:00:00</td><td></td></tr>
<tr><td class="cuttime">2010/1/14 9:00:00至2010/1/15 8:00:00</td><td></td></tr>
</table>
</body>

注意:记得引用jquery

相关推荐

ELEMENTS爱乐冬雨 / 0评论 2020-05-29