处理.NET中的部分/不完整日期 [英] Handling partial/incomplete dates in .NET

查看:101
本文介绍了处理.NET中的部分/不完整日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要处理部分日期,例如:




  • 2009年4月1日(存储为1,400)

  • 2000年1月(保存为null,1,2000)

  • 3月90日(保存为null,3,1990)

  • 00(保存为null,null,2000)



等等。


看看它,我不认为这是一个巨大的问题,因为它只需要处理英国的日期,所以全球化不是一个考虑。当他们输入50时,用户意味着什么,这意味着1950或2050年。



在我真正进入和重塑车轮之前,有这个在某些代码中已经完成了我可以作为参考或直接使用的代码?



Clarity的更新



部分日期将用于存储少于25岁的人的出生日期。



至于数据将如何显示:



用户输入:01/00



解释为:Jan / 2000



显示为:2000年1月



用户输入:01/01/99



解释为:1 / Jan / 1999



显示为:1999年1月1日

解决方案

如果我们把这些值通过DateTime.Parse,那么唯一导致问题的是最后一个,因为它不包含任何形式的日期指示:

  Console.WriteLine(DateTime.P屁股(2009年4月1日)); 
//输出01/04/2009 00:00:00
Console.WriteLine(DateTime.Parse(Jan 2000));
//输出01/01/2000 00:00:00
Console.WriteLine(DateTime.Parse(March 90));
//输出01/03/1990 00:00:00
Console.WriteLine(DateTime.Parse(00));
//抛出异常

然而,将Jan放在最后一个字符串的开头,我们:

  Console.WriteLine(DateTime.Parse(Jan 00)); 
//输出01/01/2000 00:00:00

所以,也许稍微hacky的解决方案是正则表达式匹配任何仅由2或4个数字组成的字符串,并将其与文本Jan前缀。

  String sample =00; 

//检查字符串中的2或4个数字,没有其他
//除了
之前的空格if(Regex.IsMatch(sample,@^ \ s *(\d {2} | \d {4})\s * $))
{
sample =Jan+ sample;
}

Console.WriteLine(DateTime.Parse(sample));
//输出01/01/2000

如果sample为00, 2000,或任何其他2或4位数(加上空格)字符串。



您的评论中指出的3/00字符串是一个doozy,DateTime将不会除外如果前缀为0,或者/替换为空格。
你需要做的是检查一个包含/后跟2位数的字符串,然后将这两位数字加上20,导致3/2000,这是解析好的。令人吃惊的是,DateTime.Parse方法不能检测到在解析时找到的空字段(它只是检测到这些作为月/年的开始)。



/ p>




也许,作为一个UI建议,而不是试图在插入数据库时​​太聪明,而用户正在键入在这个疯狂的日期字符串中,当表单(它是一个Web表单?)被提交时解析它。如果格式不能立即清楚,建议可能对用户有用,就像您是指1950年或2050年?


I have the need to handle partial dates, so for example:

  • 1 April 2009 (stored as 1, 4, 200)
  • Jan 2000 (Stored as null, 1, 2000)
  • March 90 (Stored as null, 3, 1990)
  • 00 (Stored as null, null, 2000)

and so on.

Looking at it, I don't think it's a massive problem as it will only have to handle UK dates, so globalization is not a consideration. The fiddly bit will be working out what a user means when they type 50, does that means 1950 or 2050.

Before I actually wade in and reinvent the wheel, has this been done before in some code that I can either use as reference or directly?

Updates for Clarity

The partial dates will be used to store the date of birth for people who are less than 25 years old.

As for how the data will be displayed:

User Enters: 01/00

Interpreted as: Jan/2000

Displayed as: Jan 2000

User Enters: 01/01/99

Interpreted as: 1/Jan/1999

Displayed as: 1 Jan 1999

解决方案

If we put these values through DateTime.Parse, then the only one that causes a problem is the last one, because it contains no date indication of any form:

Console.WriteLine(DateTime.Parse("1 April 2009"));
// Outputs 01/04/2009 00:00:00
Console.WriteLine(DateTime.Parse("Jan 2000"));
// Outputs 01/01/2000 00:00:00
Console.WriteLine(DateTime.Parse("March 90"));
// Outputs 01/03/1990 00:00:00
Console.WriteLine(DateTime.Parse("00"));
// Throws an exception

However, putting Jan at the start of the last string gives us:

Console.WriteLine(DateTime.Parse("Jan 00"));
// Outputs 01/01/2000 00:00:00

So, perhaps a slightly hacky solution is to regex match any string consisting solely of 2 or 4 numbers, and prefix it with the text "Jan"?

    String sample = "00";

    //check for 2 or 4 numbers in the string, nothing else 
    //except space before and after
    if (Regex.IsMatch(sample, @"^\s*(\d{2}|\d{4})\s*$"))
    {
        sample = "Jan " + sample;
    }

    Console.WriteLine(DateTime.Parse(sample));
    // Outputs 01/01/2000

That will also work if sample is 00, 2000, or any other 2 or 4 digit (plus whitespace) strings.

The 3/00 string as indicated in your comment is a doozy, DateTime won't except it if it's prefixed with a 0, or if the "/" is replace with whitespace. What you need to do to that is check for a string containing a "/" followed by 2 digits, and then prefix those two digits with "20", resulting in "3/2000", which is parsed ok.

Annoyingly, the DateTime.Parse method does not allow for detection of null fields it found while parsing (it simply detected those as the start of the month/year).


Perhaps, as a UI suggestion, instead of trying to be too clever when putting into the database, while the user is typing in this crazy date string, parse it when the form (is it a web form?) is submitted. If the format is not immediately clear, suggest possibles to the user, like, did you mean 1950, or 2050?

这篇关于处理.NET中的部分/不完整日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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