Reputation: 379
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();
}
Upvotes: 0
Views: 2956
Reputation: 5057
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.
Upvotes: 1
Reputation: 2884
Try dateValue.ToString("dddd hh:mm tt", CultureInfo.CreateSpecificCulture("en-US"))
You can refer to MSDN for more info.
Upvotes: 3
Reputation: 4107
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.
Upvotes: 2
Reputation: 15387
Please check here for Date Time Example or Date Time Format
For example
MyString = MyDateTime.ToString("dddd hh:mm tt");
Upvotes: 1
Reputation: 40980
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
}
}
Upvotes: 1
Reputation: 8937
Use DateTime.ParseExact Method to convert strings from custom format to DateTime
Upvotes: 1