简体   繁体   English

将 2015-06-01T02:31:00+0000 转换为 DateTime 对象 c#

[英]Convert 2015-06-01T02:31:00+0000 to DateTime object c#

I'm writting an app that consume this webservice:我正在编写一个使用此网络服务的应用程序:

http://finance.yahoo.com/webservice/v1/symbols/allcurrencies/quote?format=json http://finance.yahoo.com/webservice/v1/symbols/allcurrencies/quote?format=json

as you can see there, the JSON object comes with an utc datetime field.正如您在那里看到的,JSON 对象带有一个 utc 日期时间字段。 I want to save this information in a simple DateTime object with the following format "yyyy-MM-dd HH:mm:ss".我想将此信息保存在一个简单的 DateTime 对象中,格式如下“yyyy-MM-dd HH:mm:ss”。

This is my code:这是我的代码:

 DateTime dateParsed = DateTime.Now;       
 DateTime.TryParseExact((string)resource.SelectToken("resource").SelectToken("fields")["utctime"], "yyyy'-'MM'-'dd'T'HH':'mm':'ssz", CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.AdjustToUniversal, out dateParsed);

I'm getting an DateTime object initialized in the year 0001.我得到了一个在 0001 年初始化的 DateTime 对象。

What I'm doing wrong?我做错了什么?

You should be using the K custom format specifier (instead of z ).您应该使用K自定义格式说明符(而不是z )。

string s = "2015-06-01T04:41:10+0000";

DateTime dt = DateTime.ParseExact(s, "yyyy-MM-dd'T'HH:mm:ssK",
                                  CultureInfo.InvariantCulture,
                                  DateTimeStyles.AdjustToUniversal);

It will read the +0000 as the offset.它将读取+0000作为偏移量。 Then by using the AdjustToUniversal style, the resulting DateTime will also be in terms of Universal time, having DateTimeKind.Utc .然后通过使用AdjustToUniversal样式,生成的DateTime将使用世界时,具有DateTimeKind.Utc

Also, since you're reading from a known data source, there's no real benefit of using TryParseExact .此外,由于您是从已知数据源读取数据,因此使用TryParseExact并没有真正的好处。 The format from your data source is fixed, so just use ParseExact with that format.您的数据源的格式是固定的,因此只需将ParseExact与该格式一起使用。 The Try... methods are primarily for validating user input, or when the source format could vary. Try...方法主要用于验证用户输入,或者当源格式可能变化时。

One last point - if you just parse your data using JSON.net , that format should automatically be recognized.最后一点 - 如果您只是使用JSON.net解析数据,则应自动识别该格式。 You just use a DateTime or DateTimeOffset property, and it would parse it without issue.您只需使用DateTimeDateTimeOffset属性,它就会毫无问题地解析它。

You have just an error in your Format-String.您的格式字符串中有一个错误。 This is a working sample:这是一个工作示例:

using System;
using System.Globalization;

public class Program
{
    public static void Main()
    {
        DateTime dateParsed = DateTime.Now; 
        if ( DateTime.TryParseExact( "2015-06-01T02:31:00+0000", "yyyy-MM-ddThh:mm:ss+0000", CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.AdjustToUniversal, out dateParsed ) ) {
            Console.WriteLine(string.Format("Parsing done: {0:MM/dd/yyyy @ hh:mm}", dateParsed ) );
        } else {
            Console.WriteLine("No result");
        }
    }
}

Note: the +0000 is hardcoded, when you get other values you would need to detect them.注意: +0000是硬编码的,当您获得其他值时,您需要检测它们。 If the api returns only +0 values, you could cut them off and work without z.如果 api 仅返回 +0 值,您可以将它们切断并在没有 z 的情况下工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM