110,536
社区成员
发帖
与我相关
我的任务
分享
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);
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;
}
/// <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;
}
TimeSpan