Click here to Skip to main content
15,900,511 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:

I am developing a project and for that i need to calculate the 'Ageing'. The Ageing will be displayed in another page and should keep on increasing with each passing day.
The Ageing will be calculated as (The date when the form is submitted - Today's Date).And when the form is submitted the Ageing also gets saved in SQL database.
What i have done so far is on the Submit button click event i have stored the date of form submission in a variable 'Now'. And to calculate the Ageing i have subtracted Today's Date from it.
Below is my code
string Ageing = (DateTime.Today.Subtract(Convert.ToDateTime(now)).TotalDays).ToString();

But the problem is it does not increment with each day. Like if a user submits a case today i-e (5-Oct-2016) and initially the ageing should be 0, because the date form is submitted and current date is the same so after subtracting them it should be 0. But when a User comes tomorrow i-e (6-Oct-2016) and check the ageing it should be 1 and should keep on increasing with each passing day automatically.
Please help, your help would be highly appreciated.

What I have tried:

I have tried using the above mentioned code but whenever user submits the form. It stores the ageing as 0 and it never gets incremented.
I want it to getting increased by each passing day.
Like if the case was submitted on 2-Oct-2016 and today is 5-Oct-2016 so it should show Ageing as 3. (Since (5-Oct-2016) - (2-Oct-2016) = 3)
Updated 11-Oct-16 2:04am
[no name] 5-Oct-16 13:45pm    
It's really hard for me to imagine what it is that you are doing since we can't see the code but it actually sounds like on you form submission you are setting the variable "now" to the current date and then subtracting that from the current date....? And expecting something other than 0. Is that about right?
Faran Saleem 5-Oct-16 13:58pm    
No, I think i did not explain correctly.
What i am trying to do is calculate Ageing on the basis of the date when the form was submitted minus the current date.
Now lets assume the form was submitted on 2nd Oct 2016, so when the User submits the form on 2nd Oct 2016, he/she should view ageing as 0 because the form submission date and the current date is same so the result is 0.
But when the user visits the page the next day i-e on 3rd Oct 2016 and he/she sees the submitted forms then the ageing should be 1, because the form was submitted on 2nd Oct 2016 and the user is seeing the form on 3rd oct2016, so the difference is 1 day and Ageing in this case should be 1. And similarly when the user views the form on 4th Oct 2016 the Ageing should be 2, and likewise for 5th Oct 2016 the ageing should be 3, because the form was submitted on 2nd oct 2016 and current date is 5th Oct 2016 so it should calculate the difference between the two dates.
I hope i have cleared your confusion, if there is still any query please ask
[no name] 5-Oct-16 14:06pm    
Yes I get that. What you want to do and what you are doing are two different things. Where are storing this submission date so you can retrieve it at a later time to do your ageing calculation?
Faran Saleem 5-Oct-16 14:07pm    
I am storing it in SQL Database.
[no name] 5-Oct-16 14:11pm    
Well then it should be pretty easy for you to debug your code and find out where it's going wrong. We are not going to be able to tell you anything from one line of code why your code it not behaving like you expect it to.

Share this answer
Maciej Los 5-Oct-16 15:13pm    
Well, at this moment we know only that Ageing field is not getting increased, but we don't know what's wrong with your solution. And we know nothing about method used to increase that field!

So, i'd suggest to start with reading article provided in solution 1 by OriginalGriff[^].

If you would like to calculate the difference in days between two dates, you can use something like that:
DateTime startDate = new DateTime(2016,10,2);
DateTime endDate = new DateTime (2016,10,5);

var daydiff = (endDate-startDate).TotalDays;
//returns: 3

But... if you would like to calculate it on SQL Server, you have to use DateDiff function[^]:

SELECT DATEDIFF(DD, '2016-10-02', '2016-10-05') AS Ageing
--returns: 3

Good luck!
Share this answer
Faran Saleem 5-Oct-16 23:38pm    
That is what i am saying.. i am able to calculate the difference between two dates but i am not able to create the logic of how can i keep the Agein field getting increased with each passing day.
Thats where i need some help. Once the user submits the form, the agein field will be 0 for the first day but it should get increased to 1 automatically for the next day and then 2 and so on
Maciej Los 6-Oct-16 1:57am    
Wrong approach! You don't need to store "age". All you need is to save initial date, bacause you can always calculate "age" from that date to today.
Maciej Los 6-Oct-16 2:04am    
Wrong approach! You have to store only date, because you can calculate age from that date to today.
Maciej Los 6-Oct-16 2:04am    
Wrong approach! You have to store only date, because you can calculate age from that date to today.
Maciej Los 6-Oct-16 2:06am    
Wrong approach! You don't need to store "age" in your database. You have to calculate it each day.
Easy parse from string into Datetime;

DateTime TotalDays = DateTime.ParseExact(sample.text,"dd/MM/yyyy",System.Globalization.CultureInfo.InvariantCulture);
String Ageing = (DateTime.Now.Subtract((DateTime)TotalDays).Days).ToString();
Share this answer
Thank you all for your help. I really appreciate it. Thanks a ton.

I solved this through SQL Stored procedure from where i was calling my table.
I created a dateDiff function and subtracted the current date from date of submission and used Aliasing. Now it is calculating Ageing perfectly.

Thanks alot everyone again. Your help means alot.. :)
Share this answer
You can also find the age using below extension method.
Simply pass the DateOfBirth as argument.
There are many alternatives to achive the result.

public static int GetAge(DateTime birthDate)
    DateTime n = DateTime.Now; // To avoid a race condition around midnight
    int age = n.Year - birthDate.Year;

    if (n.Month < birthDate.Month 
        (n.Month == birthDate.Month && n.Day < birthDate.Day)

    return age;
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