C#计算输入的时间距离今天的时间差

/// <summary>
/// 计算传入的时间距离今天的时间差
/// </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;
    }
  }

  

转载于:https://www.cnblogs.com/Jian-Zhang/p/7364651.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值