示例数据:
开始时间 结束时间
2014-09-12 02:08:20 2014-09-13 12:22:20
开始时间 结束时间
2014-09-12 02:08:20 2014-09-13 12:22:20
要拆成10分钟为一个周期的数据,(一个小时分成6段)
如下所示:
2014-09-12 02:08:20 2014-09-12 02:10:00
2014-09-12 02:10:01 2014-09-12 02:20:00
2014-09-12 02:20:01 2014-09-12 02:30:00
。。。。。。。
2014-09-13 12:10:01 2014-09-13 12:20:00
2014-09-13 12:20:01 2014-09-13 12:22:20
上述时间字段为Datetime类型的
这个算法怎么实现呢?
—-
DateTime允许做加法的。另外C#里有个TimeSpan是用来表示时间间隔的,可以用TimeSpan表示你的时间间隔,然后往DateTime上加就好了
—-
我知道这个道理,就是写不出来,十分着急。
—-
1.所有时间按照 日期+小时分组;
2.分组后,每组将分钟除以10,按照余数归成6段;
2.分组后,每组将分钟除以10,按照余数归成6段;
—- 10分
public Form1() { InitializeComponent(); this.richTextBox1.Dock = DockStyle.Fill; DateTime start = DateTime.Parse("2014-09-12 02:08:20"); DateTime stop = DateTime.Parse("2014-09-13 12:22:20"); TimeSpan span = new TimeSpan(0, 0, 10, 0); Int64 count = start.Ticks / span.Ticks; DateTime first = new DateTime((count + 1) * span.Ticks); count = stop.Ticks / span.Ticks; DateTime last = new DateTime((count + 1) * span.Ticks); List<DateTime> list = new List<DateTime>(); list.Add(start); for (DateTime temp = first; temp <= last; temp += span) { list.Add(temp); } list.Add(stop); Int32 warp = 1; foreach (DateTime time in list) { richTextBox1.AppendText(time.ToString("yyyy-MM-dd hh:mm:ss")); if ((warp % 8) != 0) richTextBox1.AppendText("\t"); else richTextBox1.AppendText("\n"); warp++; } }
运行效果
—-
是这个效果么?
—- 8分
static void Main(string[] args) { DateTime begin = DateTime.ParseExact("2014-09-12 02:08:20", "yyyy-MM-dd HH:mm:ss", null); DateTime end = DateTime.ParseExact("2014-09-13 12:22:20", "yyyy-MM-dd HH:mm:ss", null); List<DateTime> timelist = new List<DateTime>(); while (begin < end) { int hour = begin.Hour; while (hour == begin.Hour && begin < end) { if (begin.Minute % 10 == 0 && (begin.Second == 1 || begin.Second == 0)) { if (begin.Minute == 30 && begin.Second == 1) { begin = begin.AddMinutes(29).AddSeconds(59); break; } timelist.Add(begin); if (begin.Second == 1) begin = begin.AddMinutes(9).AddSeconds(59); else begin = begin.AddSeconds(1); } else { timelist.Add(begin); int tempmin = 10 - begin.Minute % 10; int tempsecond = begin.Second; begin = begin.AddMinutes(tempmin).AddSeconds(tempsecond * -1); } } } if (begin != end) timelist.Add(end); timelist.ForEach(x => Console.WriteLine(x)); Console.ReadKey(); }
—-
public static void GetTimeSpan() { DateTime starttime = Convert.ToDateTime("2014-09-12 02:08:20"); DateTime endtime = Convert.ToDateTime("2014-09-13 12:22:20"); List<DateTime> list = new List<DateTime>(); for (DateTime i = starttime; i < endtime; i = i.AddMinutes(1)) { if (i.Minute % 10 == 0) { if (list.Count == 0 && i != starttime) { list.Add(starttime); } list.Add(i); } } if (list[list.Count - 1] != endtime) { list.Add(endtime); } foreach (DateTime time in list) { Console.WriteLine(time.ToString("yyyy-MM-dd hh:mm:ss")); } }
—-
public static void GetTimeSpan() { DateTime starttime = Convert.ToDateTime("2014-09-12 02:08:20"); DateTime endtime = Convert.ToDateTime("2014-09-13 12:22:20"); List<DateTime> list = new List<DateTime>(); for (DateTime i = starttime; i <= endtime; i = i.AddMinutes(1)) { if (i.Minute % 10 == 0) { if (list.Count == 0 && i != starttime) { list.Add(starttime); } list.Add(i); } } if (list[list.Count - 1] != endtime) { list.Add(endtime); } foreach (DateTime time in list) { Console.WriteLine(time.ToString("yyyy-MM-dd hh:mm:ss")); } }
—- 8分
看完6楼的代码,我发现我少了一部分,下面是重新修改的
public Form1() { InitializeComponent(); this.richTextBox1.Dock = DockStyle.Fill; DateTime start = DateTime.Parse("2014-09-12 02:08:20"); DateTime stop = DateTime.Parse("2014-09-13 12:22:20"); TimeSpan span = new TimeSpan(0, 0, 10, 0); Int64 count = start.Ticks / span.Ticks; DateTime first = new DateTime((count + 1) * span.Ticks); count = stop.Ticks / span.Ticks; DateTime last = new DateTime((count + 1) * span.Ticks); List<DateTime> list = new List<DateTime>(); list.Add(start); for (DateTime temp = first; temp <= last; temp += span) { list.Add(temp); list.Add(temp.AddSeconds(1)); } list.Add(stop); Int32 warp = 1; foreach (DateTime time in list) { richTextBox1.AppendText(time.ToString("yyyy-MM-dd hh:mm:ss")); if ((warp % 2) != 0) richTextBox1.AppendText("\t"); else richTextBox1.AppendText("\n"); warp++; } }
效果差不多,只是计算方式上有点区别,看你需要了
—-
太好了,万分感谢大家,我用了另外一种方法也实现了,真是太谢谢大家了。
—-
说说你用的方法,让大家也学习下
CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明怎么实现时间段分割的算法!