convert MM/DD/YYYY to DD/MM/YYYY

前端 未结 3 585
傲寒
傲寒 2020-12-11 14:10

I have date in format \"MM/DD/YYYY\" e.g. \"09/25/2011\", how i can convert this in format \"DD/MM/YYYY\".

my code is:

private DateTime GetDate()
{
          


        
相关标签:
3条回答
  • 2020-12-11 14:59

    Have you tried formatting the date object as dd/MM/yyyy?

    string d = Convert.ToDateTime("09/25/2011").ToString("dd/MM/yyyy"); //returns 25/09/2011
    DateTime date = DateTime.Parse("09/25/2011", new CultureInfo("en-GB")); // returns 09/25/2011
    string d2 = date.ToString("dd/MM/yyyy"); //should return 25/09/2011
    
    0 讨论(0)
  • 2020-12-11 15:00
    string input = Console.ReadLine();
    
     string[] dtarray = input.Split('/');
    
    DateTime datechanged = new DateTime(Convert.ToInt32(dtarray[2]),Convert.ToInt32(dtarray[1]),Convert.ToInt32(dtarray[0]));
    
    0 讨论(0)
  • 2020-12-11 15:16

    I would suggest your function just does the following...

    private DateTime GetDate()
    {
       return DateTime.ParseExact("09/25/2011", "MM/dd/yyyy", null);
    }
    

    Then when you use the function...

    string formattedDate = GetDate().ToString("dd/MM/yyyy");
    
    0 讨论(0)
提交回复
热议问题