首页 > Windows开发 > 详细

C#时间戳转换[转发]

时间:2016-06-30 19:59:45      阅读:241      评论:0      收藏:0      [点我收藏+]

http://www.cnblogs.com/qingliuyu/p/3835858.html

以下是C#下的日期与unix时间戳的相互转换:

/// <summary>
/// 日期转换成unix时间戳
/// </summary>
/// <param name="dateTime"></param>
/// <returns></returns>
public static long DateTimeToUnixTimestamp(DateTime dateTime)
{
    var start = new DateTime(1970, 1, 1, 0, 0, 0, dateTime.Kind);
    return Convert.ToInt64((dateTime - start).TotalSeconds);
}

/// <summary>
/// unix时间戳转换成日期
/// </summary>
/// <param name="unixTimeStamp">时间戳(秒)</param>
/// <returns></returns>
public static DateTime UnixTimestampToDateTime(this DateTime target, long timestamp)
{
    var start = new DateTime(1970, 1, 1, 0, 0, 0, target.Kind);
    return start.AddSeconds(timestamp);
}
/// <summary>
        /// unix时间戳转换成日期
        /// </summary>
        /// <param name="unixTimeStamp">时间戳(秒)</param>
        /// <returns></returns>
        public static DateTime UnixTimestampToDateTime(long timestamp)
        {
            var start = new DateTime(1970, 1, 1, 8, 0, 0, DateTimeKind.Unspecified);
            return start.AddMilliseconds(timestamp);
        }

  

 

C#时间戳转换[转发]

原文:http://www.cnblogs.com/firstcsharp/p/5630893.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!