且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

来自不同格式日期的总小时数.C#

更新时间:2023-01-30 10:55:09

C#中,提供了一堆方法可以将包含多种格式的日期时间的字符串转换成统一的日期时间 对象.这些方法可以识别相当多的标准日期时间格式,如果您的格式与它们不同,您甚至可以提供自己的.

In C#, there is a bunch of methods provided to convert strings that contain date times in many formats into a unified DateTime object. These methods can recognize quite a few standard date time formats, and if yours differ from them, you can even provide your own.

  • DateTime.Parse() - 将字符串转换为 DateTime 对象.如果操作失败,则会抛出异常.
  • DateTime.TryParse() - 仅在可能的情况下将字符串转换为 DateTime 对象.成功返回true,失败返回false.
  • DateTime.TryParseExact() - 将指定格式的字符串转换为 DateTime 对象.如果成功则返回 true,否则返回 false.
  • DateTime.Parse() - Converts a string to a DateTime object. If operation fails, it'll thrown an exception.
  • DateTime.TryParse() - Converts a string to a DateTime object only if possible. Returns true if successful, and false if it fails.
  • DateTime.TryParseExact() - Converts a string that is in the specified format into a DateTime object. Returns true if successful, and false otherwise.

在您的情况下,您可以使用 DateTime.TryParse() (推荐使用 DateTime.Parse() 除非您绝对确定格式是正确)像这样:

In your case, you can use DateTime.TryParse() (which is recommended over simply using DateTime.Parse() unless you're absolutely sure the format is correct) like so:

var dtStr1 = " 2019. 09. 23. 14:54:23";
var dtStr2 = "2019.09.23 14:54:23";
var dtStr3 = "2019-09-23 14:54:23";

DateTime.TryParse(dtStr1, out DateTime dt1);
DateTime.TryParse(dtStr2, out DateTime dt2);
DateTime.TryParse(dtStr3, out DateTime dt3);

一旦转换为 DateTime 对象,它就不再具有与之关联的格式.它是一个结构,因此只有成员变量和方法.因此,要计算总小时数等,您可以使用提供的方法.

Once converted to a DateTime object, it no longer has a format associated with it. It's a structure, and hence only has member variables and methods. So to calculate total hours etc. you can use provided methods.

假设您想计算一天工作开始和结束之间的时间.您可以将它们转换为 DateTime 对象,然后从其他对象中减去一个,这将给您一个 TimeSpam 对象.

Say you want to calculate time between day's work start and end. You can convert those into DateTime objects, then subtract one from the others which will give you a TimeSpam object.

var dtStrStart = "2019.09.23 08:23:12";
var dtStrEnd = "2019.09.23 16:17:28";

DateTime.TryParse(dtStrStart, out DateTime dtStart);
DateTime.TryParse(dtStrEnd, out DateTime dtEnd);

var diff = dtEnd - dtStart;

现在,TimeSpan 对象,也就是这里的 diff,将为您提供一系列不同的属性,以小时、分钟等为单位.

Now the TimeSpan object, which is diff here, will give you a bunch of properties with difference in hours, minutes etc.

TimeSpan.DaysTimeSpan.Minutes 等会以天、分钟等形式为您提供时间.

The TimeSpan.Days, TimeSpan.Minutes etc will give you the time in days, minutes etc.

Console.WriteLine(diff.Days);
Console.WriteLine(diff.Hours);
Console.WriteLine(diff.Minutes);
Console.WriteLine(diff.Seconds);
Console.WriteLine(diff.Milliseconds);

输出:

0

7

54

16

0

TimeSpan.TotalMinutes 等将为您提供相应单位的整个时间段.

The TimeSpan.TotalMinutes etc will give you the entire time period in respective units.

Console.WriteLine(diff.TotalDays);
Console.WriteLine(diff.TotalHours);
Console.WriteLine(diff.TotalMinutes);
Console.WriteLine(diff.TotalSeconds);
Console.WriteLine(diff.TotalMilliseconds);

输出:

0.329351851851852

0.329351851851852

7.90444444444444

7.90444444444444

474.266666666667

474.266666666667

28456

28456000

相反,当您在数据库中存储数据时,您必须再次使用标准格式,例如 datetimedatetime2.建议您使用 datetime2更多信息.

And conversely, when you're storing data in the database, you must again use a standard format, such as datetime or datetime2. It's advised you use datetime2, more info here.