如何将时间跨度拆分为分割天数 [英] How do I split a timespan to splitted days
问题描述
嗨伙计们,
首先抱歉我的英语不好!但我有一个问题,我无法解决...
我有一个对象(约会),它有几个属性(其中2个名为StartDate和EndDate )
例如:
- StartDate = 2014/3/3 17:00:00
- EndDate = 2014/3/8 08: 00:00
是否可以按如下方式拆分这些约会:
约会=> StartDate = 2014/3/3 17:00:00 EndDate = 2014/3/3 23:59:59 +原始属性
约会=> StartDate = 2014/3/4 00:00:00 EndDate = 2014/3/4 23:59:59 +原始属性
约会=> StartDate = 2014/3/5 00:00:00 EndDate = 2014/3/5 23:59:59 +原始属性
约会=> StartDate = 2014/3/6 00:00:00 EndDate = 2014/3/6 23:59:59 +原始属性
约会=> StartDate = 2014/3/7 00:00:00 EndDate = 2014/3/7 23:59:59 +原始属性
约会=> StartDate = 2014/3/8 00:00:00 EndDate = 2014/3/8 08:00:00 +原始属性
并将此作为列表返回<约会> (分割约会)
似乎可能但是此刻我无法弄明白
我已经搜索了这个问题,但谷歌都没有找到答案...
非常感谢提前!
目前我有蹩脚的代码...
Hi folks,
First of all sorry for my bad English! but I have a problem that I can't solve ...
I have an object (Appointment) which has several attributes (2 of them are named StartDate and EndDate)
e.g.:
- StartDate = 2014/3/3 17:00:00
- EndDate = 2014/3/8 08:00:00
Is it possible to split these Appointments as following:
Appointment => StartDate = 2014/3/3 17:00:00 EndDate = 2014/3/3 23:59:59 + original attributes
Appointment => StartDate = 2014/3/4 00:00:00 EndDate = 2014/3/4 23:59:59 + original attributes
Appointment => StartDate = 2014/3/5 00:00:00 EndDate = 2014/3/5 23:59:59 + original attributes
Appointment => StartDate = 2014/3/6 00:00:00 EndDate = 2014/3/6 23:59:59 + original attributes
Appointment => StartDate = 2014/3/7 00:00:00 EndDate = 2014/3/7 23:59:59 + original attributes
Appointment => StartDate = 2014/3/8 00:00:00 EndDate = 2014/3/8 08:00:00 + original attributes
And return this as a List<appointments> (splitted Appointments)
It seems possible but at this moment I can't figure it out
I have googled for this problem but neither google doesn't find the answer...
Many thanks in advance!
Crappy code I have for the moment...
private List<Appointment> SplitMultidayAppointments(Appointment app)
{
DateTime startdate = app.StartDate;
DateTime enddate = app.EndDate;
List<Appointment> splitdates = new List<Appointment>();
DateTime newStartDate = new DateTime();
DateTime newEndDate = new DateTime();
Appointment split = app;
if ((enddate - startdate).TotalDays > 1) //firstdaycheck
{
split.StartDate = app.StartDate;
split.EndDate = new DateTime(split.StartDate.Year, split.StartDate.Month, split.StartDate.Day, 23, 59, 59);
splitdates.Add(split);
newStartDate = split.EndDate.AddSeconds(1);
}
while (DateTime.Compare(enddate, newStartDate) >0)
{
split.StartDate = newStartDate;
split.EndDate = new DateTime(newStartDate.Year, newStartDate.Month, newStartDate.Day, 23, 59, 59);
splitdates.Add(split);
newStartDate = newStartDate.AddDays(1);
}
split.StartDate = newStartDate;
split.EndDate = app.EndDate;
splitdates.Add(split);
return splitdates;
}
推荐答案
非常接近你所拥有的......
Pretty close to what you have...
class Appointment
{
public DateTime StartDate { get; set; }
public DateTime EndDate { get; set; }
}
static void Main(string[] args)
{
var fullAppt = new Appointment() { StartDate = new DateTime(2014, 3, 3, 17, 0, 0), EndDate = new DateTime(2014, 3, 8, 8, 0, 0) }; //EDIT: corrected the example Start/End values
List<Appointment> splitAppt = SplitAppointment(fullAppt);
}
static List<Appointment> SplitAppointment(Appointment appt)
{
TimeSpan oneDay = TimeSpan.FromDays(1);
TimeSpan endOfDay = oneDay - TimeSpan.FromTicks(1); // last possible time of a day
List<Appointment> result = new List<Appointment>();
DateTime currentDay = appt.StartDate;
DateTime endOfCurrentDay = currentDay.Date + endOfDay;
while (endOfCurrentDay < appt.EndDate)
{
result.Add(new Appointment() { StartDate = currentDay, EndDate = endOfCurrentDay });
// copy other attributes of appt
currentDay = currentDay.Date + oneDay; // EDIT: changed this line
endOfCurrentDay += oneDay;
}
if (currentDay < appt.EndDate)
{
result.Add(new Appointment() { StartDate = currentDay, EndDate = appt.EndDate });
// copy other attributes of appt
}
return result;
}
编辑:见上文。
Edited: See above.
这篇关于如何将时间跨度拆分为分割天数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!