changing date format from dd/MM/yyyy to MM/dd/yyyy [duplicate]
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.
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")
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.
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}");
}
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;
}
精彩评论