C# 获得两日期之间所有月份(包括跨年)
前台:
<!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# 获得两日期之间所有月份(包括跨年)的更多相关文章
- java:通过Calendar类正确计算两日期之间的间隔
在开发Android应用时偶然需要用到一个提示用户已用天数的功能,从实现上来看无非就是持久化存入用户第一次使用应用的时间firstTime(通过SharedPreferences .xml.sqlit ...
- js计算日期之间的月份差
<script type="text/javascript"> getMonthBetween("2015-05-01","2016-05 ...
- C++练习 | 计算两日期之间天数差
#include<iostream> #include<string> #include<cstring> using namespace std; class D ...
- Python2 获取两日期之间的每一天
import datetime def getEveryDay(begin_date,end_date): date_list = [] begin_date = datetime.datetime. ...
- MYSQL 两日期之间的工作日(除去周六日,不考虑节假日)
select (floor(days/7)*5+days%7 -case when 6 between wd and wd+days%7-1 then 1 else 0 end - ...
- php 获取开始日期与结束日期之间所有月份
function showMonthRange($start, $end) { $end = date('Ym', strtotime($end)); // 转换为月 $range = []; $i ...
- JS 格式化时间(获取两个日期之间的每一天、每一月、每半小时、每一秒)
时间戳转换为时间 // 时间戳转换为时间 function timestampToTime(timestamp, isMs = true) { const date = new Date(timest ...
- mysql计算两个日期之间的天数
MYSQL自带函数计算给定的两个日期的间隔天数 有两个途径可获得 1.利用TO_DAYS函数 select to_days(now()) - to_days('20120512') 2 ...
- MySql计算两日期时间之间相差的天数,秒数,分钟数,周数,小时数
MySql计算两日期时间之间相差的天数,秒数,分钟数,周数,小时数 计算两日期时间之间相差的天数,秒数,分钟数,周数,小时数,这里主要分享的是通过MySql内置的函数 TimeStampDiff() ...
随机推荐
- CSS彻底研究(1)
Github pages 博文 基本选择器 标记选择器h1 {...} 类别.class_name{...},两个class同时作用,如class = 'one two',冲突取前者 ID选择器 #i ...
- hbase学习笔记-----REST客户端
1. 启动REST服务 a.启动一个非守护进程模式的REST服务器(ctrl+c 终止) bin/hbase rest start b.启动守护进程模式的REST服务器 bin/hbase-daemo ...
- JS实现下拉框选中不同的项,对应显示不同的信息
实现的效果如下图: 页面代码 下拉框: <select id="select3" name="select3" onchange="showli ...
- Android-----输入法的显示和隐藏
/** * 控制手机虚拟键盘的显示和隐藏 */public class InputMethodUtil { /** * 隐藏虚拟键盘 * @param v 参数v为获取焦点对象view */ pub ...
- Android-现场保护
现场保护 当一个活动进入到了停止的状态,是有可能被系统回收的,我们都学过Activity的生命周期 当活动处于onPause() ,onStop() ,onDestroy() 三种状态时程序可能会被A ...
- 几句话弄清楚Java参数传值还是传引用
最近刷题做了一些算法题,对于在递归函数调用的时候什么时候传入值,什么时候传入引用有疑问,在网上搜索了一下,得出了一下三条总结: 1.对象就是传引用 2.原始类型就是传值 3.String,Intege ...
- C#.NET 各种连接字符串
C#.NET 各种连接字符串 近期连接数据库时,经常忘记连接字符串是如何的格式,现在此备注 此文章引用http://www.cnblogs.com/zhiqiang-imagine/archive/2 ...
- IOS 中关于自定义Cell 上的按钮 开关等点击事件的实现方法(代理)
1.在自定义的Cell .h文件中写出代理,写出代理方法. @protocol selectButtonDelegate <NSObject> -(void)selectModelID:( ...
- JS的全局变量&局部变量
<script> var i=10; //全局变量 j = 20; //全局变量 function(){ var i=30; //局部变量 h = 40; //全局变量 } </sc ...
- C语言做一个通讯录程序(在console里面运行)
最近复习C语言的时候看到网上有个C语言通讯录的小项目,于是看了下那个程序实现的大概的功能,然后自己也跟着做了个.代码还算简洁,贴上来给有需要的人. // // main.m // AdressBook ...