Djama
Djama

Reputation: 791

Return Months & days from TimeSpan asp.net

I want to return in Seconds, Minuites, Hours, Days, Months et Years from Datetime created.

I wrote this snippet of code

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

I tested the code with the following values

Edited

For a given datetime

if the number of second is less than 60 then it should return the value in second. if the number of second is less greater 60 and less than (60 * 60) secs then it should return the value in mins , the same apply for hours, days months and years

Now i have this date "createdOn": "2017-10-16T14:41:16.557" and return 41 days instead of 1 month expected.

how can i fix it

Upvotes: 1

Views: 2786

Answers (2)

Bloggrammer
Bloggrammer

Reputation: 1111

@Tarik solution for months will get you the total number of months between the startDate and endDate.

To get the last remaining months, include this while loop statement.

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

Without the while loop, it prints: 4 year(s) and 59 month(s) With the while loop, it prints: 4 year(s) and 11 month(s)

Upvotes: 3

Tarik Attar
Tarik Attar

Reputation: 36

It might be easier to use embedded methods :

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

Upvotes: 2

Related Questions