请高人帮修改一下算法!

lanxing106 2008-07-11 10:57:37
请问一下,我想计算指定给定时间的前六个月中每半个月的区间范围
如下,我给定2008-7-1这样的时间,那么,结果显示为:
2008-6-1 2008-6-15
2008-6-16 2008-6-30
2008-5-1 2008-5-15
2008-5-16 2008-5-31
2008-4-1 2008-4-15
2008-4-16 2008-4-30
2008-3-1 2008-3-15
2008-3-16 2008-3-31
2008-2-1 2008-2-15
2008-2-16 2008-2-29
2008-1-1 2008-1-15
2008-1-16 2008-1-31

但如果我给定了 2008-7-22 这样的时间 ,那么结果如下:
2008-6-1 2008-6-15
2008-6-16 2008-6-30
2008-5-1 2008-5-15
2008-5-16 2008-5-31
2008-4-1 2008-4-15
2008-4-16 2008-4-30
2008-3-1 2008-3-15
2008-3-16 2008-3-31
2008-2-1 2008-2-15
2008-2-16 2008-2-29
2008-1-1 2008-1-15
2008-1-16 2008-1-31

大家可以看到结果是一样的, 那7月份的22天都不算了,而且把1月份给算了。其实应该把7月份的那22分割为15+7的,我想显示的结果下:

2008-6-1 2008-6-15
2008-6-16 2008-6-30
2008-5-1 2008-5-15
2008-5-16 2008-5-31
2008-4-1 2008-4-15
2008-4-16 2008-4-30
2008-3-1 2008-3-15
2008-3-16 2008-3-31
2008-2-1 2008-2-15
2008-2-16 2008-2-29
2008-1-1 2008-1-15
2008-1-16 2008-1-31

但如果我给定了 2008-7-22 这样的时间 ,那么结果如下:
2008-7-1 2008-7-15
2008-7-16 2008-7-22
2008-6-1 2008-6-15
2008-6-16 2008-6-30
2008-5-1 2008-5-15
2008-5-16 2008-5-31
2008-4-1 2008-4-15
2008-4-16 2008-4-30
2008-3-1 2008-3-15
2008-3-16 2008-3-31
2008-2-1 2008-2-15
2008-2-16 2008-2-29
2008-1-22 2008-1-31

谁能帮我将下面的算法修改一下,以能显示上面给出任意年月日,都能准确的计算

//时间算法
private string[,] SelectDate(string date)
{
string[,] timeDate = new string[12, 2];
DateTime theDate = DateTime.ParseExact(date, "yyyy-M-d", null);
theDate = theDate.AddMonths(-1);

DateTime startDate, endDate;

for (int i = 0, j = 0; i < 6; i++, j++)
{

startDate = new DateTime(theDate.Year, theDate.Month, 1);
endDate = new DateTime(theDate.Year, theDate.Month, 15);

timeDate[j, 0] = startDate.ToString("yyyy-M-d");
timeDate[j, 1] = endDate.ToString("yyyy-M-d");
Response.Write(startDate.ToString("yyyy-M-d"));
Response.Write("\t");
Response.Write(endDate.ToString("yyyy-M-d"));
Response.Write("<br/>");

startDate = new DateTime(theDate.Year, theDate.Month, 16);
endDate = new DateTime(theDate.Year, theDate.Month, DateTime.DaysInMonth(theDate.Year, theDate.Month));
j = j + 1;
timeDate[j, 0] = startDate.ToString("yyyy-M-d");
timeDate[j, 1] = endDate.ToString("yyyy-M-d");
Response.Write(startDate.ToString("yyyy-M-d"));
Response.Write("\t");
Response.Write(endDate.ToString("yyyy-M-d"));

Response.Write("<br/>");

theDate = theDate.AddMonths(-1);
}
return timeDate;
}

如果任何不明白,需要我不补充的,请狂问我~!
...全文
285 21 打赏 收藏 转发到动态 举报
写回复
用AI写文章
21 条回复
切换为时间正序
请发表友善的回复…
发表回复
lanxing106 2008-07-15
  • 打赏
  • 举报
回复
恩 大哥 太好了 我等。。。
我姓区不姓区 2008-07-15
  • 打赏
  • 举报
回复
稍等一下,帮你搞搞
lanxing106 2008-07-15
  • 打赏
  • 举报
回复
今天还没有人能给出完整的代码吗?
lanxing106 2008-07-15
  • 打赏
  • 举报
