Solved

Showing time left

Posted on 2011-03-24
5
750 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
Comment Utility
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
Comment Utility
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
Comment Utility
Can you please extend the example to show what you mean? Thanks.
0
 
LVL 9

Author Comment

by:RobertNZana
Comment Utility
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
Comment Utility
Close but not really.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

743 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

10 Experts available now in Live!

Get 1:1 Help Now