前台:

<!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. FullCalendar 的学习笔记(一)

    前一段时间,一个老项目需要新增一个小功能,日程表~ 于是网上找了下,发现FullCalendar这个控件还不错于是就拿来用了下,下面简单介绍下我的学习笔记. 首先就是了解下FullCalendar的A ...

  2. javascript创建自定义对象和prototype

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

  3. DevExpress gridcontrol添加了复选框删除选中的多行/批量删除的方法

    思路:遍历gridView1的每一行,该行中checkbox列被勾选则设置该行为选中状态,执行gridView1提供的DeleteSelectedRows方法则可 public void DoDele ...

  4. codeforces432D Prefixes and Suffixes(kmp+dp)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud D. Prefixes and Suffixes You have a strin ...

  5. [hdu5113]Black And White2014北京赛区现场赛B题(搜索加剪枝)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud Black And White Time Limit: 2000/2000 MS ...

  6. (原)vs2013静态及动态链接opencv3.0的库

    转载请注明出处: http://www.cnblogs.com/darkknightzh/p/5477551.html 静态链接步骤如下: 1. 在“通用配置”-“VC++目录”-“包含目录”中添加: ...

  7. html5增强元素--续

    progress元素使用例子 <script> ; function progress_demo() { ) { i++; updateProgress(i); setTimeout(pr ...

  8. [c language] getopt 其参数optind 及其main(int argc, char **argv) 参数解释

    getopt被用来解析命令行选项参数.#include <unistd.h> extern char *optarg; //选项的参数指针extern int optind, //下一次调 ...

  9. PHP PSR-4 Autoloader 自动加载(中文版)

    Autoloader 关键词 “必须”("MUST").“一定不可/一定不能”("MUST NOT").“需要”("REQUIRED"). ...

  10. bootstrap的datetimepicker控件只选择年月的配置

    <script src="{% static "jquery/jquery-1.11.3.min.js" %}"></script> & ...