utc datetime 互相转化

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
///


/// DateTime.UtcNow.toString()输出的是0时区的时间
/// DateTime.Now.ToString()输出的是当前时区的时间。我们中国使用的是东八区的时间,所以差了8个小时
/// utc datetime 互相转化
///

public class Timer : MonoBehaviour
{

    // Use this for initialization
    void Start()
    {
        DateTime utcNow = DateTime.Now.ToUniversalTime();
        double utc = ConvertDateTimeInt(utcNow);
        DateTime dtime = ConvertIntDateTime(utc);

    }

    public static int ConvertDateTimeInt(DateTime time)
    {
        double intResult = 0;
        DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
        intResult = (time - startTime).TotalSeconds;
        return (int)intResult;
    }
    public static DateTime ConvertIntDateTime(double utc)
    {
        System.DateTime startTimr = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
        startTimr = startTimr.AddSeconds(utc);
        startTimr = startTimr.AddHours(8);//转化为北京时间(北京时间=UTC时间+8小时)
        return startTimr;
    }

}
 

你可能感兴趣的:(utc,datetime,互相转化,C#,知识点)