时间段分割的算法,着急请教大家。

frustrate2 2014-11-26 09:24:58
示例数据:
开始时间 结束时间
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类型的

这个算法怎么实现呢?


...全文
417 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
扯淡砖家 2014-11-27
  • 打赏
  • 举报
回复
引用 10 楼 frustrate2 的回复:
太好了,万分感谢大家,我用了另外一种方法也实现了,真是太谢谢大家了。
说说你用的方法,让大家也学习下
FitPual 2014-11-26
  • 打赏
  • 举报
回复

    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"));
            }
 
        }

FitPual 2014-11-26
  • 打赏
  • 举报
回复

        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"));
            }

        }
游离失所 2014-11-26
  • 打赏
  • 举报
回复


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();
}





扯淡砖家 2014-11-26
  • 打赏
  • 举报
回复
是这个效果么?
扯淡砖家 2014-11-26
  • 打赏
  • 举报
回复
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++;
}
}

运行效果
smthgdin_020 2014-11-26
  • 打赏
  • 举报
回复
1.所有时间按照 日期+小时分组; 2.分组后,每组将分钟除以10,按照余数归成6段;
frustrate2 2014-11-26
  • 打赏
  • 举报
回复
我知道这个道理,就是写不出来,十分着急。
引用 1 楼 wow818wow 的回复:
DateTime允许做加法的。另外C#里有个TimeSpan是用来表示时间间隔的,可以用TimeSpan表示你的时间间隔,然后往DateTime上加就好了
扯淡砖家 2014-11-26
  • 打赏
  • 举报
回复
DateTime允许做加法的。另外C#里有个TimeSpan是用来表示时间间隔的,可以用TimeSpan表示你的时间间隔,然后往DateTime上加就好了
frustrate2 2014-11-26
  • 打赏
  • 举报
回复
太好了,万分感谢大家,我用了另外一种方法也实现了,真是太谢谢大家了。
扯淡砖家 2014-11-26
  • 打赏
  • 举报
回复
看完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++;
}
}


效果差不多,只是计算方式上有点区别,看你需要了

110,537

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