C#中unixtime和DateTime为转换

 

C#代码
using System;   
using System.Collections.Generic;   
using System.Linq;   
using System.Text;   
  
namespace MYspace   
{   
    public class UnixTime   
    {   
        private static DateTime BaseTime = new DateTime(1970,1,1);   
  
        /// <summary>   
        /// 将unixtime转换为.NET的DateTime   
        /// </summary>   
        /// <param name="timeStamp">秒数</param>   
        /// <returns>转换后的时间</returns>   
        public static DateTime FromUnixTime(long timeStamp)   
        {   
            return new DateTime((timeStamp + 8 * 60 * 60) * 10000000 + BaseTime.Ticks);   
        }   
  
        /// <summary>   
        /// 将.NET的DateTime转换为unix time   
        /// </summary>   
        /// <param name="dateTime">待转换的时间</param>   
        /// <returns>转换后的unix time</returns>   
        public static long FromDateTime(DateTime dateTime)   
        {   
            return (dateTime.Ticks - BaseTime.Ticks) / 10000000 - 8 * 60 * 60;   
        }   
    }   
}   

上一篇: VMware 虚拟机优化   下一篇: VMware Server中虚拟机随宿主机自动启动

提交疑问

回顶部