Convert date format of string to datetime MM/dd/yyyy when system date formate is dd/MM/yyyy

前端 未结 5 1908
逝去的感伤
逝去的感伤 2020-12-12 06:28

I need convert the date of string format \'MM/dd/yyyy\' to datetime format \'MM/dd/yyyy\' when the system date format is \'dd/MM/yyyy\'.

相关标签:
5条回答
  • 2020-12-12 06:45

    This may help:

    string dateString="05/02/2014";
    DateTime txtmyDate = DateTime.ParseExact(dateString, "MM/dd/yyyy",     
    CultureInfo.InvariantCulture);
    
    0 讨论(0)
  • 2020-12-12 06:48

    use DateTime.ParseExact

    DateTime.ParseExact("05/02/2014", "dd/MM/yyyy", CultureInfo.InvariantCulture)
                            .ToString("MM/dd/yyyy", CultureInfo.InvariantCulture);
    
    0 讨论(0)
  • 2020-12-12 06:50

    try this

    string dateString="05/02/2014";
    DateTime txtmyDate =convert.toDateTime(dateString);
    
    0 讨论(0)
  • 2020-12-12 06:50
    DateTime dt = DateTime.ParseExact("05/02/2014","MM/dd/yyyy",CultureInfo.InvariantCulture);
    
    0 讨论(0)
  • 2020-12-12 06:55

    DateTime stores a date as a numerical value, not as a string. So asking for a DateTime type of format 'MM/dd/yyyy' doesn't make much sense. If you are displaying the date in a WPF control, it will default to displaying a string that conforms to the system date format. But that is just a display string that you can manipulate using the format strings alluded to in the links above. Using @Sajeetharan's code will create a DateTime struct with the correct value internally. How you display it is up to you and your string formatting choices.

    The same goes for dates stored in a database column of type 'datetime'. The value is stored numerically. Your query editor will display the value likely in the same format as your system settings. If the date is stored as a string, then again, @Sajeetharan's code is the correct way to convert the database string into a DateTime struct.

    0 讨论(0)
提交回复
热议问题