C# DatetTime转UTC以及UTC转DateTime

    public class TimeHelper
    {
        public double ConvertDateTimeInt(System.DateTime time)//将时间格式的数据类型转换成浮点数类型   
        {
            double intResult = 0;
            System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1));
            intResult = (time - startTime).TotalSeconds;
            return intResult;
        }

        public DateTime ConvertIntDatetime(double utc)
        {
            System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1));
            startTime = startTime.AddSeconds(utc);
            startTime = startTime.AddHours(8);//转化为北京时间(北京时间=UTC时间+8小时 )
            return startTime;
        }
    }
posted @ 2022-12-03 09:09  星火燎猿*  阅读(668)  评论(0编辑  收藏  举报