简体   繁体   中英

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,

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

  • dddd - The full name of the day of the week.
  • hh - The hour, using a 12-hour clock from 01 to 12.
  • mm - The minute, from 00 through 59.
  • tt - The AM/PM designator.

You can refer to MSDN for more info.

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. hh displays the hours and mm the minutes. I left ss for seconds because it seems you don't need it. Through tt PM or AM will get displayed.

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

You are trying to compare Time value with DateTime value. 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.

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.

Link about DateTime styles: http://www.csharp-examples.net/string-format-datetime/

After that, you need to compare only time part of 2 dates. Took the answer from here: How to compare time part of 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. If you want to check that your date is earlier or later than current date, you will have to rewrite your code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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