以多种格式解析日期时间 [英] Parse datetime in multiple formats

查看:44
本文介绍了以多种格式解析日期时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个 API 端点.调用者可以通过POST 方法调用API,传递相关参数.参数中有一个参数是datetime格式.

I have created an API end-point. The caller may call the API with POST method passing the relevant parameters. In the parameters there is one parameter that is of datetime format.

问题在于调用此 API 时,调用者可能会以 3 种不同格式传递 datetime:

The problem is that when calling this API the caller may passes datetime in 3 different formats:

  1. long int - 例如1374755180
  2. 美国格式 - 例如7/25/2013 6:37:31 PM"(作为 string)
  3. 时间戳格式 - 例如2013-07-25 14:26:00"(作为string)
  1. long int - e.g. 1374755180
  2. US format - e.g. "7/25/2013 6:37:31 PM" (as string)
  3. Timestamp format - e.g. "2013-07-25 14:26:00" (as string)

我必须解析 datetime 值并将其转换为时间戳格式的 DateTimestring.

I have to parse the datetime value and convert it to a DateTime or string in Timestamp format.

我尝试过使用 DateTime.TryParse()DateTime.Parse()Convert.ToDateTime()Convert.ToDouble() 但没有一个对我来说肯定有效.

I have tried using DateTime.TryParse(), DateTime.Parse(), Convert.ToDateTime() and Convert.ToDouble() but none of them are working in certainty for me.

所需的输出必须是 en-GB 格式.

The required output has to be in en-GB format.

我曾想有一个 if-else if-else 块与 TryParse 一起使用 3 次,用一个 else 说出字符串无法解析.这是最好的解决方案吗?或者还有比这更好的解决方案吗?

I had thought to have an if-else if-else block to use with TryParse 3 times with one else to say the string could not be parsed. Is this the best solution? Or are there solutions better than this?

请帮忙!

推荐答案

您应该考虑要求时区.1 不需要它,但 #2 和 #3 需要.

You should consider requiring a timezone. 1 doesn't need it, but #2 and #3 do.

public DateTime ParseRequestDate()
{
    // https://stackoverflow.com/questions/2883576/how-do-you-convert-epoch-time-in-c

    CultureInfo enUS = new CultureInfo("en-US");

    var dt = "1374755180";
    //var dt = "7/25/2013 6:37:31 PM";
    //var dt = "2013-07-25 14:26:00";

    DateTime dateValue;
    long dtLong;

    // Scenario #1
    if (long.TryParse(dt, out dtLong))
        return dtLong.FromUnixTime();

    // Scenario #2
    if (DateTime.TryParseExact(dt, "MM/dd/yyyy hh:mm:ss tt", enUS, DateTimeStyles.None, out dateValue))
        return dateValue;

    // Scenario #3
    if (DateTime.TryParseExact(dt, "yyyy-MM-dd hh:mm:ss", enUS, DateTimeStyles.None, out dateValue))
        return dateValue;

    throw new SomeException("Don't know how to parse...");
}

编辑正如马特约翰逊指出的那样, DateTime.TryParseExact 接受格式字符串数组.2 &3 可以压缩.

EDIT As Matt Johnson points out, DateTime.TryParseExact accepts an array of format strings. 2 & 3 could be condensed.

public DateTime ParseRequestDate()
{
    // https://stackoverflow.com/questions/2883576/how-do-you-convert-epoch-time-in-c

    CultureInfo enUS = new CultureInfo("en-US");

    var dt = "1374755180";
    //var dt = "7/25/2013 6:37:31 PM";
    //var dt = "2013-07-25 14:26:00";

    DateTime dateValue;
    long dtLong;

    // Scenario #1
    if (long.TryParse(dt, out dtLong))
        return dtLong.FromUnixTime();

    // Scenario #2 & #3
    var formatStrings = new string[] { "MM/dd/yyyy hh:mm:ss tt", "yyyy-MM-dd hh:mm:ss" };
    if (DateTime.TryParseExact(dt, formatStrings, enUS, DateTimeStyles.None, out dateValue))
        return dateValue;

    throw new SomeException("Don't know how to parse...");
}

我从另一个问题借来的纪元转换.(一种扩展方法)

public static class MyExtensions
{
    public static DateTime FromUnixTime(this long unixTime)
    {
        var epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
        return epoch.AddSeconds(unixTime);
    }
}

这篇关于以多种格式解析日期时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