Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Showing time left

Posted on 2011-03-24
5
Medium Priority
?
758 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 1000 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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
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 video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Suggested Courses

610 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