//日期转时间戳
public static long DateTimeToUnixTimestamp(DateTime dateTime)
{
return (dateTime.ToUniversalTime().Ticks - 621355968000000000) / 10000000;
}
//时间戳转日期
public static DateTime ToDateTime(this string timestamp)
{
if(string.IsNullOrEmpty(timestamp))
{
throw new ArgumentNullException(timestamp);
}
DateTime dateTimeStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970,1,1));
long lTime = long.Parse(timestamp + "0000000");
TimeSpan toNow = new TimeSpan(lTime);
return dateTimeStart.Add(toNow);
}
版本号相关操作
--/// <summary>
--/// 已运行的最大行版本号
--/// </summary>
--public static byte[] RunMaxVersionNum = { 0x0 };
--var nowMaxVersionNum = channelCommDal.GetMaxVersionNum(dbConnection);
-- public byte[] GetMaxVersionNum(IDbConnection dbConnection)
-- {
-- return DbHelper.Default.QueryScalar<byte[]>(dbConnection,
-- "SELECT MAX(VersionNum) FROM dbo.Fct_ChannelCommodity");
-- }
--var runVersionNum = "0x" + BitConverter.ToString(RunMaxVersionNum).Replace("-", "");
--public static bool ByteEquals(byte[] b1, byte[] b2)
--{
-- if (b1 == null && b2 == null)
-- {
-- return true;
-- }
-- if (b1 == null || b2 == null)
-- return false;
-- if (b1.Length != b2.Length)
-- return false;
-- return !b1.Where((t, i) => t != b2[i]).Any();
--}
--if (ByteEquals(nowMaxVersionNum, RunMaxVersionNum))
-- Version = Math.Abs(Guid.NewGuid().GetHashCode()).ToString(), --nvarchar(64)
Helper类:
#region Unix时间戳:是指格林威治时间1970年01月01日00时00分00秒(北京时间1970年01月01日08时00分00秒)起至现在的总秒数。
public static long DateTimetoUnix(DateTime dateTime)
{
System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1)); // 当地时区
long timeStamp = (long)(dateTime - startTime).TotalSeconds; // 相差秒数
return timeStamp;
}
/// <summary>
/// unix时间戳转换成日期
/// </summary>
/// <param name="unixTimeStamp">时间戳(秒)</param>
/// <returns></returns>
public static DateTime UnixTimestampToDateTime(long unixTimeStamp)
{
System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1)); // 当地时区
DateTime dt = startTime.AddSeconds(unixTimeStamp);
return dt;
}
#endregion
#region JavaScript时间戳:是指格林威治时间1970年01月01日00时00分00秒(北京时间1970年01月01日08时00分00秒)起至现在的总毫秒数。
public static long DateTimetoJsTimes(DateTime dateTime)
{
System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1)); // 当地时区
long timeStamp = (long)(dateTime - startTime).TotalMilliseconds; // 相差毫秒数
return timeStamp;
}
public static DateTime JsTimestampToDateTime(long jsTimeStamp)
{
System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1)); // 当地时区
DateTime dt = startTime.AddMilliseconds(jsTimeStamp);
return dt;
}
#endregion
//日期转Unix时间戳
public static long DateTimeToUnixTimestamp(DateTime dateTime)
{
return (dateTime.ToUniversalTime().Ticks - 621355968000000000) / 10000000;
}
//Unix时间戳转日期
public static DateTime ToDateTime(this string timestamp)
{
if (string.IsNullOrEmpty(timestamp))
{
throw new ArgumentNullException(timestamp);
}
DateTime dateTimeStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
long lTime = long.Parse(timestamp + "0000000");
TimeSpan toNow = new TimeSpan(lTime);
return dateTimeStart.Add(toNow);
}
参考:https://www.cnblogs.com/polk6/p/6024892.html
1. 什么是时间戳首先要清楚JavaScript与Unix的时间戳的区别:
JavaScript时间戳:是指格林威治时间1970年01月01日00时00分00秒(北京时间1970年01月01日08时00分00秒)起至现在的总毫秒数。
Unix时间戳:是指格林威治时间1970年01月01日00时00分00秒(北京时间1970年01月01日08时00分00秒)起至现在的总秒数。
可以看出JavaScript时间戳总毫秒数,Unix时间戳是总秒数。
比如同样是的 2016/11/03 12:30:00 ,转换为JavaScript时间戳为 1478147400000;转换为Unix时间戳为 1478147400。
2. JavaScript时间戳相互转换
2.1 C# DateTime转换为JavaScript时间戳
1 2 3 | |
2.2 JavaScript时间戳转换为C# DateTime
1 2 3 4 | |
3. Unix时间戳相互转换
3.1 C# DateTime转换为Unix时间戳
1 2 3 | |
3.2 Unix时间戳转换为C# DateTime
1 2 3 4 | |