Muh*_*ais 4 c# asp.net time entity-framework
我必须将字符串值转换为TimeSpan. 但它显示错误。
字符串未被识别为有效的时间跨度。
代码是:
TimeSpan _time = TimeSpan.Parse("08:55 AM");
Run Code Online (Sandbox Code Playgroud)
我知道它可以解析 中的字符串值"08:55"。但我不需要那个。我必须在字符串中使用 AM 或 PM。在数据库中,列数据类型是time(7),我正在使用entity framework.
SQLTime数据类型不存储一天中的时间;相反,它将时间存储为自午夜以来的毫秒数。
将 AM 版本转换"08:55"为时间跨度相当于“自午夜起 8 小时 55 分钟”,而 PM 版本则为"20:55"“自午夜起 20 小时 55 分钟”。该TimeSpan对象本身并不执行此计算,但您可以模拟结果。
using System;
public class Program
{
public static void Main()
{
Console.WriteLine("Hello World");
DateTime timespan = DateTime.Parse("08:55 AM"); //gives us a DateTime object
DateTime timespan2 = DateTime.Parse("08:55 PM");
TimeSpan _time = timespan.TimeOfDay; //returns a TimeSpan from the Time portion
TimeSpan _time2 = timespan2.TimeOfDay;
Console.WriteLine(_time);
Console.WriteLine(_time2);
}
}
Run Code Online (Sandbox Code Playgroud)
https://dotnetfiddle.net/XVLVPl