如何在C#中将毫秒转换为日期格式?

时间:2011-09-07 16:00:16

标签: c# datetime format milliseconds

在C#中,如何将Unix风格的时间戳转换为yyyy-MM-ddThh:mm:ssZ?

9 个答案:

答案 0 :(得分:31)

首先将您的毫秒转换为TimeSpan

var time = TimeSpan.FromMilliseconds(milliseconds);

现在,在.NET 4中,您可以使用格式字符串参数调用.ToString()。见http://msdn.microsoft.com/en-us/library/system.timespan.tostring.aspx

在早期版本的.NET中,您必须从TimeSpan的属性中手动构造格式化字符串。

答案 1 :(得分:22)

new DateTime(numTicks * 10000)

DateTime(long ticks)构造函数是您所需要的。每个刻度表示100纳秒,因此乘以10000得到1毫秒。

答案 2 :(得分:19)

如果毫秒基于UNIX epoch time,则可以使用:

var posixTime = DateTime.SpecifyKind(new DateTime(1970, 1, 1), DateTimeKind.Utc);
var time = posixTime.AddMilliseconds(milliSecs);

答案 3 :(得分:6)

这对我有用:

DateTimeOffset.FromUnixTimeMilliseconds(milliseconds);

如果需要,您可以从中获取DateTime。

答案 4 :(得分:3)

此示例将演示一般概念,但您需要知道您的开始日期是DateTime.MinValue还是其他:

int ms = 1000;                          // One second
var date = new DateTime(ms * 10000);    // The constructor takes number of 100-nanoseconds ticks since DateTime.MinValue (midnight, january 1st, year 1)
string formatted = date.ToString("yyyy-MM-ddTHH:mm:ssZ");
Console.WriteLine(formatted);

答案 5 :(得分:3)

你走了:

public static class UnixDateTime
    {
        public static DateTimeOffset FromUnixTimeSeconds(long seconds)
        {
            if (seconds < -62135596800L || seconds > 253402300799L)
                throw new ArgumentOutOfRangeException("seconds", seconds, "");

            return new DateTimeOffset(seconds * 10000000L + 621355968000000000L, TimeSpan.Zero);
        }

        public static DateTimeOffset FromUnixTimeMilliseconds(long milliseconds)
        {
            if (milliseconds < -62135596800000L || milliseconds > 253402300799999L)
                throw new ArgumentOutOfRangeException("milliseconds", milliseconds, "");

            return new DateTimeOffset(milliseconds * 10000L + 621355968000000000L, TimeSpan.Zero);
        }

        public static long ToUnixTimeSeconds(this DateTimeOffset utcDateTime)
        {
            return utcDateTime.Ticks / 10000000L - 62135596800L;
        }

        public static long ToUnixTimeMilliseconds(this DateTimeOffset utcDateTime)
        {
            return utcDateTime.Ticks / 10000L - 62135596800000L;
        }

        [Test]
        public void UnixSeconds()
        {
            DateTime utcNow = DateTime.UtcNow;
            DateTimeOffset utcNowOffset = new DateTimeOffset(utcNow);

            long unixTimestampInSeconds = utcNowOffset.ToUnixTimeSeconds();

            DateTimeOffset utcNowOffsetTest = UnixDateTime.FromUnixTimeSeconds(unixTimestampInSeconds);

            Assert.AreEqual(utcNowOffset.Year, utcNowOffsetTest.Year);
            Assert.AreEqual(utcNowOffset.Month, utcNowOffsetTest.Month);
            Assert.AreEqual(utcNowOffset.Date, utcNowOffsetTest.Date);
            Assert.AreEqual(utcNowOffset.Hour, utcNowOffsetTest.Hour);
            Assert.AreEqual(utcNowOffset.Minute, utcNowOffsetTest.Minute);
            Assert.AreEqual(utcNowOffset.Second, utcNowOffsetTest.Second);
        }

        [Test]
        public void UnixMilliseconds()
        {
            DateTime utcNow = DateTime.UtcNow;
            DateTimeOffset utcNowOffset = new DateTimeOffset(utcNow);

            long unixTimestampInMilliseconds = utcNowOffset.ToUnixTimeMilliseconds();

            DateTimeOffset utcNowOffsetTest = UnixDateTime.FromUnixTimeMilliseconds(unixTimestampInMilliseconds);

            Assert.AreEqual(utcNowOffset.Year, utcNowOffsetTest.Year);
            Assert.AreEqual(utcNowOffset.Month, utcNowOffsetTest.Month);
            Assert.AreEqual(utcNowOffset.Date, utcNowOffsetTest.Date);
            Assert.AreEqual(utcNowOffset.Hour, utcNowOffsetTest.Hour);
            Assert.AreEqual(utcNowOffset.Minute, utcNowOffsetTest.Minute);
            Assert.AreEqual(utcNowOffset.Second, utcNowOffsetTest.Second);
            Assert.AreEqual(utcNowOffset.Millisecond, utcNowOffsetTest.Millisecond);
        }
    }

答案 6 :(得分:1)

您可以构建datetime from ticks

long ticks = new DateTime(1979, 07, 28, 22, 35, 5, 
  new CultureInfo("en-US", false).Calendar).Ticks;
DateTime dt3 = new DateTime(ticks);
Console.Write(dt3.ToString("yyyy-MM-ddThh:mm:ssZ"));

答案 7 :(得分:0)

    private static DateTime Milliseconds2Date(Double d)
    {         
        TimeSpan time = TimeSpan.FromMilliseconds(d);
        return new DateTime(1970, 1, 1) + time;
    }

    private static Double Date2Milliseconds(DateTime d)
    {

        var t = d.Subtract(new DateTime(1970, 1, 1));
        return t.TotalMilliseconds;            

    }

答案 8 :(得分:-5)

这个问题应该有the answer you need

简短版本:

DateTime date = new DateTime(long.Parse(ticks));
date.ToString("yyyy-MM-ddThh:mm:ssZ");