解析多种格式的日期时间
我已经创建了一个API终点。调用者可以调用API以 POST
方法,将相关参数。在参数方面有一个参数是日期时间
格式。
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个不同的格式:
The problem is that when calling this API the caller may passes datetime
in 3 different formats:
-
长整型
- 例如1374755180 - 美国格式 - 例如2013年7月25日下午6点37分31秒(如
字符串
) - 时间戳格式 - 例如2013年7月25日14时26分零零秒(如
字符串
)
-
long int
- e.g. 1374755180 - US format - e.g. "7/25/2013 6:37:31 PM" (as
string
) - Timestamp format - e.g. "2013-07-25 14:26:00" (as
string
)
我要解析日期时间
值,并将其转换为的DateTime
或字符串
中的时间戳格式。
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次使用其他
来表示字符串无法解析。这是最好的解决方案?还是有解决方案比这更好的?
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()
{
// http://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()
{
// http://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...");
}
划时代的转换,我从另一个问题借来的。
(扩展方法)
The epoch conversion I borrowed from another question. (An extension method)
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);
}
}