Return 来自 TimeSpan 的月数和天数 asp.net

Return Months & days from TimeSpan asp.net

我想在 return 秒、分钟、小时、天、月和年中创建 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,那么它应该 return 以秒为单位的值。 如果秒数小于 60 且小于 (60 * 60) 秒,那么它应该 return 以分钟为单位的值,这同样适用于小时、天、月和年

现在我有这个日期 "createdOn": "2017-10-16T14:41:16.557" 和 return 41 天而不是预期的 1 个月。

我该如何解决

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

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();

@Tarik 几个月的解决方案将为您提供开始日期和结束日期之间的总月数。

要获取最后剩余的月份,请包含此 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 year(s) and 11 month(s)