-5

I use this code to get date

String.Format("{0:yyyy/MM/dd}", Convert.ToDateTime(calender.Text))

It work correct.

But I cannot get 3 special data so my program crashing.

in the day "31" and Month "2" , "4" , "6" for example "1397/06/31"or "1397/04/31"

How to fix it? I use persian date calendar.

jschroedl
  • 4,916
  • 3
  • 31
  • 46
emy shoj
  • 25
  • 4
  • 3
    I imagine this gves you an errror as those are not valid dates. Months 2,4, and 5 does not have 31 days. – zaza Oct 04 '18 at 05:51
  • For persian date times please refer to this answer: https://stackoverflow.com/a/9491805/1300049 – JleruOHeP Oct 07 '18 at 22:22

2 Answers2

7

Because there is no such thing as 31st of June, your conversion fails.

You should try parse it:

DateTime.TryParse("1900/06/31", out calendarDay)

And proceed only if conversion was successful (TryParse returns true)

JleruOHeP
  • 10,106
  • 3
  • 45
  • 71
5

It will crash because 31st June, 31st April and 31st February are not valid dates. Why do you want these dates to be as input?

progrAmmar
  • 2,606
  • 4
  • 29
  • 58