前台:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
开始<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
结束<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<br />
<br />
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
</div>
</form>
</body>
</html>

后台:

protected void Button1_Click(object sender, EventArgs e)
{
string Begain_time = TextBox1.Text;
string End_time = TextBox2.Text;
string year_b = Begain_time.Substring(, );
string year_e = End_time.Substring(, );
string Begain_month = Begain_time.Substring(, );
string End_month = End_time.Substring(, );
int year_count=Convert.ToInt32(year_e)-Convert.ToInt32(year_b); if (year_count==)
{
int count = Convert.ToInt32(End_month) - Convert.ToInt32(Begain_month);
for (int i = ; i <= count; i++)
{
string month = (Convert.ToInt32(Begain_month) + i).ToString();
if (month.Length == )
{
month = "" + month;
}
DateTime datetime = DateTime.Now;
string[] time = datetime.ToString().Split(new char[] { '/' });
string date = year_b + "-" + month + "-" + time[];
string first = "";
string last = "";
if (Convert.ToInt32(month) == Convert.ToInt32(Begain_month))
{
first = Begain_time;
}
else
{
first = Convert.ToDateTime(date).AddDays( - Convert.ToDateTime(date).Day).ToString();
}
if (Convert.ToInt32(month) == Convert.ToInt32(End_month))
{
last = End_time;
}
else
{
last = Convert.ToDateTime(date).AddDays( - Convert.ToDateTime(date).Day).AddMonths().AddDays(-).ToString();
}
if (first.IndexOf(" ") > )
{
first = first.Remove(first.IndexOf(" "));
}
if (last.IndexOf(" ") > )
{
last = last.Remove(last.IndexOf(" "));
}
Response.Write(first + "至" + last + "");
}
}
else
{
int a = Convert.ToInt32(End_month);
int b = Convert.ToInt32(Begain_month);
int count = Convert.ToInt32(End_month)+ * year_count - Convert.ToInt32(Begain_month) ;
for (int i = ; i <= count; i++)
{
DateTime datetime = DateTime.Now;
string[] time = datetime.ToString().Split(new char[] { '/' });
string month = "";
string date = "";
string year = "";
int month_ = Convert.ToInt32(Begain_month) + i;
int year_add=;
if (month_>)
{
year_add = month_ / ;
}
year = (Convert.ToInt32(year_b) + year_add).ToString();
if (month_%==)
{
month = "";
if (year_add>)
{
year = (Convert.ToInt32(year) - ).ToString();
}
}
else
{
month = (month_ - * year_add).ToString();
}
if (month.Length == )
{
month = "" + month;
}
date = year + "-" + month + "-" + time[]; string first = "";
string last = "";
if (Convert.ToInt32(month) == Convert.ToInt32(Begain_month) && Convert.ToInt32(year) == Convert.ToInt32(year_b))
{
first = Begain_time;
}
else
{
first = Convert.ToDateTime(date).AddDays( - Convert.ToDateTime(date).Day).ToString();
}
if (Convert.ToInt32(month) == Convert.ToInt32(End_month) && Convert.ToInt32(year) == Convert.ToInt32(year_e))
{
last = End_time;
}
else
{
last = Convert.ToDateTime(date).AddDays( - Convert.ToDateTime(date).Day).AddMonths().AddDays(-).ToString();
}
if (first.IndexOf(" ") > )
{
first = first.Remove(first.IndexOf(" "));
}
if (last.IndexOf(" ") > )
{
last = last.Remove(last.IndexOf(" "));
}
Response.Write(first+"-"+last+"\t");
}
}
}

注:输入日期月和日必需是两位,如2015-08-05或2015/08/05

