Convert string to specific DateTime format

前端 未结 5 1579
既然无缘
既然无缘 2021-01-07 01:17

I\'ve been googling for a while now and for the life of me can\'t seem to find a solution. I thought this would be easy but it\'s taking too long and am turning to stackover

5条回答
  •  南方客
    南方客 (楼主)
    2021-01-07 02:23

    If it's stored as a DateTime data type, it's stored correctly, but your UI is displaying it wrong. The DateTime data type always has the seconds (milliseconds, etc) regardless of how you set the value. The problem is in how it's being displayed back to the user.

    You need to display the date you want in the right string format at display time as in

    Label1.Text = startTime.ToString("MM/dd/yyyy hh:mmtt");
    

    Edit - added

    To format it in a GridView, see here: http://peterkellner.net/2006/05/24/how-to-set-a-date-format-in-gridview-using-aspnet-20using-htmlencode-property/

提交回复
热议问题