简体   繁体   English

如何将自定义格式设置为DateTime.Now?

[英]How to set custom format to DateTime.Now?

I want to know how can I format DateTime.Now in this same format Thursday 03:00 PM so that I can compare it to my custom format date time value, 我想知道如何格式化DateTime.Now以这种相同的格式在Thursday 03:00 PM进行比较,以便将其与自定义格式的日期时间值进行比较,

I am trying to create a simple scheduling system, that if a specific date time will be equivalent to current date time a code will run under it but I am stuck in comparing my custom dates to current date time 我正在尝试创建一个简单的调度系统,如果特定的日期时间将等于当前日期时间,则将在其下运行代码,但是我只能将自定义日期与当前日期时间进行比较

string[] schedule = { "Thursday 2:47 PM", "Thursday 2:50 PM", "Thursday 2:55 PM" };
        private void timer1_Tick(object sender, EventArgs e)
        {
            DateTime dateValue;
            foreach (var i in schedule)
            {
                if (DateTime.TryParse(i, out dateValue))
                {
                    if (dateValue == DateTime.Now)
                    {
                        //do something here
                    }
                }
            }
        }

        private void Form1_Shown(object sender, EventArgs e)
        {
            timer1.Start();
        }

Try dateValue.ToString("dddd hh:mm tt", CultureInfo.CreateSpecificCulture("en-US")) 试试dateValue.ToString("dddd hh:mm tt", CultureInfo.CreateSpecificCulture("en-US"))

  • dddd - The full name of the day of the week. dddd-星期几的全名。
  • hh - The hour, using a 12-hour clock from 01 to 12. hh-小时,使用12小时制,从01到12。
  • mm - The minute, from 00 through 59. mm-分钟,从00到59。
  • tt - The AM/PM designator. tt -AM / PM指示符。

You can refer to MSDN for more info. 您可以参考MSDN以获得更多信息。

It should work this way. 它应该以这种方式工作。

if (DateTime.TryParse(i, out dateValue))
{
  if (dateValue.ToShortTimeString() == DateTime.ToString("dd hh:mm tt"))
  {
    //do something here
  }
}

dd tells to show the current day. dd指示显示当前日期。 hh displays the hours and mm the minutes. hh显示小时, mm显示分钟。 I left ss for seconds because it seems you don't need it. 我离开ss了几秒钟,因为似乎您不需要它。 Through tt PM or AM will get displayed. 通过tt,将显示PMAM

使用DateTime.ParseExact方法将字符串从自定义格式转换为DateTime

You are trying to compare Time value with DateTime value. 您正在尝试将Time值与DateTime Time值进行比较。 I think you just want to compare that Time values of your array to current time value, in that case you can compare time part of the dates. 我认为您只想将array Time值与current时间值进行比较,在这种情况下,您可以比较日期的时间部分。

if (DateTime.TryParse(i, out dateValue))
{
  if (dateValue.ToShortTimeString() == DateTime.Now.ToShortTimeString())
  {
    //do something here
  }
}

Please check here for Date Time Example or Date Time Format 请在此处查看日期时间示例日期时间格式

For example 例如

MyString = MyDateTime.ToString("dddd  hh:mm tt");

First, you need to convert time in schedule string array into DateTime. 首先,您需要将时间表字符串数组中的时间转换为DateTime。

Link about DateTime styles: http://www.csharp-examples.net/string-format-datetime/ 有关DateTime样式的链接: http : //www.csharp-examples.net/string-format-datetime/

After that, you need to compare only time part of 2 dates. 之后,您只需要比较2个日期的时间部分。 Took the answer from here: How to compare time part of datetime 从这里得到答案: 如何比较datetime的时间部分

So, finally your code will be something like this: 因此,最终您的代码将如下所示:

string[] schedule = { "Thursday 2:47 PM", "Thursday 2:50 PM", "Thursday 2:55 PM" };
private void timer1_Tick(object sender, EventArgs e)
{
    DateTime dateValue;
    foreach (var i in schedule)
    {
        if (DateTime.TryParseExact(i, "dddd h:mm tt", CultureInfo.CreateSpecificCulture("en-US"), DateTimeStyles.None, out dateValue))
        {
            var currentDate = DateTime.Now;
            if (dateValue.DayOfWeek == currentDate.DayOfWeek && dateValue.TimeOfDay == currentDate.TimeOfDay)
            {
                //do something here
            }
        }
    }
}

private void Form1_Shown(object sender, EventArgs e)
{
    timer1.Start();
}

Of course, you can convert DateTime.Now to a string of valid format (as in your string array) but I think that it's wrong. 当然,您可以将DateTime.Now转换为有效格式的字符串(如在字符串数组中一样),但是我认为这是错误的。 If you want to check that your date is earlier or later than current date, you will have to rewrite your code. 如果要检查您的日期早于或晚于当前日期,则必须重写代码。

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

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