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

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

问题描述

我需要处理局部的日期,因此,例如:

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

  • 1 2009年4月(存储为1,4,200)
  • 在2000年1月(存储为空值,1,2000)
  • 在三月90(存储为空,3,1990年)
  • 00(存储为NULL,NULL,2000)

等等。

看着它,我不认为这是一个巨大的问题,因为它只会有处理英国的日期,所以全球化不是一个考虑因素。什么是用户表示,当他们键入50,这是否意味着1950年或2050年繁琐位将被工作了。

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.

在我实际涉水而推倒重来,这已经被之前的一些code,我可以作为参考使用或直接做?

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?

更新为清晰

该部分的日期将被用于存储出生日期的人谁是不到25岁。

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:

用户输入:01/00

User Enters: 01/00

PTED为国米$ P $:一月/ 2000

Interpreted as: Jan/2000

显示为:2000年1月

Displayed as: Jan 2000

用户输入:99年1月1日

User Enters: 01/01/99

国米preTED为:1 /月/ 1999

Interpreted as: 1/Jan/1999

显示为:1999年1月1日

Displayed as: 1 Jan 1999

推荐答案

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

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

所以,也许稍微哈克的解决办法是正则表达式匹配任何字符串,但仅以2个或4个数字,和preFIX它与文本简?

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

这也将工作,如果样品是00,2000,或任何其他2或4位(加上空白)字符串。

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

在您的评论指出的3/00字符串是一个谎言,日期时间也不会,除了它,如果它是pfixed了0 $ P $,或者/是替换空白。 你需要做的,什么是检查包含/后面2个数字,然后preFIX这两个数字为20的字符串,导致3/2000,这是分析确定。

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.

烦人,该DateTime.Parse方法不允许检测空字段它发现在解析(它只是检测到那些为月/年的开始)。

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).


也许,作为一个UI的建议,而不是试图把到数据库时要太聪明,当用户键入这个疯狂的日期字符串,解析它当窗体(它是一个Web表单?)提交。如果格式不立即清除,建议符合候选条件的用户,比如,你的意思是1950年,年或2050年?

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天全站免登陆