日期时间转换/魔法

发布于 2024-09-24 13:30:51 字数 226 浏览 2 评论 0原文

有没有一种简洁的方法可以用 C# 编写这个逻辑?

if ((DateTime.Now >= "8:00 AM") && (DateTime.Now < 5:00 PM))
{// do something}

我正在制作一个演示应用程序,我想在工作日做一些事情,但我不希望这段代码太突出(大量转换=糟糕)。 (因为我希望我的演示内容更容易看到。

Is there a nice concise way to write this logic in C#?

if ((DateTime.Now >= "8:00 AM") && (DateTime.Now < 5:00 PM))
{// do something}

I am making a demo app where I want to make something happen in the work day, but I don't want this code to stand out much (a lot of casting = bad). (Because I want my demo stuff to be easier to see.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

2024-10-01 13:30:51

好吧,你可以这样做:

DateTime now = DateTime.Now;
if (now.Hour < 8 || now.Hour >= 17)

请注意,我通常更喜欢只使用 DateTime.Now 属性一次,将结果复制到上面的局部变量中 - 这样你就不会'由于呼叫之间的时间变化,不会出现奇怪的可能性。这里不是问题,但在其他情况下可能会出现问题。

如果您想以这种方式处理事情,另一种可能性是使用DateTime.TimeOfDay。我认为上面的内容已经很简单了。

编辑:史蒂文指出我改变了 &&按照你原来的逻辑 || - 你原来的逻辑永远不会起作用,因为它永远不可能在上午 8 点之前下午 5 点之后。上面的内容适用于“如果不是在工作日” - 如果您想要“如果是在工作日”,您只需要:

DateTime now = DateTime.Now;
if (now.Hour >= 8 && now.Hour < 17)

Well, you could do:

DateTime now = DateTime.Now;
if (now.Hour < 8 || now.Hour >= 17)

Note that I generally prefer to only use the DateTime.Now property once, copying the result into the local variable as above - that way you don't get odd possibilities due to the time changing between calls. Not a problem here, but it could be in other cases.

Another possibility is to use DateTime.TimeOfDay if you want to handle things that way. I think the above is about as simple as it gets though.

EDIT: Steven pointed out that I changed the && in your original logic to || - your original logic can never work, as it can never be before 8am and after 5pm. The above works for "if it's not in the working day" - if you want "if it is in the working day" you just need:

DateTime now = DateTime.Now;
if (now.Hour >= 8 && now.Hour < 17)
灯角 2024-10-01 13:30:51

我想你想在这里做的是这样的:

var now = DateTime.Now;
if (now.Hour >= 8 && now.Hour < 17)

I think what you want to do here is something like this:

var now = DateTime.Now;
if (now.Hour >= 8 && now.Hour < 17)
怪我太投入 2024-10-01 13:30:51

如果你想让它变得“漂亮”,你还可以使用扩展方法:

public static class DateTimeHelper
{
    public static DateTime Time(this string time)
    {
        DateTime theTime = DateTime.Parse(time);
        return theTime;
    }
}

...

   if (DateTime.Now < "8:00 AM".Time() && DateTime.Now > "5:00 PM".Time())
   {
        // do something
   }

If you wanted to make it "pretty" you could also use an extension method:

public static class DateTimeHelper
{
    public static DateTime Time(this string time)
    {
        DateTime theTime = DateTime.Parse(time);
        return theTime;
    }
}

...

   if (DateTime.Now < "8:00 AM".Time() && DateTime.Now > "5:00 PM".Time())
   {
        // do something
   }
大姐,你呐 2024-10-01 13:30:51

如果您可以将字符串格式化为当前日期和开始/结束时间的正确格式,则可以使用类似的内容。

DateTime now = DateTime.Now;
DateTime start = Convert.ToDateTime(now.Date.ToShortDateString() + " 8:00:00 AM");
DateTime end = Convert.ToDateTime(now.Date.ToShortDateString() + " 5:00:00 PM");
if (now >= start && now < end)
{

}

我仍然认为使用其他人发布的小时方法更干净,但这种方法提供的答案类似于您提出问题的方式。

If you can format a string in the right format for the current date and start / end times you can use something like this.

DateTime now = DateTime.Now;
DateTime start = Convert.ToDateTime(now.Date.ToShortDateString() + " 8:00:00 AM");
DateTime end = Convert.ToDateTime(now.Date.ToShortDateString() + " 5:00:00 PM");
if (now >= start && now < end)
{

}

I still think it's cleaner to use the hour method posted by others, but this is a method that provides an answer similar to how you asked your question.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文