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(0, 4);string year_e = End_time.Substring(0, 4);string Begain_month = Begain_time.Substring(5, 2);string End_month = End_time.Substring(5, 2);int year_count=Convert.ToInt32(year_e)-Convert.ToInt32(year_b);if (year_count==0){int count = Convert.ToInt32(End_month) - Convert.ToInt32(Begain_month);for (int i = 0; i <= count; i++){string month = (Convert.ToInt32(Begain_month) + i).ToString();if (month.Length == 1){month = "0" + month;}DateTime datetime = DateTime.Now;string[] time = datetime.ToString().Split(new char[] { '/' });string date = year_b + "-" + month + "-" + time[2];string first = "";string last = "";if (Convert.ToInt32(month) == Convert.ToInt32(Begain_month)){first = Begain_time;}else{first = Convert.ToDateTime(date).AddDays(1 - Convert.ToDateTime(date).Day).ToString();}if (Convert.ToInt32(month) == Convert.ToInt32(End_month)){last = End_time;}else{last = Convert.ToDateTime(date).AddDays(1 - Convert.ToDateTime(date).Day).AddMonths(1).AddDays(-1).ToString();}if (first.IndexOf(" ") > 0){first = first.Remove(first.IndexOf(" "));}if (last.IndexOf(" ") > 0){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)+ 12 * year_count - Convert.ToInt32(Begain_month) ;for (int i = 0; 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=0;if (month_>12){year_add = month_ / 12;}year = (Convert.ToInt32(year_b) + year_add).ToString();if (month_%12==0){month = "12";if (year_add>0){year = (Convert.ToInt32(year) - 1).ToString();}}else{month = (month_ - 12 * year_add).ToString();}if (month.Length == 1){month = "0" + month;}date = year + "-" + month + "-" + time[2];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(1 - 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(1 - Convert.ToDateTime(date).Day).AddMonths(1).AddDays(-1).ToString();}if (first.IndexOf(" ") > 0){first = first.Remove(first.IndexOf(" "));}if (last.IndexOf(" ") > 0){last = last.Remove(last.IndexOf(" "));}Response.Write(first+"-"+last+"\t");}}}
输入日期月和日必需是两位
C#中两个日期的月份差相关推荐
- java计算两个月份差_Java编程实现计算两个日期的月份差实例代码
本文实例主要实现计算两个日期的月份差,具体如下: package com.forezp.util; import org.joda.time.DateTime; import org.joda.tim ...
- 两个日期月数差值计算
1. 场景 在金融类项目中,经常会涉及到利息计算,这里讨论一下按月计息的情况下,月数如何计算 2. 需求分析 前提条件,计算日期差值的时候,留头去尾,举例01.01-01.02差值为1天 月数其实算起 ...
- 两个日期之间月份的差异
本文翻译自:Difference in months between two dates How to calculate the difference in months between two d ...
- Joda-Time中两个日期之间的天数
本文翻译自:Number of days between two dates in Joda-Time How do I find the difference in Days between two ...
- 获取Moment Js中两个日期之间的小时差异
本文翻译自:Get hours difference between two dates in Moment Js I'm able to get the difference between two ...
- 怎么计算两个日期之间的差值
写程序时会遇到比较两个日期之间的差值,差了多少天,或者其他什么,于是自己写了一个方法,供大家参考,也作为自己的备份吧. 日期之间的差值:差了多少天function DateDiff(sDate1, s ...
- java 计算两个日期相差月数_Java简单计算两个日期月数差的方法
本文实例讲述了Java简单计算两个日期月数差的方法.分享给大家供大家参考,具体如下: /** * 获取两个日期相差的月数 * @param d1 较大的日期 * @param d2 较小的日期 * @ ...
- 日期函数的计算(计算两个日期的天数差,除去工作日(工作日表自己定义))
1.节假日表 CREATE TABLE [DicDate] ( [ID] [int] IDENTITY (1, 1) NOT NULL , [DateStr] [varchar10] NULL , ...
- R语言筛选dataframe中两个日期之外的数据行,介于两个日期(包括两个日期)之间的数据行
R语言筛选dataframe中两个日期之外的数据行,介于两个日期(包括两个日期)之间的数据行 目录
最新文章
- 用原生 js jquery 实现知乎收起答案功能
- 安装wamp时出现httpd.exe无法找到组件MSVCR100.dll的解决办法
- 乡村振兴谋定齐鲁样板-农业大健康·周荣江:国情讲坛创新
- python自动操作微信_Python+Appium 自动化操作微信入门看这一篇就够了
- 【自制工具类】Java删除字符串中的元素
- 对cookie和子cookie操作的封装
- 第一行代码(第二版)全书代码下载
- 转:Page.ClientScript.RegisterStartupScript(me.GetType(),script1,scriptalert('111');/script)...
- mysql优化--避免数据类型的隐式转换
- 认知盈余时代,知乎是如何运营的
- Java基础内容整理(一)
- h5底部输入框被键盘遮挡_搜遍整个谷歌, 只有我是在认真解决安卓端hybrid app键盘遮挡输入框的问题...
- 短信的发送(SMS)的发送
- linux下编译和安装log4cxx,Linux下log4cxx的安装使用
- springboot游泳池管理系统
- 推荐大数据Spark必读书目
- 九宫八卦--易学基础
- osgEarthSamples1.0运行事例
- java陵行_唐陵行纪之端陵(2013.3.19)
- Rancher docker集群
热门文章
- SaaSpace:9种最佳免费时间管理软件
- 【Ribbon】来手写一个负载均衡
- 直播技术总结(五)如何快速起播直播流
- uni-app 头部导航条改为背景图
- 2021-11-18 adb发送广播应用收不到的问题,BroadcastQueue: Background execution not allowed: receiving Intent { act=
- oracle 10g clusterware,安装Oracle10g Clusterware遇到的一点问题
- XMLGregorianCalendar和Date之间的转换
- 博士申请简历_不要让我阅读您的简历(如何申请工作)
- 腾讯云服务器操作汇总
- 【app运营】3阶段解析O2O产品的APP该如何运营推广?