如何将毫秒转换为 UTC 日期时间 C#

How to convert millisecond to UTC dateTime C#

我正在尝试将毫秒转换为 UTC 日期时间。我已经设法将毫秒转换为本地 DateTime,但无法将本地 dateTime 转换为 utc dateTime。当我使用 DateTimeOffset 时,我落后一小时所以我相信这是本地 DateTime?

我无法将本地时间转换为 UTC 日期时间

测试Class

[Fact]
    public void DateTimeCalculation_MustTakeDateTimeParameter_InMilliSecond()
    {
        //Arrange
        long startDateTimeUtc = 1626987600000; //2021-07-22 22:00:00

        //Act
        DateTimeOffset calculatedTimeOffset = DateTimeCalculationHelper.DateTimeCalculation(startDateTimeUtc);
        var utcDateTime = calculatedTimeOffset.UtcDateTime.ToString(); //not working

        var t1 = calculatedTimeOffset.ToUniversalTime(); //not working

        //Assert
        string actual = calculatedTimeOffset.ToString();
        string expected = "22/07/2021 10:00:00 PM +00:00";
        Assert.Equal(expected, actual);
    }

静态class 将 DateTime 转换为毫秒

public static class DateTimeCalculationHelper
{
    public static DateTimeOffset DateTimeCalculation(long milSec)
    {
        DateTimeOffset epochTime = new DateTimeOffset(1970, 1, 1, 0, 0, 0, TimeSpan.Zero);
        return epochTime.AddMilliseconds(milSec);
    }
}
   var longDate = new DateTime(132713856000000000);//{7/22/0421 12:00:00 AM}
            var dateNow = new DateTime(2021 , 07 ,22);//{7/22/2021 12:00:00 AM}
            var dateNowUTC = dateNow.ToFileTimeUtc();//132713856000000000
             var  utcDate = DateTime.SpecifyKind(dateNow, DateTimeKind.Utc).ToString("U");//"Thursday, July 22, 2021 12:00:00 AM"