面向云技术架构 - 痴者工良

  • 首页
  • 工良写的电子书
    • kubernetes 教程
    • 从 C# 入门 Kafka
    • 多线程和异步
    • 动态编程-反射、特性、AOP
    • 表达式树
  • 本站文章导航
  • 隐私政策
愿有人陪你颠沛流离
遇到能让你付出的事物或者人,都是一种运气。
能遇到,就该珍惜。或许你们最终没能在一起,但你会切实地感受到力量。
正因为这样,那段相遇才变得有价值,才没有辜负这世间的每一段相遇。
  1. 首页
  2. .NET
  3. 正文

C# 中的 ISO8601 时间格式

2022年11月19日 688点热度 0人点赞 0条评论
内容纲要

ISO8601 规定了时间格式。
在 Go 语言中,Duration 就实现了这种时间格式。

package main

import (
    "fmt"
    "time"
)

func main() {
    t := time.Now()
    fmt.Println(t)
}

输出:

2022-11-19 02:59:28.201938781 +0000 UTC m=+0.000051897

在 C# 中,可以通过引用 Iso8601DurationHelper 来实现 Duration。

var duration = Duration.Parse("2022-11-19 02:59:28.201938781 +0000 UTC m=+0.000051897");

为了方便将 Duration 与 TimeSpan 之间转换,加上一个扩展:

    public static class DurationExtensions
    {
        /// <summary>
        /// Converts the <see cref="Duration"/> into a <see cref="TimeSpan"/>
        /// </summary>
        /// <param name="duration">The <see cref="Duration"/> to convert</param>
        /// <returns>The converted <see cref="TimeSpan"/></returns>
        public static TimeSpan ToTimeSpan(this Duration duration)
        {
            int days = (int)duration.Days;
            days += (int)duration.Weeks * 7;
            days += (int)duration.Months * 30;
            days += (int)duration.Years * 365;
            return new TimeSpan(days, (int)duration.Hours, (int)duration.Minutes, (int)duration.Seconds);
        }

    }
本作品采用 知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议 进行许可
标签: c
最后更新:2022年11月19日

痴者工良

高级程序员劝退师

点赞
< 上一篇
下一篇 >

文章评论

razz evil exclaim smile redface biggrin eek confused idea lol mad twisted rolleyes wink cool arrow neutral cry mrgreen drooling persevering
取消回复

COPYRIGHT © 2022 whuanle.cn. ALL RIGHTS RESERVED.

Theme Kratos Made By Seaton Jiang

粤ICP备18051778号

粤公网安备 44030902003257号