Solved

Showing time left

Posted on 2011-03-24
5
755 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Enroll in June's Course of the Month

June’s Course of the Month is now available! Experts Exchange’s Premium Members, Team Accounts, and Qualified Experts have access to a complimentary course each month as part of their membership—an extra way to sharpen your skills and increase training.

Question has a verified solution.

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

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

695 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