Skip to content

Using Month Only in DateTime

John Smith

Asked Sep 9, 2010, 1:20 PM EDT6 answers
Share
LinkedInX
I only want to change the MonthlyCalender Month to a variable(May) but for some reason i have to enter the full date which breaks my code, as i search for the month in the variable and then match it so it then excutes the code when it finds the string, below is the code;

Code
             if (textDueDate.Contains("May"))
            {
                DateTime dt = new DateTime();
                clndDueDate.SetDate(dt.AddMonths(05));
                clndDueDate.Update();
                MessageBox.Show("Inside May");
            }
            else
            {

            }

This does nothing, So do i use SetDate property in a different way... since i added the month on the Monthly calender.

MessageBox is just my debugger :P

Maybe substring?

Java
 DateTime dt = new DateTime(2001, 01, 01); //Works with full date

 DateTime dt = new DateTime(null, 01, null); //Not working ;(

Thanks for reading

6 Answers

Know the answer? Post it. Somebody with the same question will find it here.

  • Bechir Bejaoui
    0
    Is your variable where your searching is type of string?
    0
  • 0
    Yes and i'll upload the rest of the source code
    0
  • Bechir Bejaoui
    0
    Hi
    I have a question why in May if section the code is different?
    0
  • 0
    Because i was trying so many different things, but doesn't seem to make a difference as they don't work.

    Since MonthlyCalender doesn't accept month only for some reason from the DateTime, maybe i should try DateTimePicker.
    0
  • 0
    I prefer not going with string in the month.  If culture varies, then your code wont work. U better go with Month Property.  As you said, u always want to use month, just ignore rest.  There is no constructor in DateTime which takes nullable values, which is not meaning ful way.
    Otherwise create your own month enum .
     
    0
  • Bechir Bejaoui
    0
    Hi
    I m agree with you  krishna, so John could refactor his code by introducting an enumeration
    enum Months{Jun,Feb,Mar,.........}; to avoid hard coding with strings
    0