两个日期之间计算月份和天数 c#日期计算 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 timespan 可以计算返回天数、小时数等,但月份那个怎么计算呢?还有我只想计算日期,时分秒不管。我是用dateTimePicker日期控件选择日期,timespan ts=date1-date2 计算返回的天数不准确,还有时分秒。 人家返回的天数带时分秒那是精确,到你这就变成不准确了,你自己决定超过整数天是取整数还是整数天加1不就行了么。月份,你用TotalDays来/30算也行。就像要非常不精确的月份差,那么年份差*12-月份差就是了。 不要用timespan,你这个问题根本不是要把两个时间相减1.先把字符串转datetime类型,然后获取到datetime.year和datetime.month2.然后就可以循环了 /// <summary> /// 计算时间差 /// </summary> /// <param name="t">时间1</param> /// <param name="t2">时间2</param> /// <returns>返回值:时间差(毫秒为单位)</returns> private static long TimeDiff(DateTime t, DateTime t2) { long lReturn = -1; System.TimeSpan NowValue = new TimeSpan(t.Ticks); System.TimeSpan TimeValue = new TimeSpan(t2.Ticks); System.TimeSpan DateDiff = TimeSpan.Zero; try { //计算时间差 //DateDiff = TimeValue.Subtract(NowValue).Duration(); DateDiff = TimeValue.Subtract(NowValue); int hours = DateDiff.Hours; int minutes = DateDiff.Minutes; int seconds = DateDiff.Seconds; int milliseconds = DateDiff.Milliseconds; string TimeDiff = hours.ToString() + ":" + minutes.ToString() + ":" + seconds.ToString() + "." + milliseconds.ToString(); //是否比现在的时间小 if (hours <= 0 && minutes <= 0 && seconds <= 0 && milliseconds <= 0) lReturn = 0; else lReturn = hours * 3600 * 1000 + minutes * 60 * 1000 + seconds * 1000 + milliseconds; } catch (Exception ) { return -1; } return lReturn; } List<string> GetFileNames(DateTime Date1, DateTime Date2){ List<string> FileNameList = new List<string>(); int year1, year2; int month1, month2; if (Date1 < Date2) { year1 = Date1.Year; year2 = Date2.Year; month1 = Date1.Month; month2 = Date2.Month; } else { year1 = Date2.Year; year2 = Date1.Year; month1 = Date2.Month; month2 = Date1.Month; } while (year1 != year2 && month1 != month2) { FileNameList.Add(year1.ToString() + month1.ToString("D2")); month1++; if (month1 > 12) { year1++; month1 = 1; } } return FileNameList;} DateTime startDate = DateTime.Parse("2014-1-1"); DateTime endDate = DateTime.Parse("2014-2-3"); TimeSpan ts = new TimeSpan(); int totalMonth = endDate.Year * 12 + endDate.Month - startDate.Year * 12 - startDate.Month; ts = endDate - startDate; Response.Write("相隔月份:" + totalMonth + ",相隔天数:" + ts.Days); 关于列数据转换为行数据的问题 如何设置颜色透明度 asp.netExcel导入 帮帮忙 如何设置string的第i个字符 access的表主键ID,自动编号.做测试用了数字越来越大,发布的时候清空数据库了.这么ID怎么从1开始呢 20分,求_-_-_-_-_-_-___kingnight(愚翁)___-_-_-_-_-_-_-_-大哥的QQ号码 在大家说的网站上原版COPY的FTP代码,怎么上传下载功能都不行??急!!! C#调用C的dll文件的问题 打开所在文件夹这个功能怎么实现 江湖救急!读取大于100M的txt文件出错。 DataGridView 向数据库里inset数据
月份,你用TotalDays来/30算也行。就像要非常不精确的月份差,那么年份差*12-月份差就是了。
1.先把字符串转datetime类型,然后获取到datetime.year和datetime.month
2.然后就可以循环了
/// <summary>
/// 计算时间差
/// </summary>
/// <param name="t">时间1</param>
/// <param name="t2">时间2</param>
/// <returns>返回值:时间差(毫秒为单位)</returns>
private static long TimeDiff(DateTime t, DateTime t2)
{
long lReturn = -1;
System.TimeSpan NowValue = new TimeSpan(t.Ticks);
System.TimeSpan TimeValue = new TimeSpan(t2.Ticks);
System.TimeSpan DateDiff = TimeSpan.Zero;
try
{
//计算时间差
//DateDiff = TimeValue.Subtract(NowValue).Duration();
DateDiff = TimeValue.Subtract(NowValue);
int hours = DateDiff.Hours;
int minutes = DateDiff.Minutes;
int seconds = DateDiff.Seconds;
int milliseconds = DateDiff.Milliseconds;
string TimeDiff = hours.ToString() + ":"
+ minutes.ToString() + ":"
+ seconds.ToString() + "."
+ milliseconds.ToString();
//是否比现在的时间小
if (hours <= 0 && minutes <= 0 && seconds <= 0 && milliseconds <= 0)
lReturn = 0;
else
lReturn = hours * 3600 * 1000
+ minutes * 60 * 1000
+ seconds * 1000
+ milliseconds;
}
catch (Exception )
{
return -1;
}
return lReturn;
}
List<string> GetFileNames(DateTime Date1, DateTime Date2)
{
List<string> FileNameList = new List<string>();
int year1, year2;
int month1, month2; if (Date1 < Date2)
{
year1 = Date1.Year;
year2 = Date2.Year;
month1 = Date1.Month;
month2 = Date2.Month;
}
else
{
year1 = Date2.Year;
year2 = Date1.Year;
month1 = Date2.Month;
month2 = Date1.Month;
}
while (year1 != year2 && month1 != month2)
{
FileNameList.Add(year1.ToString() + month1.ToString("D2"));
month1++;
if (month1 > 12)
{
year1++;
month1 = 1;
}
} return FileNameList;
}
DateTime startDate = DateTime.Parse("2014-1-1");
DateTime endDate = DateTime.Parse("2014-2-3");
TimeSpan ts = new TimeSpan();
int totalMonth = endDate.Year * 12 + endDate.Month - startDate.Year * 12 - startDate.Month;
ts = endDate - startDate;
Response.Write("相隔月份:" + totalMonth + ",相隔天数:" + ts.Days);