代码之家  ›  专栏  ›  技术社区  ›  Djama

从TimeSpan asp返回月和日。网

  •  1
  • Djama  · 技术社区  · 7 年前

    我想在几秒钟、几分钟、几小时、几天、几个月和几年后回来 Datetime 创建。

    public static string ReturnCreatedSince(DateTime createdOn)
        {
            //Get current datetime
            var today = DateTime.Now;
    
            // Get days in current month
            var daysInMonth = DateTime.DaysInMonth(today.Year, today.Month);
    
            double seconds = 60;
            double minutes = seconds * 60;
            double hours = minutes * 60;
            double days = hours * 24;
            //double weeks = days * 7;
            double months = days * daysInMonth;
            double years = months * 12;
    
            //Convert created datetime to seconds
            var datetimeInSeconds = (today - createdOn).TotalSeconds;
    
            var createdSince = string.Empty;
    
            if (datetimeInSeconds <= seconds) //seconds between 1 to 60
            {
                return TimeSpan.FromSeconds(datetimeInSeconds).Seconds.ToString() + " sec";
            }
            else if (datetimeInSeconds <= minutes)// Minuites between 1 to 60
            {
                return TimeSpan.FromSeconds(datetimeInSeconds).Minutes.ToString() + " mins";
            }
            else if (datetimeInSeconds <= hours)// Hours between 1 to 24
            {
                return TimeSpan.FromSeconds(datetimeInSeconds).Hours.ToString() + " hrs";                
            }
            else if (datetimeInSeconds <= days)// Days between 1 to 24
            {
                return TimeSpan.FromSeconds(datetimeInSeconds).Days.ToString() + " jrs";
            }           
            else if (datetimeInSeconds <= months)// Months between 1 to 24
            {
               return (datetimeInSeconds / months).ToString() + " m";
            }
            else if (datetimeInSeconds <= years)// Years between 1 to 12 
            {
                return (datetimeInSeconds / years).ToString() + " yrs";
            }
            else
            {
                return createdOn.ToShortDateString();
            }
        }
    

    我用以下值测试了代码

    已编辑

    对于给定的 日期时间

    如果秒数小于60,则应返回以秒为单位的值。 如果秒数小于大于60秒且小于(60*60)秒,则应返回以分钟为单位的值,这同样适用于小时、天、月和年

    现在我有这个约会了 "createdOn": "2017-10-16T14:41:16.557" 并返回41天,而不是预期的1个月。

    我该怎么修

    2 回复  |  直到 7 年前
        1
  •  3
  •   Bloggrammer    4 年前

    @数月的Tarik解决方案将获得startDate和endDate之间的总月数。

    要获取最后剩下的几个月,请包含此while循环语句。

    DateTime startDate = new DateTime(2003, 1, 1);
    DateTime endDate = new DateTime(2007, 12, 1);
    int nbYears = endDate.Year - startDate.Year;
    int nbMonths = ((endDate.Year - startDate.Year) * 12) + endDate.Month - startDate.Month;
    while (nbMonths > 12)
    {
        nbMonths = nbMonths % 12;
    }
    Console.WriteLine($"{nbYears} year(s) and {nbMonths} month(s)");
    

    没有while循环,它打印:4年59个月 通过while循环,它可以打印:4年11个月

        2
  •  2
  •   daniele3004    7 年前

    使用嵌入式方法可能更容易:

    DateTime startDate = new DateTime(1970, 01, 01);
    DateTime endDate = DateTime.Now.ToUniversalTime();
    TimeSpan diff = (endDate - startDate);
    
    Console.WriteLine("Number of seconds:" + diff.TotalSeconds);
    Console.WriteLine("Number of minutes:" + diff.TotalDays);
    Console.WriteLine("Number of hours:" + diff.TotalHours );
    Console.WriteLine("Number of days:" + diff.TotalDays);
    
    //months 
    int nbMonths = ((endDate.Year - startDate.Year) * 12) + endDate.Month - startDate.Month;
    Console.WriteLine("Number of months:" + nbMonths);
    //years 
    int nbYears = endDate.Year - startDate.Year;
    Console.WriteLine("Number of years:" + nbYears);
    Console.ReadKey();