sql,JS,C#的时间间隔或者时间加上某个数

时间相加其实是指某个日期+天数 ,总不能让两个时间真的相加吧

时间相减指的是两个时间相间隔的天数或者小时等

先说js

时间相加:

 <script type="text/javascript">
        function date() {
            //var dD = new Date(date[0], date[1], date[2]); 
            var date = new Date(Date.parse("2014-02-28"));
            var newdate = new Date(date.setDate(date.getDate() + 1)); //Sun Feb 9 08:00:00 UTC+0800 2014
            var s = Todate(newdate);
            var txt = document.getElementById("TextBox1");
            txt.value = s;
        }
        function Todate(num) {  
            num = num + "";
            var date = "";
            var month = new Array();
            month["Jan"] = 1; month["Feb"] = 2; month["Mar"] = 3; month["Apr"] = 4; month["May"] = 5; month["Jan"] = 6;
            month["Jul"] = 7; month["Aug"] = 8; month["Sep"] = 9; month["Oct"] = 10; month["Nov"] = 11; month["Dec"] = 12;
            var week = new Array();
            week["Mon"] = "一"; week["Tue"] = "二"; week["Wed"] = "三"; week["Thu"] = "四"; week["Fri"] = "五"; week["Sat"] = "六"; week["Sun"] = "日";
            str = num.split(" ");
            date = str[5] + "-";
            date = date + month[str[1]] + "-" + str[2];
            return date;
        }

    </script>


时间间隔:

    <script type="text/javascript">
           function syDate() {
               var t1 = new Date(Date.parse("2014-02-28"));
               var t2 = new Date(Date.parse("2014-03-01"));
               var difference = t2.getTime() - t1.getTime(); //剩余时间
               var thisdays = Math.floor(difference / (1000 * 60 * 60 * 24)); //剩余天
               var thishours = Math.floor(difference / 3600000); //小时
               var syDate = document.getElementById("TextBox1");
               syDate.value ='时间相隔'+ thisdays + "天即" + thishours+"小时";

           }
    </script>


 

SQL语句的写法:

时间加上天数:2008-08-08+3天=2008-08-11

 

dateadd(d,3,'2008-08-08') 


时间间隔:为当前时间-2014-02-01

select datediff(d,'2014-02-01',getdate())

知识点:年、月、日、时、分、秒

datediff(yy,date1,date2)
datediff(m,date1,date2)
datediff(d,date1,date2)
datediff(hh,date1,date2)
datediff(n,date1,date2)
datediff(s,date1,date2)
datediff(ms,date1,date2) 

 

C#写法:

时间加上天数或小时或分钟等

  DateTime t1 = DateTime.Parse("2014-02-28");
  DateTime t=t1.AddDays(1);//日期+1天
  DateTime tt = t1.AddYears(1);//日期+1年
  DateTime twt = t1.AddHours(1);//日期+1小时

时间间隔

            DateTime t1 = DateTime.Parse("2014-02-28");
            DateTime t2 = DateTime.Parse("2014-02-01");
            System.TimeSpan ts = t2 - t1;
            int days = ts.Days;





 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值