Click here to Skip to main content
14,979,362 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
My requirement is....In c# i need to show date format as "dd/MM/yyyy" in all server control, but in sql the date format is "MM/dd/yyyy" how ?
Posted
Updated 2-Sep-20 21:03pm
v2

Use DateTime or Date data type in the database to store the date. In C#, use DateTime type variable to store the date. In C#, use string formatting to format the date into your choice of format.

C# Example of string formatting:
C#
Console.WriteLine(YourDateTimeVariable.ToString("MM/dd/yyyy"))
   
v3
Use this method convert Date(DD/MM/yyyy) in sql format(MM/DD/yyyy)
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";
                   }
               }
           }
   
v4

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