[英]Parse datetime in multiple formats
I have created an API end-point.我已经创建了一个 API 端点。 The caller may call the API with
POST
method passing the relevant parameters.调用者可以通过传递相关参数的
POST
方法调用 API。 In the parameters there is one parameter that is of datetime
format.在参数中有一个是
datetime
格式的参数。
The problem is that when calling this API the caller may passes datetime
in 3 different formats:问题在于,调用此 API 时,调用者可能会以 3 种不同格式传递
datetime
时间:
long int
- eg 1374755180 long int
- 例如 1374755180string
)string
)string
)string
) I have to parse the datetime
value and convert it to a DateTime
or string
in Timestamp format.我必须解析
datetime
值并将其转换为时间戳格式的DateTime
或string
。
I have tried using DateTime.TryParse()
, DateTime.Parse()
, Convert.ToDateTime()
and Convert.ToDouble()
but none of them are working in certainty for me.我曾尝试使用
DateTime.TryParse()
、 DateTime.Parse()
、 Convert.ToDateTime()
和Convert.ToDouble()
但没有一个对我来说肯定有效。
The required output has to be in en-GB
format.所需的输出必须是
en-GB
格式。
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.我原以为有一个
if-else if-else
块使用具有TryParse
有一个3倍else
说字符串无法解析。 Is this the best solution?这是最好的解决方案吗? Or are there solutions better than this?
或者有比这更好的解决方案吗?
Please help!请帮忙!
You should consider requiring a timezone.您应该考虑要求时区。 1 doesn't need it, but #2 and #3 do.
1 不需要它,但 #2 和 #3 需要。
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...");
}
EDIT As Matt Johnson points out, DateTime.TryParseExact accepts an array of format strings.编辑正如马特约翰逊指出的那样, DateTime.TryParseExact 接受格式字符串数组。 2 & 3 could be condensed.
2 & 3 可以压缩。
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...");
}
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);
}
}
You are looking for the DateTime.ParseExact
( MSDN Article )您正在寻找
DateTime.ParseExact
( MSDN 文章)
Which you would use in a situation like this:你会在这样的情况下使用它:
string[] formats= { "MM/dd/yyyy hh:mm:ss tt", "yyyy-MM-dd hh:mm:ss" }
var dateTime = DateTime.ParseExact("07/25/2013 6:37:31 PM", formats, new CultureInfo("en-GB"), DateTimeStyles.None);
This allows you to add as many DateTime
formats to the array
as you need and the method will do the conversion without the if
... else
statements.这允许您根据需要向
array
添加DateTime
格式,并且该方法将在没有if
... else
语句的情况下进行转换。
If your integer is in seconds since Unix Epoch you add the number of seconds to the DateTime of the Epoch (01/01/1970) (.Net doesn't have an out of the box method for this, but the logic is seconds since 'Epoch'):如果您的整数是自Unix Epoch以来的秒数,您将秒数添加到 Epoch (01/01/1970) 的 DateTime(.Net 没有开箱即用的方法,但逻辑是自'时代'):
new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddSeconds(seconds);
From this question .从这个问题。
One way to deal with this problem would be setting up a factory method that "understands" different formats, and parses them accordingly.处理这个问题的一种方法是设置一个“理解”不同格式的工厂方法,并相应地解析它们。
You can create a chain of if
- then
- else
s to deal with this problem, but you can also make a "table-driven" implementation: what you need is an array of delegates that take a string, and tell you two things:您可以创建一个
if
- then
- else
链来处理这个问题,但您也可以进行“表驱动”实现:您需要的是一个接受字符串的委托数组,并告诉您两件事:
DateTime
DateTime
Here is a sample implementation:这是一个示例实现:
private static readonly DateParsers = new Func<string,Tuple<DateTime,bool>>[] {
(s) => {
long res;
if (long.TryParse(s, out res)) {
// The format was correct - make a DateTime,
// and return true to indicate a successful parse
return Tuple.Create(new DateTime(res), true);
} else {
// It does not matter what you put in the Item1
// when Item2 of the tuple is set to false
return Tuple.Create(DateTime.MinValue, false);
}
}
...
// Add similar delegates for other formats here
};
Now your factory method could be implemented as follows:现在你的工厂方法可以实现如下:
private static bool TryParseMultiformat(string s, out DateTime res) {
// Check all parsers in turn, looking for one returning success
foreach (var p in DateParsers) {
var tmp = p(s);
if (tmp.Item2) {
res = tmp.Item1;
return true;
}
}
res = DateTime.MinValue;
return false;
}
If the possible formats are fixed then you can use TryParseExact如果可能的格式是固定的,那么您可以使用TryParseExact
A possible solution is to use TryParse , if it fails to get proper date then fallback to known formats and use TryPraseExact一个可能的解决方案是使用TryParse ,如果它无法获得正确的日期,则回退到已知格式并使用TryPraseExact
I was facing same problem in a project where my code will run on different environments with various culture formats.我在一个项目中遇到了同样的问题,我的代码将在具有各种文化格式的不同环境中运行。
Google showed me this hidden gem .谷歌向我展示了这个隐藏的宝石。 The helper function is indispensable to auto-parse datetime properly regardless of culture formats
无论文化格式如何,辅助函数对于正确自动解析日期时间都是必不可少的
Usage Examples:用法示例:
string str = @"The last round was June 10, 2005; this time the unbroken record was held.";
DateTimeRoutines.ParsedDateTime pdt;
if (DateTimeRoutines.TryParseDate(str, DateTimeRoutines.DateTimeFormat.USA_DATE, out pdt))
Console.WriteLine("Date was found: " + pdt.DateTime.ToString());
According to the author, the code is capable of parsing various cases:根据作者的说法,该代码能够解析各种情况:
@"Member since: 10-Feb-2008"
@"Last Update: 18:16 11 Feb '08 "
@"date Tue, Feb 10, 2008 at 11:06 AM"
@"see at 12/31/2007 14:16:32"
@"sack finish 14:16:32 November 15 2008, 1-144 app"
@"Genesis Message - Wed 04 Feb 08 - 19:40"
@"The day 07/31/07 14:16:32 is "
@"Shipping is on us until December 24, 2008 within the U.S."
@" 2008 within the U.S. at 14:16:32"
@"5th November, 1994, 8:15:30 pm"
@"7 boxes January 31 , 14:16:32."
@"the blue sky of Sept 30th 2008 14:16:32"
@" e.g. 1997-07-16T19:20:30+01:00"
@"Apr 1st, 2008 14:16:32 tufa 6767"
@"wait for 07/31/07 14:16:32"
@"later 12.31.08 and before 1.01.09"
@"Expires: Sept 30th 2008 14:16:32"
@"Offer expires Apr 1st, 2007, 14:16:32"
@"Expires 14:16:32 January 31."
@"Expires 14:16:32 January 31-st."
@"Expires 23rd January 2010."
@"Expires January 22nd, 2010."
@"Expires DEC 22, 2010."
Thanks for your answers.感谢您的回答。 I tried the options suggested in couple of answers and found out a very simple approach that worked for me.
我尝试了几个答案中建议的选项,并找到了一种对我有用的非常简单的方法。
public static bool ParseDate(string dateString, out DateTime dateValue)
{
long dtLong = 0L;
bool result = false;
if (long.TryParse(dateString, out dtLong))
{
// I copied the epoch code here for simplicity
dateValue = new DateTime(1970, 1, 1, 0, 0, 0).AddSeconds(dtLong);
result = true;
}
// Working for US and Timestamp formats
else if (DateTime.TryParse(dateString, out dateValue))
result = true;
return result;
}
Earlier I was trying to use TryParse
for all the 3 formats which was not working.早些时候,我尝试将
TryParse
用于所有 3 种无效的格式。
Somehow, TryParseExact
did not work for the timestamp format.不知何故,
TryParseExact
不适用于时间戳格式。 It worked for the US format.它适用于美国格式。 That is the reason I had to write my own.
这就是我必须自己写的原因。
If you use TryParseExact
, only GOD and the Microsoft developers know how many possible date time formats it will try to parse before it gives up.如果您使用
TryParseExact
,则只有 GOD 和 Microsoft 开发人员知道它在放弃之前将尝试解析多少种可能的日期时间格式。 Perhaps a better solution is to use a quick regex and then an appropriate parser.也许更好的解决方案是使用快速正则表达式,然后使用适当的解析器。 I tried to make the regex as simple as possibke, you may have to tweak this a little
我试图让正则表达式像 possibke 一样简单,你可能需要稍微调整一下
private static readonly Regex R1
= new Regex(@"^\d+$", RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.Singleline);
private static readonly Regex R2
= new Regex(@"M$", RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.Singleline);
private static readonly Regex R3
= new Regex(@"^\d{4}-", RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.Singleline);
private static void Main(string[] args)
{
string[] stringDates = new[]
{
"1374755180",
"2013-07-25 14:26:00",
"7/25/2013 6:37:31 PM"
};
foreach (var s in stringDates)
{
DateTime date = default(DateTime);
if (R1.IsMatch(s))
date = new DateTime(long.Parse(s));
else if (R2.IsMatch(s))
date = DateTime.Parse(s);
else if (R3.IsMatch(s))
date = DateTime.Parse(s);
if (date != default(DateTime))
Console.WriteLine("{0}", date);
}
Console.WriteLine("Press ENTER to continue...");
Console.ReadLine();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.