Click here to Skip to main content
15,892,674 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
DateTime dateTo = DateTime.ParseExact(tb_DateTo.Text, "MM/dd/yyyy", CultureInfo.InvariantCulture);
string dateToFormat = dateFrom.ToString("dd/MM/yyyy");
Posted

Try this


C#
DateTime dateTo = DateTime.ParseExact(tb_DateTo.Text.Trim(), "MM/dd/yyyy", CultureInfo.InvariantCulture);
      string dateToFormat = dateTo.ToString("dd/MM/yyyy");
 
Share this answer
 
It's not necessarily wrong. I suspect you're getting a FormatException? When using ParseExact[^], the input string must exactly match the specified format, anything different and the parse will fail.

For example:
C#
DateTime.ParseExact("02/04/2014", "MM/dd/yyyy", CultureInfo.InvariantCulture)

Will parse just fine.

However, this:
C#
DateTime.ParseExact("02/04/2014 12:00:00", "MM/dd/yyyy", CultureInfo.InvariantCulture)

will fail
 
Share this answer
 
Comments
Member 10548723 4-Feb-14 3:53am    
how do you check if date is empty in a textbox
Maarten Kools 4-Feb-14 3:55am    
if (tb_DateTo.Text.Length != 0) // do something else
Member 10548723 4-Feb-14 4:13am    
DateTime dateFrom = DateTime.ParseExact(tb_DateFrom.Text, "dd/MM/yyyy", CultureInfo.InvariantCulture);
this still giving me string was not a valid datetime
Maarten Kools 4-Feb-14 4:15am    
Then your input is incorrect. Like I said, if you use ParseExact, the input must exactly match the pattern. Any other case will give you a FormatException.
Member 10548723 4-Feb-14 4:31am    
Ok, how you do a validation for this format dd/MM/yyyy

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900