DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 7 of 7

Thread: Converting seconds to days, hours, minutes, seconds

  1. #1
    Brett Hicking Guest

    Converting seconds to days, hours, minutes, seconds


    hi all

    Is there an easy way to convert a period of time like 1000 minutes or 10000
    seconds to
    a date/time format or just a time format such as hh:mm:ss ?

    Thanks in advance



    Brett

  2. #2
    Rick Rothstein Guest

    Re: Converting seconds to days, hours, minutes, seconds

    I once posted this function for printing out the Days, Hours, Minutes and
    Seconds (which is what the function name DHMS stands for) between two given
    date values. If you change the two function parameters to this single
    parameter

    ByVal NumberOfSeconds As Long

    and remove the first comment and code line after the Dim statements, you can
    then modify the DHMS= statement to combine the NumberOfSeconds,
    NumberOfMinutes, NumberOfHours (and possibly NumberOfDays) to return string
    suitable to your needs.

    Rick


    Function DHMS(Date1 As Date, Date2 As Date) As String
    Dim NumberOfSeconds As Long
    Dim NumberOfMinutes As Long
    Dim NumberOfHours As Long
    Dim NumberOfDays As Long
    'Number of seconds between the two dates
    NumberOfSeconds = Abs(DateDiff("s", Date1, Date2))
    'Calculate Days and remaining Seconds
    NumberOfDays = NumberOfSeconds \ 86400
    NumberOfSeconds = NumberOfSeconds Mod 86400
    'Calculate Hours and remaining Seconds
    NumberOfHours = NumberOfSeconds \ 3600
    NumberOfSeconds = NumberOfSeconds Mod 3600
    'Calculate Minutes and remaining Seconds
    NumberOfMinutes = NumberOfSeconds \ 60
    NumberOfSeconds = NumberOfSeconds Mod 60
    'Return the time remaining
    DHMS = NumberOfDays & " Day" & _
    IIf(NumberOfDays = 1, " ", "s ") & _
    NumberOfHours & " Hour" & _
    IIf(NumberOfHours = 1, " ", "s ") & _
    NumberOfMinutes & " Minute" & _
    IIf(NumberOfMinutes = 1, " ", "s ") & _
    NumberOfSeconds & " Second" & _
    IIf(NumberOfsecondss = 1, "", "s")
    End Function



  3. #3
    Craig Clearman Guest

    Re: Converting seconds to days, hours, minutes, seconds

    Brett,

    >Is there an easy way to convert a period of time like 1000 minutes or 10000
    >seconds to
    >a date/time format or just a time format such as hh:mm:ss ?


    Dim nTimeInSeconds As Long
    Dim nHours As Long
    Dim nMinutes As Long
    Dim nSeconds As Long

    nTimeInSeconds = 10000

    nHours = nTimeInSeconds \ 3600
    nTimeInSeconds = nTimeInSeconds Mod 3600
    nMinutes = nTimeInSeconds \ 60
    nSeconds = nTimeInSeconds Mod 60

    Debug.Print nHours, nMinutes, nSeconds

    Ciao, Craig


  4. #4
    Karl E. Peterson Guest

    Re: Converting seconds to days, hours, minutes, seconds

    Hi Brett --

    >Is there an easy way to convert a period of time like 1000 minutes or 10000
    >seconds to a date/time format or just a time format such as hh:mm:ss?


    Simple math. Examples:

    Public Function FormatSeconds(ByVal nSeconds As Long) As String
    Const SecsInDay As Long = 86400
    FormatSeconds = Format(nSeconds / SecsInDay, "hh:mm:ss")
    End Function

    Public Function SecondsToDate(ByVal nSeconds As Long) As Double
    Const SecsInDay As Long = 86400
    SecondsToDate = nSeconds / SecsInDay
    End Function

    Later... Karl



  5. #5
    Matthew Cromer Guest

    Re: Converting seconds to days, hours, minutes, seconds

    in article 38cdf730@news.devx.com, Brett Hicking at bhicking@ralabone.co.uk
    wrote on 3/14/00 3:24 AM:

    >
    > hi all
    >
    > Is there an easy way to convert a period of time like 1000 minutes or 10000
    > seconds to
    > a date/time format or just a time format such as hh:mm:ss ?
    >
    > Thanks in advance
    >
    >
    >
    > Brett


    Yes.

    ? format(cdate(timeinseconds/(60*60*24)),"hh:mm:ss")
    ? format(cdate(timeinminutes/(60*24)),"hh:mm:ss")



    --
    Matthew Cromer
    President, SDA Consulting, Inc.
    matthew@sdaconsulting.com
    http://www.sdaconsulting.com/
    (919) 274-0074




  6. #6
    Join Date
    Sep 2009
    Posts
    1

    Smile This is easy and works

    Dim ts As TimeSpan = Now.AddSeconds("20403606.00").Subtract(Now)

  7. #7
    Join Date
    Apr 2007
    Location
    Sterling Heights, Michigan
    Posts
    8,663
    Welcome to DevX
    Quote Originally Posted by rob19991 View Post
    Dim ts As TimeSpan = Now.AddSeconds("20403606.00").Subtract(Now)
    I would ask you need to make sure your posts are not only appropriate for the question, but also appropriate for the language/forum section.

    That will work in a VB.NET program, but will not work in a Classic VB program.

    (Also, this thread is 9 years old...)
    Last edited by Hack; 09-17-2009 at 08:56 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center