C# 計(jì)算傳入的時(shí)間距離今天的時(shí)間差
更新時(shí)間:2017年08月09日 11:45:06 作者:TKK_LCM
本文通過一段簡單的代碼給大家介紹了C# 計(jì)算傳入的時(shí)間距離今天的時(shí)間差,代碼簡單易懂,需要的朋友參考下吧
廢話不多說了,直接給大家貼代碼了,具體代碼如下所示:
/// <summary>
/// 計(jì)算傳入的時(shí)間距離今天的時(shí)間差
/// </summary>
/// <param name="dt"></param>
/// <param name="yy"></param>
/// <param name="mm"></param>
/// <param name="dd"></param>
public void GetCriminalYX(DateTime dt, out int yy, out int mm, out int dd)
{
DateTime now = DateTime.Now;
yy = mm = dd = 0;
if (dt.Year > 9000 || dt.Year == 1900)
{
return;
}
if (dt <= now)
{
return;
}
StringBuilder str = new StringBuilder();
int dt_Y = dt.Year;
int dt_M = dt.Month;
int dt_D = dt.Day;
int now_Y = DateTime.Now.Year;
int now_M = DateTime.Now.Month;
int now_D = DateTime.Now.Day;
yy = dt_Y - now_Y;
mm = dt_M - now_M;
dd = 0;
int dt_M_SY = 0;
if (dt_D < now_D)
{
mm -= 1;
dt_M_SY = dt_M - 1;
if (dt_M_SY == 0)
{
dt_M_SY = 12;
}
if (dt_M_SY == 2)
{
dt_M_SY = dt_Y % 4 == 0 ? 29 : 28;
}
else
{
dt_M_SY = dt_M_SY == 2 || dt_M_SY == 4 || dt_M_SY == 6 || dt_M_SY == 9 || dt_M_SY == 11 ? 30 : 31;
}
dt_D += dt_M_SY;
}
dd = dt_D - now_D;
if (mm < 0)
{
yy -= 1;
mm += 12;
}
}
總結(jié)
以上所述是小編給大家介紹的C# 計(jì)算傳入的時(shí)間距離今天的時(shí)間差,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
C#刪除只讀文件或文件夾(解決File.Delete無法刪除文件)
這篇文章主要介紹了C#刪除只讀文件或文件夾(解決File.Delete無法刪除文件),需要的朋友可以參考下2015-09-09
c#入門之實(shí)現(xiàn)簡易存款利息計(jì)算器示例
這篇文章主要介紹了c#入門之實(shí)現(xiàn)簡易存款利息計(jì)算器示例,需要的朋友可以參考下2014-04-04
WPF中的ListBox實(shí)現(xiàn)按塊顯示元素的方法
這篇文章主要介紹了WPF中的ListBox實(shí)現(xiàn)按塊顯示元素的方法,涉及ListBox屬性設(shè)置相關(guān)操作技巧,需要的朋友可以參考下2016-09-09
基于C#實(shí)現(xiàn)簡單離線注冊(cè)碼生成與驗(yàn)證
本文使用RSA非對(duì)稱加密和Base64簡單地實(shí)現(xiàn)離線注冊(cè)碼的生成與驗(yàn)證功能。感興趣的朋友跟著小編一起學(xué)習(xí)吧2015-09-09
DevExpress之SplashScreen用法實(shí)例
這篇文章主要介紹了DevExpress中SplashScreen的用法,對(duì)于C#初學(xué)者有很好的參考借鑒價(jià)值,需要的朋友可以參考下2014-08-08

