编程语言
首页 > 编程语言> > c# – 输出ISO 8601字符串

c# – 输出ISO 8601字符串

作者:互联网

我们如何在特定时区创建日期/时间并输出a short ISO 8601 date/time with offset from UTC?例如,2015年9月8日下午5点太平洋标准时间必须如下所示:

2015-09-08T17:00:00-07:00

这是我目前的尝试.

using System;
using NodaTime;
using NodaTime.Text;

namespace ConsoleApplication1_Nodatime
{
    class Program
    {
        public static void Log(string x) => Console.WriteLine(x);
        public static void Read() => Console.ReadLine();

        static void Main(string[] args)
        {
            var localDateTime = new LocalDateTime(2015, 09, 08, 17, 0);
            var zone = DateTimeZoneProviders.Tzdb.GetZoneOrNull("America/Vancouver");
            var zonedDateTime = localDateTime.InZoneStrictly(zone);
            Log(zonedDateTime.ToOffsetDateTime().ToString());
            Read();
        }
    }
}

虽然看起来有太多步骤,但这样可行.

>创建LocalDateTime
>创建DateTimeZone
>将LocalDateTime转换为ZonedDateTime
>将ZonedDateTime转换为OffsetDateTime

我们如何用更少的步骤来做到这一点?

解决方法:

正如问题评论中所述,您只需要ZonedDateTime即可实现所需的格式化(不需要OffsetDateTime).格式字符串传递“常规”偏移模式,以便仅当偏移量具有分钟时才应包括分钟(“中等格式”).

zonedDateTime.ToString(
    "yyyy-MM-ddTHH:mm:sso<g>", 
    System.Globalization.CultureInfo.InvariantCulture)

为简洁起见,这里列出了可用的偏移模式:

  • f: Full format, displaying all information including fractional seconds. Typical pattern text: +HH:mm:ss.fff
  • l: Long format, displaying information down to the second. Typical pattern text: +HH:mm:ss
  • m: Medium format, displaying information down to the minute. Typical pattern text: +HH:mm
  • s: Short format, displaying information down to the hour. Typical pattern text: +HH
  • g: General pattern. Formatting depends on the value passed in:
    • If the offset has fractional seconds, the full format is used; otherwise
    • If the offset has seconds, the long format is used; otherwise
    • If the offset has minutes, the medium format is used; otherwise
    • The short format is used When parsing, the other standard format patterns are tried one at a time. This is the default format pattern.
  • G: As g, but using Z for an offset of 0, as if it were Z-prefixed.

资料来源:http://nodatime.org/1.3.x/userguide/offset-patterns.html

原始问题

原始请求格式化为ISO-8601始终显示尾随分钟,您可以使用下面的自定义格式字符串.默认情况下,它符合ISO-8601标准,不需要尾随“:00”.但是,您可以传递偏移模式以强制您想要的格式:

zonedDateTime.ToString(
    "yyyy-MM-ddTHH:mm:sso<m>", 
    System.Globalization.CultureInfo.InvariantCulture)

资料来源:http://nodatime.org/1.3.x/userguide/offset-patterns.html

更新密码更新

如果您只是想缩短代码,可以始终将代码包装在辅助方法中 – 甚至可以作为静态扩展方法.

public static class NodaTimeHelpers
{
    public static Lazy<DateTimeZone> Zone = new Lazy<DateTimeZone>(
        () => DateTimeZoneProviders.Tzdb.GetZoneOrNull("America/Vancouver"));
    public static string ToStringWithOffset(this LocalDateTime localDateTime)
    {
        if (localDateTime == null)
            return "";
        var zonedDateTime = localDateTime.InZoneStrictly(Zone.Value);
        return zonedDateTime.ToString(
            "yyyy-MM-ddTHH:mm:sso<g>",
            System.Globalization.CultureInfo.InvariantCulture);
    }
}

这允许您的本地日期时间对象非常容易地转换为字符串:

localDateTime.ToStringWithOffset();

标签:c,net,nodatime
来源: https://codeday.me/bug/20190609/1203055.html