Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
My textbox have a jquery calender which have a format dd/MM/yy.I've picked a date like 24/03/2013,so my txtdate.text= "24/03/2013".

Now in my code behind how can convert this into datetime format?I've tried
C#
Convert.Todatetime(txtdate.text)
but facing error.Can anyone solve this?
Posted
Updated 8-Oct-13 18:30pm
v2
Comments
Harshil_Raval 9-Oct-13 0:17am    
It seems confusing, because you said that your jquery calander have format "dd/MM/yy", then how can you get value like "24/03/2013"? it should be "24/03/13". correct me if i am wrong.

Use the parse functions available in DateTime.

C#
DateTime.ParseExact(txtdate.Text, "dd/MM/yy", CultureInfo.InvariantCulture);


DateTime.ParseExact[^]
 
Share this answer
 
v2
Comments
Thanks7872 9-Oct-13 0:26am    
This should be high lighted as green one..:-) +5..!
Ron Beyer 9-Oct-13 0:28am    
Thanks!
Try this

C#
IFormatProvider provider = new System.Globalization.CultureInfo("en-CA", true);
 String datefromtime = txtFromdate.Text.Trim();
 String datetotime = txtTodate.Text.Trim();
 DateTime dtf = DateTime.Parse(datefromtime, provider,             System.Globalization.DateTimeStyles.NoCurrentDateDefault);
 DateTime dtt = DateTime.Parse(datetotime, provider, System.Globalization.DateTimeStyles.NoCurrentDateDefault);
 
Share this answer
 
v2
Check following link
simple to understand and explain with Examples

datetime-parse
 
Share this answer
 
// If the string has been input by an end user, you might
// want to format it according to the current culture:
// IFormatProvider culture = System.Threading.Thread.CurrentThread.CurrentCulture;

DateTime dt2 = DateTime.Parse(date, culture, System.Globalization.DateTimeStyles.AssumeLocal);
 
Share this answer
 
v2
coursebatchinput.Start_Date = Convert.ToDateTime(txtdate.Text).ToString("dd/MM/yyyy");
 
Share this answer
 
Use this method convert Date(DD/MM/yyyy) in sql format(MM/DD/yyyy)
Collapse | Copy Code
C#
public static string ConvertDtFormat(string dateTimeString)
           {
               dateTimeString = dateTimeString.Trim();
               while (dateTimeString.Contains("  "))
               {
                   dateTimeString = dateTimeString.Replace("  ", " ");
               }
 
               if (dateTimeString == null || dateTimeString.Length == 0)
               {
                   return string.Empty;
               }
               else
               {
                   DateTime convertedDateTime = new DateTime();
                   string userDateFormat = HMS.DEFAULT_DATE_FORMAT;
 
                   try
                   {
                       if (userDateFormat.Trim().Contains("dd/MM/yyyy"))
                           convertedDateTime = DateTime.ParseExact(dateTimeString, format_dmy, CultureInfo.InvariantCulture,
                                                                   DateTimeStyles.AllowLeadingWhite | DateTimeStyles.AllowTrailingWhite);
                       else if (userDateFormat.Trim().Contains("MM/dd/yyyy"))
                           convertedDateTime = DateTime.ParseExact(dateTimeString, format_mdy, CultureInfo.InvariantCulture,
                                                                   DateTimeStyles.AllowLeadingWhite | DateTimeStyles.AllowTrailingWhite);
 
                       return convertedDateTime.ToString("MMM dd, yyyy hh:mm tt");
                   }
                   catch
                   {
                       return "Invalid DateTime";
                   }
               }
           }
 
Share this answer
 

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