Change the date format from dd / MM / yyyy to MM / dd / yyyy

I have a datepicker that shows the date in dd / MM / yyyy format (I know that I can change it myself, but the client needs it), and in the database in MM / dd / yyyy format, so I want to convert this way.

eg. in the text box 09/23/2010 and with a sharp conversion to mm / dd / yyyy (txtbo1.text)

Relationship Indranil.

+3
source share
4 answers

If you really store the date as a string in the database, you can convert the string format as follows:

DateTime.ParseExact(dateTimeString, "dd/MM/yyyy", null).ToString("MM/dd/yyyy")
+10
source

, . . dd/MM/yyyy DateTime / .

+8
 using System.Globalization;

 string dt = DateTime.Parse(txtDate.Text.Trim()).ToString("MM/dd/yyyy", CultureInfo.InvariantCulture);

can also be done using 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;
 }
+1
source

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}");
    }
0
source

Source: https://habr.com/ru/post/1746559/


All Articles