check if date time string contains time

后端 未结 5 1790
鱼传尺愫
鱼传尺愫 2021-02-06 23:37

I have run into an issue. I\'m obtaining a date time string from the database and and some of these date time strings does not contain time. But as for the new requirement every

5条回答
  •  情书的邮戳
    2021-02-07 00:22

    Here's what I'm going with for now. It may not be perfect, but likely better than considering any 12am datetime as not having a time. The premise is that if I tack a full time specification on the end it will parse if it's just a date but fail if it already has a time component.

    I had to make the assumption that there's not some valid, date/time that has 7 non-white-space characters or less. It appears that "1980/10" parses, but not "1980/10 01:01:01.001".

    I've included various test cases. Feel free to add your own and let me know if they fail.

    public static bool IsValidDateTime(this string dateString, bool requireTime = false)
    {
        DateTime outDate;
        if(!DateTime.TryParse(dateString, out outDate)) return false;
    
        if (!requireTime) return true;
        else
        {
            return Regex.Replace(dateString, @"\s", "").Length > 7 
    && !DateTime.TryParse(dateString + " 01:01:01.001", out outDate);
        }
    }
    
    public void DateTest()
    {
        var withTimes = new[]{
        "1980/10/11 01:01:01.001",
        "02/01/1980 01:01:01.001",
        "1980-01-01 01:01:01.001",
        "1980/10/11 00:00",
        "1980/10/11 1pm",
        "1980-01-01 00:00:00"};
    
        //Make sure our ones with time pass both tests
        foreach(var date in withTimes){
            Assert.IsTrue(date.IsValidDateTime(), String.Format("date: {0} isn't valid.", date));
            Assert.IsTrue(date.IsValidDateTime(true), String.Format("date: {0} does have time.", date));
        }
    
        var withoutTimes = new[]{
        "1980/10/11",
        "1980/10",
        "1980/10 ",
        "10/1980",
        "1980 01",
        "1980/10/11 ",
        "02/01/1980",
        "1980-01-01"};
    
        //Make sure our ones without time pass the first and fail the second
        foreach (var date in withoutTimes)
        {
            Assert.IsTrue(date.IsValidDateTime(), String.Format("date: {0} isn't valid.", date));
            Assert.IsFalse(date.IsValidDateTime(true), String.Format("date: {0} doesn't have time.", date) );
        }
    
        var bogusTimes = new[]{
        "1980",
        "1980 01:01",
        "80 01:01",
        "1980T01",
        "80T01:01",
        "1980-01-01T01",
        };
    
        //Make sure our ones without time pass the first and fail the second
        foreach (var date in bogusTimes)
        {
            DateTime parsedDate;
            DateTime.TryParse(date, out parsedDate);
            Assert.IsFalse(date.IsValidDateTime(), String.Format("date: {0} is valid. {1}", date, parsedDate));
            Assert.IsFalse(date.IsValidDateTime(true), String.Format("date: {0} is valid. {1}", date, parsedDate));
        }
    }
    

提交回复
热议问题