user1986761
user1986761

Reputation: 229

C# Compare Months

I am trying to check if the selected month is already past.

if (Convert.ToDateTime(DDMonths.SelectedItem.Text).Month > DateTime.Now.Month)
{
      //logic here if date is not in the past
}

DDMonths.SelectedItem.Text value is April

However I am getting the following Format exception error:

String was not recognized as a valid DateTime.

Upvotes: 1

Views: 6753

Answers (6)

Matt Johnson-Pint
Matt Johnson-Pint

Reputation: 241890

Since you are just looking for the number of the month, why parse it to a DateTime at all? You can just get it from the DateTimeFormatInfo directly:

string input = "April";

var months = DateTimeFormatInfo.CurrentInfo.MonthNames;
var monthNumber = 1 + Array.FindIndex(months, x => x.Equals(input, StringComparison.CurrentCultureIgnoreCase));
if (monthNumber > DateTime.Now.Month)
{
    // ...
}

Do think about what you want to do if it is currently April. Depending on what you are doing, you may want to compare using >=.

Also, if you are writing a desktop application, this code (and the others) are just fine. But if you are writing a web application and this code is running server-side, then you have two additional concerns:

  • The culture should match the input. You may need to use a different culture, or the InvariantCulture.
  • You are comparing to DateTime.Now - which will be in the server's time zone. So if a user in another part of the world uses this on the 1st of their new month while your server is still on the prior day, then your comparison will fail.

Upvotes: 1

Tim Schmelter
Tim Schmelter

Reputation: 460288

So the Text of your DropDownList-Item is not convertible to DateTime with the current culture. So maybe you are showing the month-name (what i assume) or the error is more subtiel. You could use the ListItem.Value to store the datetime in a specific format, for example:

"yyyyMMdd" -> "20130726"

Then you can parse it in this way:

var dt = DateTime.ParseExact("20130726", "yyyyMMdd", CultureInfo.InvariantCulture);

If you want to allow the monthname:

dt = DateTime.ParseExact("July", "MMMM", CultureInfo.InvariantCulture);

Upvotes: 1

V4Vendetta
V4Vendetta

Reputation: 38230

You should be using a ParseExact variant of DateTime

DateTime.ParseExact("April", "MMMM", CultureInfo.InvariantCulture).Month // outputs 4

you should also try using the Value (DDMonths.SelectedItem.Value) component and fill it as needed

Upvotes: 0

CodingIntrigue
CodingIntrigue

Reputation: 78595

Convert.ToDateTime cannot understand your date format, you need to use DateTime.ParseExact instead:

if(DateTime.ParseExact(DDMonths.SelectedItem.Text, "MMMM", CultureInfo.CurrentCulture).Month > DateTime.Now.Month) {
  ...
}

Upvotes: 2

Ant P
Ant P

Reputation: 25231

You can parse the month by name with the following:

DateTime.ParseExact(DDMonths.SelectedItem.Text, "MMMM", CultureInfo.CurrentCulture ).Month

However, you'd be better off making the Value of each element in DDMonths the integer value corresponding to the month instead, if possible.

Upvotes: 5

Ehsan
Ehsan

Reputation: 32719

it means that your line

 Convert.ToDateTime(DDMonths.SelectedItem.Text)

is giving you error. You should use

DateTime.ParseExact(DDMonths.SelectedItem.Text,"MMMM",CultureInfo.InvariantCulture);

Upvotes: 1

Related Questions