Last post Oct 31, 2013 10:02 AM by Rion Williams
Oct 31, 2013 06:37 AM|disearches|LINK
I was wondering if anyone had a code snippet (or a resource link) to get months, or days, or hour, or minutes between now and a DATETIME value.
Oct 31, 2013 06:48 AM|itityagi|LINK
What is the language?
Oct 31, 2013 06:51 AM|Siva Krishna Macha|LINK
// Define two dates.
DateTime date1 = new DateTime(2010, 1, 1, 8, 0, 15); //You could go with DateTime.Now also
DateTime date2 = new DateTime(2010, 8, 18, 13, 30, 30);
// Calculate the interval between the two dates.
TimeSpan interval = date2 - date1;
// interval object contains all the information like, days etc..
interval (of type TimeSpan) object contains interval.Days, interval.TotalDays, interval.Hours, etc until interval.Milliseconds..
Please mark this as answer if this answers your question.
Oct 31, 2013 07:19 AM|patidar.shekhar|LINK
We can refer below code snippet for this:
DateTime Mydate = new DateTime(20110, 2, 2, 3, 0, 15);
DateTime yourdate = new DateTime(2011, 3, 12, 11, 20, 10);
TimeSpan span = Mydate - Yourdate;
// span object contains all the information like Hours,days etc.
also please refer bellow link:
Oct 31, 2013 09:25 AM|disearches|LINK
I'll check this out asap.
Oct 31, 2013 10:02 AM|Rion Williams|LINK
A TimeSpan object is likely what you need to use in this case (assuming that you are referring to .NET and not SQL). Basically, it will store all of the information between
two DateTime objects and will yield all of the relavent information that you need about the difference.
For instance, if you have an existing DateTime object and you want to get the TimeSpan between that date and now, you would use :
//Your DateTime object
DateTime yourOtherDateTime = PopulatedThroughPreviousCode();
//Get the difference (TimeSpan) between then and now
TimeSpan ts = DateTime.Now - yourOtherDateTime;
TimeSpan objects have numerous different properties that you can use to retrieve the information that you need such as Days, Hours, and Minutes as seen below :
TimeSpan ts = DateTime.Now - YourDateTime;
var daysPassed = ts.TotalDays;
var hoursPassed = ts.TotalHours;
var minutesPassed = ts.TotalMinutes;
Months isn't a property that is actively tracked through TimeSpan or DateTime objects, so you'll likely either need to estimate it (consider dividing the number of days by 30.4~) or by using
another method such as one of the ones recommended in this discussion for a more exact answer.