js、 sql、 C#时间、时间戳相互转换
//1.获取当前时间戳_c#
long aa = (DateTime.Now.ToUniversalTime().Ticks - 621355968000000000) / 10000000
//2.时间戳->时间 C#
DateTime b11 = GetTime("1483461862");//11位时间戳->时间
DateTime b13 = ConvertStringToDateTime("1483461862");//13 位时间戳->时间
//3. 时间->时间戳C#
int a11= ConvertDateTimeInt(b11);//11 位 时间->时间戳
long a13 = ConvertDateTimeToInt(b13);//13位 时间->时间戳
private DateTime GetTime(string timeStamp) //11 时间戳转为C#格式时间
{
DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
long lTime = long.Parse(timeStamp + "0000000");
TimeSpan toNow = new TimeSpan(lTime); return dtStart.Add(toNow);
}
private int ConvertDateTimeInt(System.DateTime time) //11 DateTime时间格式转换为Unix时间戳格式
{
System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1));
return (int)(time - startTime).TotalSeconds;
}
public static long ConvertDateTimeToInt(System.DateTime time) ////13 位 时间转为时间
{
System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1, 0, 0, 0, 0));
long t = (time.Ticks - startTime.Ticks) / 10000; //除10000调整为13位
return t;
}
private DateTime ConvertStringToDateTime(string timeStamp) //13位时间戳转为C#格式时间
{
DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
long lTime = long.Parse(timeStamp + "0000");
TimeSpan toNow = new TimeSpan(lTime);
return dtStart.Add(toNow);
}
sql
--1当前时间戳 获取sql
SELECT DATEDIFF(S,'1970-01-01 00:00:00', GETDATE()) - 8 * 3600
SELECT DATEDIFF(s, '1970-01-01 00:00:00', GETUTCDATE())
--2时间戳→普通时间sql
SELECT DATEADD(s,1483461862,'1970-01-01 08:00:00') as DTime
SELECT DATEADD(S,1483461862 + 8 * 3600,'1970-01-01 00:00:00')
--3 普通时间 → Unix时间戳sql
SELECT DATEDIFF(s, '1970-01-01 08:00:00', '2017-01-04 00:44:22.000')
js
1当前时间戳 获取js
Math.round(new Date().getTime()/1000)
2时间戳→普通时间js
var unixTimestamp = new Date(Unix timestamp * 1000);
var commonTime =unixTimestamp.toLocaleString();
3 普通时间 → Unix时间戳js
var commonTime = new Date(Date.UTC(year, month - 1, day, hour, minute, second))