Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
How can i do date difference like for eg 2 days ago, 1 hour ago,
Can you please guide me how to do this function?
Can you please post a code of it ?



Thanks in Advance
Posted
Updated 30-May-14 4:06am
v2

Check this

C#
DateTime[] dd = new DateTime[] { new DateTime(2014, 01, 10, 10, 15, 01),new DateTime(2014, 01, 10, 10, 10, 10) };

        int x = Convert.ToInt32((dd[0] - dd[1]).TotalMinutes);

        String unit = "days";

        if (x / 60 == 0)
        {
            unit = "minutes";
        }

        else if (x / 60 / 24 == 0)
        {
            unit = "hours";
            x = x / 60;
        }

        else
        {
            x = x / (60 * 24);
        }

        Console.WriteLine(x + " " + unit);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900