回复
谢谢,偶等了5天了。。。终于让我等到了。JF啦
我姓区不姓区 2008-07-15
  • 打赏
  • 举报
回复
改一下

private DateTime[,] GetDateTimes(DateTime dt)
{
DateTime[,] result = new DateTime[13, 2];
int daysOfdt = DateTime.DaysInMonth(dt.Year, dt.Month);
int index = 0;
if (dt.Day >= 1 && dt.Day <= 15)
{
result[0, 0] = new DateTime(dt.Year, dt.Month, 1);
result[0, 1] = new DateTime(dt.Year, dt.Month, dt.Day);
index = 1;
}
else if (dt.Day > 15 && dt.Day <= daysOfdt)
{
result[0, 0] = new DateTime(dt.Year, dt.Month, 1);
result[0, 1] = new DateTime(dt.Year, dt.Month, 15);
result[1, 0] = new DateTime(dt.Year, dt.Month, 16);
result[1, 1] = new DateTime(dt.Year, dt.Month, dt.Day);
DateTime dtTemp = dt.AddMonths(-6);
result[12, 0] = new DateTime(dtTemp.Year, dtTemp.Month, 16);
result[12, 1] = new DateTime(dtTemp.Year, dtTemp.Month, dtTemp.Day);
index = 2;
}
int count = 1;
for (int i = index; i < 12; i += 2)
{
DateTime dtTemp = dt.AddMonths(-count);
int daysOfdtTemp = DateTime.DaysInMonth(dtTemp.Year, dtTemp.Month);
if (count != 6)
{
result[i, 0] = new DateTime(dtTemp.Year, dtTemp.Month, 1);
result[i, 1] = new DateTime(dtTemp.Year, dtTemp.Month, 15);
result[i + 1, 0] = new DateTime(dtTemp.Year, dtTemp.Month, 16);
result[i + 1, 1] = new DateTime(dtTemp.Year, dtTemp.Month, daysOfdtTemp);
count++;
}
else if (index == 1)
{
result[11, 0] = new DateTime(dtTemp.Year, dtTemp.Month, dtTemp.Day);
result[11, 1] = new DateTime(dtTemp.Year, dtTemp.Month, 15);
result[12, 0] = new DateTime(dtTemp.Year, dtTemp.Month, 16);
result[12, 1] = new DateTime(dtTemp.Year, dtTemp.Month, daysOfdt);
}
}
return result;
}

lanxing106 2008-07-15
  • 打赏
  • 举报
