C # calcular el valor de tiempo

Asp.net (C #) resta de tiempo para obtener el número de días, horas, minutos y segundos
Método 1
DateTime dtone = Convert.ToDateTime ("2007-1-1 05:00:00");
DateTime dtwo = Convert. ToDateTime ("2007 -1-5 08:00:00");
TimeSpan span = dtone.Subtract (dtwo); // El algoritmo es dtone menos dtwo
tss.Text = span.Days + "天"

  • span.Hours + "horas"
  • span.Minutes + "minutes"
  • span.Segundos + "segundos"
  • span.TotalDays;
    Método 2
    SpendHours = DiffMinutes(x.ENDTIME, x.REPAIRLOG.Where(y => y.STATUSNOW == 2).OrderByDescending(y => y.CREATETIME).Select(y => y.CREATETIME.Value).FirstOrDefault()) > 0 ? DiffMinutes(x.ENDTIME, x.REPAIRLOG.Where(y => y.STATUSNOW == 2).OrderByDescending(y => y.CREATETIME).Select(y => y.CREATETIME.Value).FirstOrDefault()) : 0,

Método 3

 SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    
        Date d1 = null;
        Date d2 = null;
    
        try {
            d1 = format.parse(dateStart);
            d2 = format.parse(dateStop);
    
            //毫秒ms
            long diff = d2.getTime() - d1.getTime();
    
            long diffSeconds = diff / 1000 % 60;
            long diffMinutes = diff / (60 * 1000) % 60;
            long diffHours = diff / (60 * 60 * 1000) % 24;
            long diffDays = diff / (24 * 60 * 60 * 1000);
    
            System.out.print("两个时间相差:");
            System.out.print(diffDays + " 天, ");
            System.out.print(diffHours + " 小时, ");
            System.out.print(diffMinutes + " 分钟, ");
            System.out.print(diffSeconds + " 秒.");
    
        } catch (Exception e) {
            e.printStackTrace();
        }

Prueba y uso exitosos del método 4
Inserte la descripción de la imagen aquí

    TimeSpan span = DateTime.Now - Convert.ToDateTime(sCallTime);
                var ercData = getEmsRCallCount(sCallRepairNo).Where(x => x.CALL_REPAIR_NO == sCallRepairNo).SingleOrDefault();
                {
    
    
                    ercData.CLOSE_TIME = now;
                    ercData.UPDATE_BY = UserSession.Account.Id;
                    //ercData.SPEND_TIMES = (Convert.ToDecimal(now) - Convert.ToDecimal(sCallTime)) / (60 * 1000) % 60;
                    ercData.SPEND_TIMES = Convert.ToDecimal(span.TotalMinutes);
                    ercData.STATUS = 0;
                };

Después de tomar el valor, cambie su valor a un número entero
ercData.SPEND_TIMES = Convert.ToInt32 (span.TotalMinutes);

Supongo que te gusta

Origin blog.csdn.net/caoguanghui0804/article/details/110691909
Recomendado
Clasificación