I have a datepicker which shows date in format dd/MM/yyyy(i know i coould change there itself but by client want it that way) and in database its in format MM/dd/yyyy so i do want to convert in that way.
e.g. in text box 23/09/2010 and in c sharp its convert to mm/dd/yyyy(txtbo1.text)
Regards
Indranil.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
If you really store the date as a string in the DB, you can convert the string format in the following manner:
DateTime.ParseExact(dateTimeString, "dd/MM/yyyy", null).ToString("MM/dd/yyyy")
Method 2
If you are storing dates in a database, you should be storing them in a field with an appropriate data type. The format shouldn’t come into it. You should parse your dd/MM/yyyy text into a DateTime variable, and pass it in a parameters of a date/time type to your database.
Method 3
Use regex:
public static String DMYToMDY(String input)
{
return Regex.Replace(input,
@"b(?<day>d{1,2})/(?<month>d{1,2})/(?<year>d{2,4})b",
"${month}/${day}/${year}");
}
Method 4
using System.Globalization;
string dt = DateTime.Parse(txtDate.Text.Trim()).ToString("MM/dd/yyyy", CultureInfo.InvariantCulture);
also can be done with this
public string FormatPostingDate(string str)
{
if (str != null && str != string.Empty)
{
DateTime postingDate = Convert.ToDateTime(str);
return string.Format("{0:MM/dd/yyyy}", postingDate);
}
return string.Empty;
}
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0