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

发布于 2024-12-03 10:00:38 字数 54 浏览 0 评论 0原文

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

In C# how can I convert Unix-style timestamp to yyyy-MM-ddThh:mm:ssZ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

神魇的王 2024-12-10 10:00:38

首先将毫秒转换为 TimeSpan

var time = TimeSpan.FromMilliseconds(milliseconds);

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

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

Start by converting your milliseconds to a TimeSpan:

var time = TimeSpan.FromMilliseconds(milliseconds);

Now, in .NET 4 you can call .ToString() with a format string argument. See http://msdn.microsoft.com/en-us/library/system.timespan.tostring.aspx

In previous versions of .NET, you'll have to manually construct the formatted string from the TimeSpan's properties.

爱格式化 2024-12-10 10:00:38

new DateTime(numTicks * 10000)

DateTime(长刻度) 构造函数是你需要什么。每个刻度代表 100 纳秒,因此乘以 10000 即可得到 1 毫秒。

new DateTime(numTicks * 10000)

The DateTime(long ticks) constructor is what you need. Each tick represents 100 nanoseconds so multiply by 10000 to get to 1 millisecond.

〃安静 2024-12-10 10:00:38

如果毫秒基于 UNIX 纪元时间,那么您可以使用:

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

If the milliseconds is based on UNIX epoch time, then you can use:

var posixTime = DateTime.SpecifyKind(new DateTime(1970, 1, 1), DateTimeKind.Utc);
var time = posixTime.AddMilliseconds(milliSecs);
巷子口的你 2024-12-10 10:00:38

这对我有用:

DateTimeOffset.FromUnixTimeMilliseconds(milliseconds);

如果需要,您可以从中获取日期时间。

This worked for me:

DateTimeOffset.FromUnixTimeMilliseconds(milliseconds);

You can get just the DateTime from that if you need it.

日记撕了你也走了 2024-12-10 10:00:38

干得好:

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);
        }
    }

Here you go:

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);
        }
    }
情话已封尘 2024-12-10 10:00:38

此示例将演示总体思路,但您需要知道开始日期是 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);

This sample will demonstrate the general idea, but you need to know if your starting date is DateTime.MinValue or something else:

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);
原来是傀儡 2024-12-10 10:00:38

您可以根据刻度构建日期时间

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"));

You can construct your 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"));
两相知 2024-12-10 10:00:38
    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;            

    }
    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;            

    }
〃温暖了心ぐ 2024-12-10 10:00:38

这个问题应该有您需要的答案

简短版本:

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

This question should have the answer you need.

Short version:

DateTime date = new DateTime(long.Parse(ticks));
date.ToString("yyyy-MM-ddThh:mm:ssZ");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文