Parse string in HH.mm format to TimeSpan

后端 未结 6 1588
栀梦
栀梦 2020-12-06 03:57

I\'m using .NET framework v 3.5 and i need to parse a string representing a timespan into TimeSpan object.

The problem is that

6条回答
  •  时光取名叫无心
    2020-12-06 04:56

    If the TimeSpan format is Twelve Hour time format like this "9:00 AM", then use TimeSpan.ParseExact method with format string "h:mm tt", like this

    TimeSpan ts = DateTime.ParseExact("9:00 AM", "h:mm tt", CultureInfo.InvariantCulture).TimeOfDay;
    

    Thanks.

提交回复
热议问题