75 lines
2.6 KiB
C#
75 lines
2.6 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Web;
|
|
|
|
namespace DongYingAPI.Util
|
|
{
|
|
public class Tool
|
|
{
|
|
/// <summary>
|
|
/// 计算两个时间年份月份差
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public static int GetUsedMonth1(string type, DateTime dynamicTime, DateTime currentDate)
|
|
{
|
|
try
|
|
{
|
|
int year = currentDate.Year - dynamicTime.Year; //相差的年份
|
|
int month = (currentDate.Year - dynamicTime.Year) * 12 + (currentDate.Month - dynamicTime.Month); //相差的月份
|
|
//int month1 = currentDate.Year * 12 + currentDate.Month - dynamicTime.Year * 12 - dynamicTime.Month; //相差的月份
|
|
|
|
TimeSpan used = DateTime.Now - dynamicTime;
|
|
double totalDays = used.TotalDays; //相差总天数
|
|
if (type == "年")
|
|
{
|
|
return Convert.ToInt32(year);
|
|
}
|
|
else
|
|
{
|
|
return Convert.ToInt32(month);
|
|
}
|
|
}
|
|
catch (Exception)
|
|
{
|
|
return 0;
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// 计算两个时间的差别
|
|
/// </summary>
|
|
/// <param name="type"></param>
|
|
/// <param name="dynamicTime"></param>
|
|
/// <param name="currentDate"></param>
|
|
/// <returns></returns>
|
|
public static int GetUsedMonth2(string type, DateTime dynamicTime, DateTime currentDate)
|
|
{
|
|
try
|
|
{
|
|
int year = currentDate.Month - dynamicTime.Month; //月
|
|
int month = dynamicTime.Day-currentDate.Day; //日
|
|
int day = dynamicTime.Hour - currentDate.Hour;//小时 //int month1 = currentDate.Year * 12 + currentDate.Month - dynamicTime.Year * 12 - dynamicTime.Month; //相差的月份
|
|
if (type == "年")
|
|
{
|
|
return Convert.ToInt32(year);
|
|
}
|
|
else if(type=="月")
|
|
{
|
|
return Convert.ToInt32(month);
|
|
}
|
|
else if (type == "日")
|
|
{
|
|
return Convert.ToInt32(day);
|
|
}
|
|
else
|
|
{
|
|
return 0;
|
|
}
|
|
}
|
|
catch (Exception)
|
|
{
|
|
return 0;
|
|
}
|
|
}
|
|
}
|
|
} |