C# 获得两日期之间所有月份(包括跨年)的更多相关文章

  1. java:通过Calendar类正确计算两日期之间的间隔

    在开发Android应用时偶然需要用到一个提示用户已用天数的功能,从实现上来看无非就是持久化存入用户第一次使用应用的时间firstTime(通过SharedPreferences .xml.sqlit ...

  2. js计算日期之间的月份差

    <script type="text/javascript"> getMonthBetween("2015-05-01","2016-05 ...

  3. C++练习 | 计算两日期之间天数差

    #include<iostream> #include<string> #include<cstring> using namespace std; class D ...

  4. Python2 获取两日期之间的每一天

    import datetime def getEveryDay(begin_date,end_date): date_list = [] begin_date = datetime.datetime. ...

  5. MYSQL 两日期之间的工作日(除去周六日,不考虑节假日)

    select (floor(days/7)*5+days%7      -case when 6 between wd and wd+days%7-1 then 1 else 0 end      - ...

  6. php 获取开始日期与结束日期之间所有月份

    function showMonthRange($start, $end) { $end = date('Ym', strtotime($end)); // 转换为月 $range = []; $i ...

  7. JS 格式化时间(获取两个日期之间的每一天、每一月、每半小时、每一秒)

    时间戳转换为时间 // 时间戳转换为时间 function timestampToTime(timestamp, isMs = true) { const date = new Date(timest ...

  8. mysql计算两个日期之间的天数

    MYSQL自带函数计算给定的两个日期的间隔天数   有两个途径可获得   1.利用TO_DAYS函数   select to_days(now()) - to_days('20120512')   2 ...

  9. MySql计算两日期时间之间相差的天数,秒数,分钟数,周数,小时数

    MySql计算两日期时间之间相差的天数,秒数,分钟数,周数,小时数 计算两日期时间之间相差的天数,秒数,分钟数,周数,小时数,这里主要分享的是通过MySql内置的函数 TimeStampDiff() ...

随机推荐

  1. Js获取Gridview中Dropdownlist选中状态

    在Gridview中加入Dropdownlist模板列,加入DropDownlist 是一种常用的操作,其中涉及到如何获取选择项和Gridview重新绑定两个要点. 如图 前台代码如下 <%@ ...

  2. HTML基础知识笔记(一)

    HTML定义 HTML指的是超文本标记语言 HTML不是编程语言,而是标记语言 标记语言是一套标记标签 HTML是用标记标签来描述网页   HTML标签1 <html></html& ...

  3. 编写简单的 NT 式驱动程序的加载与卸载工具

    写驱动的加载需要用到五个函数: OpenSCManager() CreateService() OpenService() StartService() CloseServiceHandle() 这五 ...

  4. Qt Library 链接库

    官方教程:http://wiki.qt.io/How_to_create_a_library_with_Qt_and_use_it_in_an_application http://qimo601.i ...

  5. Js之Screen对象

    Window Screen window.screen 对象在编写时可以不使用 window 这个前缀. 属性: screen.availWidth - 可用的屏幕宽度,以像素计,减去界面特性,比如窗 ...

  6. 使用URLConnection获取网页信息的基本流程

    参考自core java v2, chapter3 Networking. 注:URLConnection的子类HttpURLConnection被广泛用于Android网络客户端编程,它与apach ...

  7. zabbix之2安装编译/基本功能实现

    1.安装方式: rpm或者编译都可,rpm可以直接用yum安装. rpm安装的话,根据文件名进行选择即可. 编译的话,不同参数对应不同的组件. 编译安装zabbix:同时安装server和agent, ...

  8. 2016_ThinkinG of everyDay

    2016-2-15:新年的第一篇心情,慢慢更新. 1.以后的文章当然都会是ACM竞赛的题解,但会多写写自己的思考以及总结,因为感觉自己缺少思考这个习惯,有些东西在脑子里也只是一带而过,最后也不会停留多 ...

  9. Radar Installation(POJ 1328 区间贪心)

    Radar Installation Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 68578   Accepted: 15 ...

  10. IIS6.0架构概览(翻译)

    IIS6.0提供一个重新设计的万维网发布服务(World Wide Web Publishing Service)架构,可以帮助你为你的网站构建更好的性能.可靠.可扩展性(scalability),无 ...