回复
[Quote=引用 18 楼 ojlovecd 的回复:]
C# code
private DateTime[,] GetDateTimes(DateTime dt)
{
DateTime[,] result = new DateTime[13, 2];
int daysOfdt = DateTime.DaysInMonth(dt.Year, dt.Month);
int index = 0;
if (dt.Day >= 1 && dt.Day <= 15)
{
result[0, 0] = new DateTime(dt.Year, dt.Month, 1);
result[0, 1] = new DateTime(dt.Year, dt.Month, dt.Day);

[/Quote]
当为2008-7-13 应该是 2008-7-13到2008-1-13的时间段
下面的就是算出结果:
2008-07-01 2008-07-13
2008-06-01 2008-06-15
2008-06-16 2008-06-30
2008-05-01 2008-05-15
2008-05-16 2008-05-31
2008-04-01 2008-04-15
2008-04-16 2008-04-30
2008-03-01 2008-03-15
2008-03-16 2008-03-31
2008-02-01 2008-02-15
2008-02-16 2008-02-29
2008-01-01 2008-01-15
2008-01-16 2008-01-31
可是这个代码把1月份的所有的天数都算了
我姓区不姓区 2008-07-15
  • 打赏
  • 举报
回复

private DateTime[,] GetDateTimes(DateTime dt)
{
DateTime[,] result = new DateTime[13, 2];
int daysOfdt = DateTime.DaysInMonth(dt.Year, dt.Month);
int index = 0;
if (dt.Day >= 1 && dt.Day <= 15)
{
result[0, 0] = new DateTime(dt.Year, dt.Month, 1);
result[0, 1] = new DateTime(dt.Year, dt.Month, dt.Day);
index = 1;
}
else if (dt.Day > 15 && dt.Day <= daysOfdt)
{
result[0, 0] = new DateTime(dt.Year, dt.Month, 1);
result[0, 1] = new DateTime(dt.Year, dt.Month, 15);
result[1, 0] = new DateTime(dt.Year, dt.Month, 16);
result[1, 1] = new DateTime(dt.Year, dt.Month, dt.Day);
DateTime dtTemp = dt.AddMonths(-6);
result[12, 0] = new DateTime(dtTemp.Year, dtTemp.Month, 16);
result[12, 1] = new DateTime(dtTemp.Year, dtTemp.Month, dtTemp.Day);
index = 2;
}
int count = 1;
for (int i = index; i < 12; i += 2)
{
DateTime dtTemp = dt.AddMonths(-count);
int daysOfdtTemp = DateTime.DaysInMonth(dtTemp.Year, dtTemp.Month);
if (count != 6 || index == 1)
{
result[i, 0] = new DateTime(dtTemp.Year, dtTemp.Month, 1);
result[i, 1] = new DateTime(dtTemp.Year, dtTemp.Month, 15);
result[i + 1, 0] = new DateTime(dtTemp.Year, dtTemp.Month, 16);
result[i + 1, 1] = new DateTime(dtTemp.Year, dtTemp.Month, daysOfdtTemp);
count++;
}
}
return result;
}

protected void Page_Load(object sender, EventArgs e)
{
DateTime dt = new DateTime(2008, 7, 22);
DateTime[,] dates = GetDateTimes(dt);
for (int i = 0; i < dates.GetLength(0); i++)
{
Response.Write(dates[i, 0].ToString("yyyy-MM-dd") + " " + dates[i, 1].ToString("yyyy-MM-dd") + "<br>");
}
}

lanxing106 2008-07-14
  • 打赏
  • 举报
回复
难道没有人可以给我一个完整无误的算法吗?我哭呀~!
lanxing106 2008-07-14
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 haonanq20015921 的回复:]
引用 11 楼 lanxing106 的回复:
引用 10 楼 bollton 的回复:
private string[,] SelectDate(string date)
{
string[,] timeDate = new string[12, 2];
DateTime theDate = DateTime.ParseExact(date, "yyyy-M-d", null);
int days = theDate.Day;
theDate = theDate.AddMonths(-1);

DateTime startDate, endDate;
for (int i = 0, j = 0; i < 6; i++, j…
[/Quote]

这个算法是我求别人写的 我不会写 能给我个完整的代码吗
7仔 2008-07-14
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 lanxing106 的回复:]
引用 10 楼 bollton 的回复:
private string[,] SelectDate(string date)
{
string[,] timeDate = new string[12, 2];
DateTime theDate = DateTime.ParseExact(date, "yyyy-M-d", null);
int days = theDate.Day;
theDate = theDate.AddMonths(-1);

DateTime startDate, endDate;
for (int i = 0, j = 0; i < 6; i++, j++)
{


[/Quote]
这个6采用动态方式获取 ,设置成变量 不久可以了吗
jimh 2008-07-11
  • 打赏
  • 举报
回复
string[,] timeDate = new string[12, 2]; //这里已经错了,最大的情况应该是13个时间段
DateTime theDate = DateTime.ParseExact(date, "yyyy-M-d", null);
theDate = theDate.AddMonths(-1); //这里有问题,应该从当前日期的前一天开始。

这里给个思路,没有考虑计算31号等时间前推的问题,因为搂主没有回复相关的问题。
endDate = new DateTime(2008,07,22);

while (enddate > 半年前的时间[2008-01-22])
{
endDate = theDate.AddDays(-1);
beginDate = (endDate.Days >= 15)? new DateTime(endDate.Year, endDate.Month, 16): new DateTime(endDate.Year, endDate.Month, 1);
response.Write("......");
endDate = beginDate.AddDays(-1);
}
xlong224 2008-07-11
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 keyake863 的回复:]
获取当前日期。大于15天表示当前已经过了半个月,否则在前半个月中。
后面的逻辑部门你自己补齐吧。

C# code
int days = DateTime.ParseExact(date, "yyyy-M-d", null).Day;
if(days>=15)
{}
else
{}
[/Quote]
lanxing106 2008-07-11
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 jimh 的回复:]
有些边际的问题想确认一下,比如8月30号,那时间范围是02-30 --- 08-30, 02-30不存在,应该怎么算,
[/Quote]
如果是8月30号的话
那么就应该是从2月份的最后一天(28或者29号)---8月30之间
我上面的那个函数已经解决了那个问题,可以运行一下的
lanxing106 2008-07-11
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 syc958 的回复:]
看不懂!请说的明白就!
[/Quote]
请问你哪里不明白,请说具体些
jimh 2008-07-11
  • 打赏
  • 举报
回复
有些边际的问题想确认一下,比如8月30号,那时间范围是02-30 --- 08-30, 02-30不存在,应该怎么算,
syc958 2008-07-11
  • 打赏
  • 举报
回复
看不懂!请说的明白就!
keyake863 2008-07-11
  • 打赏
  • 举报
回复
获取当前日期。大于15天表示当前已经过了半个月,否则在前半个月中。
后面的逻辑部门你自己补齐吧。

int days = DateTime.ParseExact(date, "yyyy-M-d", null).Day;
if(days>=15)
{}
else
{}
lanxing106 2008-07-11
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 bollton 的回复:]
private string[,] SelectDate(string date)
{
string[,] timeDate = new string[12, 2];
DateTime theDate = DateTime.ParseExact(date, "yyyy-M-d", null);
int days = theDate.Day;
theDate = theDate.AddMonths(-1);

DateTime startDate, endDate;
for (int i = 0, j = 0; i < 6; i++, j++)
{

startDate = new DateTime(the…
[/Quote]
谢谢你,我刚才运行了一下
结果为:
2008-7-1 2008-7-15
2008-7-16 2008-7-22
2008-6-1 2008-6-15
2008-6-16 2008-6-30
2008-5-1 2008-5-15
2008-5-16 2008-5-31
2008-4-1 2008-4-15
2008-4-16 2008-4-30
2008-3-1 2008-3-15
2008-3-16 2008-3-31
2008-2-1 2008-2-15
2008-2-16 2008-2-29

但是还缺1月份的时间段
如果是2008-7-22 那么算6个月前的就应该是2008-1-22
少了2008-1-22 2008-1-31这个时间段
我是新手 没学编程多长时间
你可否能把这个时间段给补上呢?
bollton 2008-07-11
  • 打赏
  • 举报
回复
private string[,] SelectDate(string date)
{
string[,] timeDate = new string[12, 2];
DateTime theDate = DateTime.ParseExact(date, "yyyy-M-d", null);
int days = theDate.Day;
theDate = theDate.AddMonths(-1);

DateTime startDate, endDate;
for (int i = 0, j = 0; i < 6; i++, j++)
{

startDate = new DateTime(theDate.Year, theDate.Month, 1);
endDate = new DateTime(theDate.Year, theDate.Month, 15);
timeDate[j, 0] = startDate.ToString("yyyy-M-d");
timeDate[j, 1] = endDate.ToString("yyyy-M-d");

startDate = new DateTime(theDate.Year, theDate.Month, 16);
endDate = new DateTime(theDate.Year, theDate.Month, DateTime.DaysInMonth(theDate.Year, theDate.Month));
j = j + 1;
timeDate[j, 0] = startDate.ToString("yyyy-M-d");
timeDate[j, 1] = endDate.ToString("yyyy-M-d");

theDate = theDate.AddMonths(-1);
}
DateTime theDate2 = DateTime.ParseExact(date, "yyyy-M-d", null);
if (days >= 15)
{
for (int i = 11; i >= 2; i--)
{
timeDate[i, 0] = timeDate[i - 2, 0];
timeDate[i, 1] = timeDate[i - 2, 1];
}
startDate = new DateTime(theDate2.Year, theDate2.Month, 1);
endDate = new DateTime(theDate2.Year, theDate2.Month, 15);
timeDate[0, 0] = startDate.ToString("yyyy-M-d");
timeDate[0, 1] = endDate.ToString("yyyy-M-d");
startDate = new DateTime(theDate2.Year, theDate2.Month, 16);
endDate = new DateTime(theDate2.Year, theDate2.Month, days);
timeDate[1, 0] = startDate.ToString("yyyy-M-d");
timeDate[1, 1] = endDate.ToString("yyyy-M-d");

}
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 12; i++)
{
sb.AppendFormat(timeDate[i, 0] + "{0}", "    ");
sb.AppendFormat(timeDate[i, 1]+ "{0}", "<br>");
}
Response.Write(sb.ToString());
return timeDate;
}
wackyboy 2008-07-11
  • 打赏
  • 举报
回复
你自己压根就没有判断,直接从上个月开始取值,你在 “theDate = theDate.AddMonths(-1);”之前进行判断就可以了,或者在for循环中进行判断。
其实也没有必要非得28天就前后14天,31天就前后15.5天,就按常理,前半个月十五天,后半个月是剩下的天数就可以了。
加载更多回复(1)
内容概要:本文系统研究了开关频率大于谐振频率(fs>fr)工况下,移相混合控制LLC谐振变换器在低压增益区域的工作特性,深入分析其在变频与移相结合控制模式下的调制机理、工作模态划分及损耗分布规律。通过Simulink平台构建高保真仿真模型,对变换器在不同负载和输入条件下的电压增益、转换效率、关键器件电压电流应力等性能指标进行了全面仿真验证,重点探讨了其在低增益区间的软开关实现能力与效率优化潜力,旨在提升LLC变换器在宽范围输入输出应用中的动态响应与能源转换效率。; 适合人群:从事电力电子变换器设计、高频电源开发及相关领域的高校研究生、科研院所研究人员及企业研发工程师,要求具备扎实的电路理论基础、电力电子技术知识以及一定的Simulink仿真能力。; 使用场景及目标:①深入理解LLC谐振变换器在fs>fr条件下采用移相混合控制的内在工作机理与模态转换过程;②掌握利用Simulink搭建复杂谐振变换器精确仿真模型的方法与技巧;③分析并优化低压增益区的增益特性与损耗构成,为设计高效率、高功率密度的软开关电源提供理论依据和数据支持; 阅读建议:建议读者结合文中所述仿真模型,亲自复现仿真过程,重点观察不同控制参数(如移相比、开关频率)对电压增益曲线和关键波形的影响,并对比传统变频控制策略,深入探究混合控制在拓宽调压范围、提升轻载效率方面的优势,从而深化对现代高效谐振电源设计的理解。
内容概要:本文提出了一种基于粒子群优化算法(PSO)的配电网光伏储能双层优化配置模型,以IEEE33节点系统为标准算例,实现光伏发电单元与储能系统的协同选址与定容优化。该模型采用双层架构设计,上层以投资成本、运行经济性及网络损耗最小为目标优化设备配置方案,下层通过潮流计算评估系统在不同负荷场景下的运行性能,综合考虑电压稳定性、供电可靠性及可再生能源消纳能力,最终通过Matlab编程实现完整求解流程,为高渗透率分布式电源接入背景下的配电网规划提供了有效的技术支撑。; 适合人群:具备电力系统分析基础和Matlab编程能力的研究生、高校科研人员及从事新能源并网、智能配电网规划与优化的工程技术人员。; 使用场景及目标:①研究含高比例光伏接入的配电网规划与运行协同优化问题;②掌握双层优化建模方法与粒子群算法在复杂电力系统问题中的应用技巧;③为实际工程中分布式光伏与储能系统的科学选址与容量配置提供理论依据与仿真验证平台。; 阅读建议:建议读者结合Matlab代码深入理解双层迭代求解机制,重点关注算法收敛性分析、参数敏感性测试,并可通过更换初始种群、调整权重因子或引入其他标准测试系统(如IEEE69节点)进行对比实验,进一步验证所提模型的普适性与鲁棒性。
内容概要:本文围绕双机并联虚拟同步发电机(VSG)在微电网中的功率分配、黑启动、虚拟阻抗与预同步控制展开,基于Simulink平台构建了完整的微电网系统仿真模型。重点研究了VSG在双机并联运行下的有功/无功功率均分控制策略,通过引入虚拟阻抗技术有效解决了因线路阻感比差异导致的功率分配不均问题。同时,设计了微电网黑启动流程与并网预同步控制模块,实现了待并网系统与主网在电压幅值、频率和相位上的精确同步,显著降低了并网冲击电流。系统整合了VSG控制、下垂控制、虚拟阻抗、锁相环(PLL)及预同步逻辑等关键环节,全面验证了多VSG协同运行的稳定性、自主恢复能力与并网可靠性。; 适合人群:具备电力系统、电力电子及自动控制等相关专业知识,从事微电网、分布式发电、VSG控制与并网技术研究的研究生、科研人员及工程技术人员。; 使用场景及目标:①深入理解双机并联VSG系统中功率分配不均的机理及虚拟阻抗的补偿作用;②掌握微电网黑启动全过程及预同步控制的关键技术要点;③学习并实践基于Simulink的微电网多层次、多目标控制策略的建模与仿真方法;④为相关科研课题、毕业设计或实际工程项目提供可复现、可拓展的技术方案与仿真参考。; 阅读建议:建议结合提供的Simulink模型文件进行同步学习,重点关注VSG控制参数整定、虚拟阻抗设计原则、预同步切换逻辑等核心模块的实现细节,并可通过改变负载投切、线路参数或初始频率偏差等条件进行多工况仿真测试,以深入探究系统的动态响应特性与鲁棒性。

62,271

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

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