在两个DateTimes之间存储TimeSpan [英] Storing a TimeSpan between two DateTimes

查看:122
本文介绍了在两个DateTimes之间存储TimeSpan的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一种使用C#存储和操作日期范围的方法,并将数据存储在SQL数据库中。存储两个DateTimes是最好的方法吗?

I need a way to store and manipulate a date range using C# and storing the data in a SQL database. Is storing two DateTimes the best way to go about this?

例如,我需要员工通过使用DatePicker选择开始日期和结束日期来选择他们在特定项目上工作的时间。

For example I need to employees to be able to select the duration they spent working on a specific project by selecting the start date and end date using a DatePicker.

我还有以下进一步的要求:

I have the following further requirements:


  • 我需要支持半天在开始
    和/或持续时间的结束。

  • 我需要能够计算两个日期之间的
    天数(作为
    双,0.5是半天)。

  • 我需要能够计算
    两个日期之间的
    工作天数(双重)。

  • 时间跨度需要显示在
    jquery日历上。

  • 最低
    持续时间为半天。

  • I need to support half days at start and/or end of the duration.
  • I need to be able to calculate the number of days between the two dates (as a double where 0.5 is half a day).
  • I need to be able to calculate the number of business days between the two dates (as a double).
  • The time span needs to be displayed on a jquery calendar.
  • The minimum duration is half a day.

5月24日至5月27日全天:
2011-05-24 12:00:00.000 => 2011-05-28 00:00:00.000

A Date Range of 1/2 Day From 24th May to a full day 27th May: 2011-05-24 12:00:00.000 => 2011-05-28 00:00:00.000

五月二十四日至五月二十五日全日的日期范围:
2011-05-24 00:00:00.000 => 2011-05-27 12:00:00.000

A Date Range of Full Day From 24th May to a 1/2 day 27th May: 2011-05-24 00:00:00.000 => 2011-05-27 12:00:00.000

5月24日半天:
2011-05-24 12:00:00.000 => 2011-05-25 00:00:00.000

A Half Day on 24th May: 2011-05-24 12:00:00.000 => 2011-05-25 00:00:00.000

5月24日的一整天:
2011-05-24 00:00:00.000 => 2011-05-25 00:00:00.000

A Full Day on 24th May: 2011-05-24 00:00:00.000 => 2011-05-25 00:00:00.000

这个表示是否有意义?我应该考虑在StartDate和TimeSpan中存储DateTime,并考虑到我的要求?

Does this representation make sense? Should I rather look at storing a DateTime for the StartDate and a TimeSpan taking into account my requirements?

编辑:

我的结束日期的表示是否有意义?所以第二个可能会被保存为2011-05-03 00:00:00.000,因为这是持续时间结束。考虑到这一点,我需要从一个日历中显示的日期中减去一天。

Does my representation of end date make sense? So that 2nd of may will be saved as '2011-05-03 00:00:00.000' because that is when the duration ends. Bearing this in mind I'll need to subtract a day from the end date when displaying this in a calendar..

推荐答案

我建议将开始和结束日期保存到数据库。差异可以随时计算。

I suggest to save the start and end date to your database. The difference can always be calculated.

日期范围的关键方面是如何处理边界。您可以使用映射器进行开始/结束日期,以确保正确的时间计算(内部/触摸):

The critical aspect of date ranges is how to handle the boundaries. You can use a mapper for the start/end date to ensure correct time calculations (Inside/Touching):

// ----------------------------------------------------------------------
public void TimePeriodMapperSample()
{
  TimeCalendar timeCalendar = new TimeCalendar();
  CultureInfo ci = CultureInfo.InvariantCulture;

  DateTime start = new DateTime( 2011, 3, 1, 13, 0, 0 );
  DateTime end = new DateTime( 2011, 3, 1, 14, 0, 0 );

  Console.WriteLine( "Original start: {0}",
                     start.ToString( "HH:mm:ss.fffffff", ci ) );
  // > Original start: 13:00:00.0000000
  Console.WriteLine( "Original end: {0}",
                     end.ToString( "HH:mm:ss.fffffff", ci ) );
  // > Original end: 14:00:00.0000000

  Console.WriteLine( "Mapping offset start: {0}", timeCalendar.StartOffset );
  // > Mapping offset start: 00:00:00
  Console.WriteLine( "Mapping offset end: {0}", timeCalendar.EndOffset );
  // > Mapping offset end: -00:00:00.0000001

  Console.WriteLine( "Mapped start: {0}",
    timeCalendar.MapStart( start ).ToString( "HH:mm:ss.fffffff", ci ) );
  // > Mapped start: 13:00:00.0000000
  Console.WriteLine( "Mapped end: {0}",
    timeCalendar.MapEnd( end ).ToString( "HH:mm:ss.fffffff", ci ) );
  // > Mapped end: 13:59:59.9999999
} // TimePeriodMapperSample

查看文章<一个href =http://www.codeproject.com/KB/datetime/TimePeriod.aspx =nofollow> .NET时间段库(部分日历时间段)。

Check out the article Time Period Library for .NET (section Calendar Time Periods).

这篇关于在两个DateTimes之间存储TimeSpan的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