Solved

Showing time left

Posted on 2011-03-24
5
754 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

726 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