Solved

Showing time left

Posted on 2011-03-24
5
751 Views
Last Modified: 2012-05-11
Given a future date/time variable (function input), how can I output how much time is left in this format...

"2 years, 3 months, 4 days, 11 hours, 20 minutes and 10 seconds remaining."

To go 1 step further, I would like to have several boolean input variables for the function, like...
bShowYears as boolean, bShowMonths as boolean, bShowDays as boolean

So if they turn bShowYears to false then the "months" would reflect this and the out put would be (using the same example)....

"27 months, 4 days, 11 hours, 20 minutes and 10 seconds remaining."

Please provide sample code in vb.net. Thanks
0
Comment
Question by:RobertNZana
  • 3
  • 2
5 Comments
 
LVL 4

Accepted Solution

by:
MaximumIQ earned 500 total points
ID: 35211742
I started on this but ran out of time. The function I wrote below will show you the time remaining in each format (years, months, days, hours, minutes, seconds). for example if you have a 48 hour difference, it will show 0 years, 0 months, 2 days, 48 hours instead of 2 days, 0 hours... if you get what i mean.

I know you wanted more, but I have to run. so here is the concept, you can tweak it up.
 
Public Function TimeRemaining(ByVal strDate As Date, ByVal bShowYears As Boolean, ByVal bShowMonths As Boolean, ByVal bShowDays As Boolean) As String
        Dim strResult, strYearDiff, strMonthDiff, strDayDiff, strHourDiff, strMinuteDiff, strSecondDiff As String
        strResult = ""

        strYearDiff = DateDiff(DateInterval.Year, DateTime.Parse(strDate), DateTime.Now)
        strMonthDiff = DateDiff(DateInterval.Month, DateTime.Parse(strDate), DateTime.Now)
        strDayDiff = DateDiff(DateInterval.Day, DateTime.Parse(strDate), DateTime.Now)
        strHourDiff = DateDiff(DateInterval.Hour, DateTime.Parse(strDate), DateTime.Now)
        strMinuteDiff = DateDiff(DateInterval.Minute, DateTime.Parse(strDate), DateTime.Now)
        strSecondDiff = DateDiff(DateInterval.Second, DateTime.Parse(strDate), DateTime.Now)

        If bShowYears = True Then strResult = strYearDiff & " Years  "
        If bShowMonths = True Then strResult = strResult & strMonthDiff & " Months  "
        If bShowDays = True Then strResult = strResult & strDayDiff & " Days  "
        strResult = strResult & strHourDiff & " Hours  " & strMinuteDiff & " Minutes  " & strSecondDiff & " Seconds"
        Return strResult
    End Function

Open in new window

0
 
LVL 4

Expert Comment

by:MaximumIQ
ID: 35211786
To make it work how you want you can make it only query the seconds difference and just use math to calculate the rest
0
 
LVL 9

Author Comment

by:RobertNZana
ID: 35218830
Can you please extend the example to show what you mean? Thanks.
0
 
LVL 9

Author Comment

by:RobertNZana
ID: 35241652
Your code doesn't do what it should. I need it to break it down further. Can anyone assist please?

To simplify, given 2 date/times, return the time left between them in this format....

x years, x months, x weeks, x days, x hours, x minutes, x seconds

Thanks
0
 
LVL 9

Author Closing Comment

by:RobertNZana
ID: 35345400
Close but not really.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now