Let's go

时间处理

一丶计时器

 1             timer = window.setInterval(function () {
 2                 if (IsStopTime) {
 3                     clearInterval(timer);
 4                 } else {
 5                     if (maxtime > 0) {
 6                         var minutes = Math.floor(maxtime / 60);
 7                         var seconds = Math.floor(maxtime % 60);
 8 
 9                         var minuteStr = "" + minutes;
10                         if (minutes < 10) {
11                             minuteStr = "0" + minuteStr;
12                         }
13 
14                         var secondStr = "" + seconds;
15                         if (seconds < 10) {
16                             secondStr = "0" + secondStr;
17                         }
18 
19                         var timespanStr = minuteStr + ":" + secondStr;
20 
21                         $("#j_timer").html(timespanStr);
22 
23                         maxtime--;
24                         residueTime++;
25                     }
26                     else {
27                         clearInterval(timer);
28                         mui.alert("时间到,自动提交!");
29                     }
30                 }
31             }, 1000);
View Code

 二丶

1 var userSecond = Convert.ToInt32((nowTime - startTime).TotalSeconds);//计算时间并转化为秒
2 
3 var demoTime = nowTime.AddMinutes(-10);//初始化时间为十分钟前
var nowTime=DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), //获取此时时间并进行格式化

 三,时间比较(年月)

 //将日期字符串转换为日期对象
DateTime t1 = Convert.ToDateTime(“2019-12”);
DateTime t2 = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM"));
//通过DateTIme.Compare()进行比较()
int compNum = DateTime.Compare(t1, t2);
if(compNum>0){
    //t1>t2
}

四,计算两个时间相隔总月数

function datemonth(date1, date2) {  
   return Math.round((new Date(date2) - new Date(date1)) / (1000 * 60 * 60 * 24) / 30.417) ;
}

五,计算两个时间相隔天数

function  r(){
var sdate1 = new Date(sDate1.replace(/-/g, "/"));
var sdate2 = new Date(sDate2.replace(/-/g, "/"));
if ((new Date(sdate2.Format('yyyy/MM/dd')) - new Date(sdate1.Format('yyyy/MM/dd'))) >= 0) 
{
        alert("两个时间大小的比较");
}
var yqDate = new Date(sDate2.replace(/-/g, "/"));
var sxDate = new Date(sDate3.replace(/-/g, "/"));
var totalDays = yqDate.getTime() - sxDate.getTime();
var totalDay = parseInt(totalDays / (1000 * 60 * 60 * 24));
alert("相差的天数为:"+totalDay);  
}

 六、获取日期【因为 月份在JS里面是从0开始的,所以月份需要+1】

 //获取完整的日期
 var date=new Date;
 var year=date.getFullYear(); 
 var month=date.getMonth()+1;
 month =(month<10 ? "0"+month:month); 
 var mydate = (year.toString()+month.toString());

 

posted @ 2018-09-12 15:23  chenze  阅读(230)  评论(0编辑  收藏  举报
有事您Q我