Change format of DateTime object in c# and store back as DateTime object

前端 未结 1 659
慢半拍i
慢半拍i 2020-12-07 06:33

I want to convert string 2017-03-05 to Datetime object in c# in format yyyy-MM-dd

string startDate = \"2017-03-05\"; 
DateTime myDate = DateTime.ParseExact         


        
相关标签:
1条回答
  • 2020-12-07 06:53

    When you parse your string to DateTime, it will have no format.

    DateTime structure does not have any implicit format. It just have date and time values which is based on long field as Ticks. Format concept only applies when you try to get it's textual (aka string) representation of it. It is really important to understand the difference between a DateTime instance and it's formatted string representation.

    So, if you wanna get a specific textual format of your DateTime, you will need to get it's string representation again.

    string result = myDate.ToString("yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture);
    
    0 讨论(0)
提交回复
热议问题