.net时间差计算

C#

using System;

 
public class Test
{
public static void Main()
{
    DateTime t=Convert.ToDateTime("2018-05-29 16:25:20");
TimeSpan t1 = new TimeSpan(t.Ticks);
            TimeSpan t2 = new TimeSpan(DateTime.Now.Ticks);
            TimeSpan t3=t2-t1;

           Console.WriteLine(t3.TotalSeconds) ;//时间段换算为总秒数

           Console.WriteLine(t3.Seconds);//两个时间秒数差

//其他依次类推

             }

}

sql

DATEDIFF(Second, t, GETDATE())    //以上例同效转换,获得两个时间的总秒数。

//在sqlserver中运用时间差函数时应注意t的范围为Int范围,否则会发生类型转换出错不易发现bug

//以下为sql常见类型范围

bigint

从 -2^63 (-9223372036854775808) 到 2^63-1(9223372036854775807) 的整型数据(所有数字)。存储大小为 8 个字节。

int

从 -2^31 (-2,147,483,648) 到 2^31 - 1(2,147,483,647) 的整型数据(所有数字)。存储大小为 4 个字节。int 的 SQL-92 同义字为 integer。

smallint

从 -2^15 (-32,768) 到 2^15 - 1 (32,767) 的整型数据。存储大小为 2 个字节。

tinyint

从 0 到 255 的整型数据。存储大小为 1 字节。

 

猜你喜欢

转载自blog.csdn.net/qq_35048895/article/details/80498888