简体   繁体   English

DateTime.TryParse只用于Time参数?

[英]DateTime.TryParse for only a Time parameter?

I am writing a service that calls a method at 4pm and 5am. 我正在写一个在下午4点和凌晨5点调用方法的服务。 I would like to keep these times from being hard coded and so I would like to have put them in my appconfig. 我想保持这些时间不被硬编码,所以我想将它们放在我的appconfig中。

Here is the code I have: 这是我的代码:

public bool CheckTime() 
        {
            DateTime startTime;
            DateTime endTime; 

            DateTime.TryParse(ConfigurationManager.AppSettings["ProcessingStartTime"], out startTime);
            DateTime.TryParse(ConfigurationManager.AppSettings["ProcessingEndTime"], out endTime);

            if(DateTime.Now.TimeOfDay.Equals(startTime) || DateTime.Now.TimeOfDay.Equals(endTime))
                return true;
            else
                return false;
        }

But, since I only want to store the time as a string (something as easy as "4:00pm") how do I parse and encapsulate JUST the time in a DateTime object? 但是,因为我只想将时间存储为字符串(就像“下午4点”一样简单),如何解析和封装JUST时间在DateTime对象中? Or is there another object? 还是有另一个对象? I don't care about the date, it just has to be MF of any given week of any given year. 我不关心日期,它只需要是任何给定年份的任何给定周的MF。

Thanks guys. 多谢你们。

You can use TryParseExact with an appropriate format string, and then ignore the date. 您可以将TryParseExact与适当的格式字符串一起使用,然后忽略日期。 There's no nice encapsulation for "just a time" in .NET. .NET中的“只是时间”没有很好的封装。 However, there's an alternative - I've been working hard on Noda Time which has the LocalTime type for just this purpose: 但是,有一个替代方案 - 我一直在Noda Time上努力工作,它具有LocalTime类型就是为了这个目的:

// "Traditional" way
LocalTime time;
if (LocalTime.TryParseExact(text, "h:mmtt", out time))
{
    ...
}

// Preferred way

private static readonly LocalTimePattern TimePattern = 
    LocalTimePattern.CreateWithInvariantInfo("h:mmtt");
...

ParseResult<LocalTime> parseResult = TimePattern.Parse(text);
if (parseResult.Success)
{
    LocalTime time = parseResult.Value;
    ...
}

Two side-notes: 两个旁注:

  • It's almost never appropriate to ignore the return value from DateTime.TryParse (or any other TryParse method) - do you really want to use DateTime.MinValue if there's a problem with the data? 忽略DateTime.TryParse (或任何其他TryParse方法)的返回值几乎永远不合适 - 如果数据有问题,你真的想使用DateTime.MinValue吗? There are cases where the default value is appropriate, but they're pretty rare 某些情况下,默认值是合适的,但它们非常罕见
  • Please try to avoid code of the form: 请尽量避免使用表单代码:

     if (condition) return true; else return false; 

    This is much more clearly written as: 这更明确地写为:

     return condition; 

I've had a hard time doing the tryparse (even w/ TryParseExact) so I wrote a regex: 我很难做tryparse(甚至是w / TryParseExact)所以我写了一个正则表达式:

Regex checkTime = new Regex(@"^(?i)(0?[1-9]|1[0-9]|2[0-4]):([0-5][0-9])(:[0-5][0-9])?( AM| PM)?$");

And you can validate your time with this: 你可以用这个验证你的时间:

checkTime.IsMatch(str);

This will work for these format (case insensitive): 这将适用于这些格式(不区分大小写):

  • 08:09:00 AM 上午08:09:00
  • 08:09 AM 上午08:09
  • 8:30 AM 上午8:30
  • 8:30:59 八时30分59秒

You can check it here 你可以在这里查看

Hope this helps 希望这可以帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM