Using DateTime.ParseExact to get only the time (without the day)

前端 未结 3 1906
情歌与酒
情歌与酒 2021-01-18 02:14

I get unexpected results when I use DateTime.ParseExact. Here\'s my test code:

Dim MinVal As DateTime = #12:00:01 AM#
Dim MaxVal As DateTime = #11:59:59 PM#         


        
3条回答
  •  臣服心动
    2021-01-18 02:29

    Documentation states:

    If format defines a time with no date element and the parse operation succeeds, the resulting DateTime value has a date of DateTime.Now.Date.

    If you want a time with no date, you can use:

    var parsedDate = DateTime.ParseExact(...);
    var timeOnly = parsedDate - parsedDate.Date;
    

提交回复
热议问题