DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 9 of 9
  1. #1
    convert Guest

    How to convert seconds to hh:mm:ss format?


    Hi guys,

    I'v got this problem: How to convert seconds into hh:mm:ss format?

    for example: I have 3601 seconds in integer, I want to convert it into 01:00:01
    string format.

    can anyone show me a function to get this done?

    thanks.


  2. #2
    GunPaq Guest

    Re: How to convert seconds to hh:mm:ss format?

    Try:

    DateAdd("s", 3601, #12:00am#)

    Steve.

    "convert" <zhang5gor@yahoo.com> wrote in message
    news:3d00bf0a$1@10.1.10.29...
    >
    > Hi guys,
    >
    > I'v got this problem: How to convert seconds into hh:mm:ss format?
    >
    > for example: I have 3601 seconds in integer, I want to convert it into

    01:00:01
    > string format.
    >
    > can anyone show me a function to get this done?
    >
    > thanks.
    >




  3. #3
    GunPaq Guest

    Re: How to convert seconds to hh:mm:ss format?

    If you actually want a string then use the Format function on the result.

    Format(DateAdd("s", 3601, #12:00am#), "hh:mm:ss am/pm")

    for example.

    Steve.

    "GunPaq" <steve@kcfsi.com> wrote in message news:3d00d258@10.1.10.29...
    > Try:
    >
    > DateAdd("s", 3601, #12:00am#)
    >
    > Steve.
    >
    > "convert" <zhang5gor@yahoo.com> wrote in message
    > news:3d00bf0a$1@10.1.10.29...
    > >
    > > Hi guys,
    > >
    > > I'v got this problem: How to convert seconds into hh:mm:ss format?
    > >
    > > for example: I have 3601 seconds in integer, I want to convert it into

    > 01:00:01
    > > string format.
    > >
    > > can anyone show me a function to get this done?
    > >
    > > thanks.
    > >

    >
    >




  4. #4
    GunPaq Guest

    Re: How to convert seconds to hh:mm:ss format?

    Whoops, forgot I was in asp. Try FormatDateTime instead.

    "GunPaq" <steve@kcfsi.com> wrote in message news:3d00d331$1@10.1.10.29...
    > If you actually want a string then use the Format function on the result.
    >
    > Format(DateAdd("s", 3601, #12:00am#), "hh:mm:ss am/pm")
    >
    > for example.
    >
    > Steve.
    >
    > "GunPaq" <steve@kcfsi.com> wrote in message news:3d00d258@10.1.10.29...
    > > Try:
    > >
    > > DateAdd("s", 3601, #12:00am#)
    > >
    > > Steve.
    > >
    > > "convert" <zhang5gor@yahoo.com> wrote in message
    > > news:3d00bf0a$1@10.1.10.29...
    > > >
    > > > Hi guys,
    > > >
    > > > I'v got this problem: How to convert seconds into hh:mm:ss format?
    > > >
    > > > for example: I have 3601 seconds in integer, I want to convert it into

    > > 01:00:01
    > > > string format.
    > > >
    > > > can anyone show me a function to get this done?
    > > >
    > > > thanks.
    > > >

    > >
    > >

    >
    >




  5. #5
    Kris Eiben Guest

    Re: How to convert seconds to hh:mm:ss format?

    Try this:

    'get raw values
    nSec = 3601
    nMin = cint(nSec/60)
    nHr = cint(nMin/60)

    'take just the remainder to eliminate the part handled by larger
    increments
    nSec = nSec mod 60
    nMin = nMin mod 60

    'format for display with leading zeroes
    if len(nSec) = 0 then nSec = "00" elseif len(nSec) = 1 then nSec = "0" &
    nSec
    ' repeat this line for nMin and nHr

    sTime = nDay & ":" & nHr & ":" & nMin & ":" & nSec

    > >> "convert" <zhang5gor@yahoo.com> wrote in message
    > >> news:3d00bf0a$1@10.1.10.29...
    > >> > I've got this problem: How to convert seconds into hh:mm:ss

    format?
    > >> > for example: I have 3601 seconds in integer, I want to convert it

    into
    > >> 01:00:01
    > >> > string format.





  6. #6
    convert Guest

    Re: How to convert seconds to hh:mm:ss format?


    Hello Steve,

    Thanks for your response.

    I think I did not make the question clear.

    1. I do not need am/pm;
    2. 3601 is the result of rs("TotalSeconds");
    3. The result 01:00:01 is string for hh:mm:ss; and,
    4. I use VBScript on my asp.

    Any new thoughts?

    Thanks.


    "GunPaq" <steve@kcfsi.com> wrote:
    >If you actually want a string then use the Format function on the result.
    >
    >Format(DateAdd("s", 3601, #12:00am#), "hh:mm:ss am/pm")
    >
    >for example.
    >
    >Steve.
    >
    >"GunPaq" <steve@kcfsi.com> wrote in message news:3d00d258@10.1.10.29...
    >> Try:
    >>
    >> DateAdd("s", 3601, #12:00am#)
    >>
    >> Steve.
    >>
    >> "convert" <zhang5gor@yahoo.com> wrote in message
    >> news:3d00bf0a$1@10.1.10.29...
    >> >
    >> > Hi guys,
    >> >
    >> > I'v got this problem: How to convert seconds into hh:mm:ss format?
    >> >
    >> > for example: I have 3601 seconds in integer, I want to convert it into

    >> 01:00:01
    >> > string format.
    >> >
    >> > can anyone show me a function to get this done?
    >> >
    >> > thanks.
    >> >

    >>
    >>

    >
    >



  7. #7
    GunPaq Guest

    Re: How to convert seconds to hh:mm:ss format?

    What Kris said on the calculation.

    An alternate method to pad the leading zeros would be to use the String
    function or a do-while loop:

    nSec = String(2 - Len(nSec), "0") & nSec

    or

    do while Len(nSec) < 2
    nSec = "0" & nSec
    loop

    This may not be such a big deal for two digits, but would be for more. For
    example if you wanted to display 27 as 000027 then:

    MyVal = 27
    MyStr = String(6 - Len(MyVal), "0") & MyVal

    instead of:

    if ... elseif ... elseif ... elseif ......

    You could then create your own general function - something like:

    Function PadLeadingZeros(ValueToPad, TotalLength)
    ValueToPad = Trim(ValueToPad)

    PadLeadingZeros = ValueToPad

    If Len(ValueToPad) > TotalLength Then Exit Function

    PadLeadingZeros = String(TotalLength - Len(ValueToPad), "0") &
    ValueToPad
    End Function

    Steve.

    "Kris Eiben" <eibenkthisisforspammers@yahoo.com> wrote in message
    news:3d00dcf6@10.1.10.29...
    > Try this:
    >
    > 'get raw values
    > nSec = 3601
    > nMin = cint(nSec/60)
    > nHr = cint(nMin/60)
    >
    > 'take just the remainder to eliminate the part handled by larger
    > increments
    > nSec = nSec mod 60
    > nMin = nMin mod 60
    >
    > 'format for display with leading zeroes
    > if len(nSec) = 0 then nSec = "00" elseif len(nSec) = 1 then nSec = "0" &
    > nSec
    > ' repeat this line for nMin and nHr
    >
    > sTime = nDay & ":" & nHr & ":" & nMin & ":" & nSec
    >
    > > >> "convert" <zhang5gor@yahoo.com> wrote in message
    > > >> news:3d00bf0a$1@10.1.10.29...
    > > >> > I've got this problem: How to convert seconds into hh:mm:ss

    > format?
    > > >> > for example: I have 3601 seconds in integer, I want to convert it

    > into
    > > >> 01:00:01
    > > >> > string format.

    >
    >
    >




  8. #8
    convert Guest

    Re: How to convert seconds to hh:mm:ss format?


    Hi guys,

    thank you for your responses.

    However, when I tested 3690, by using Kris's suggestion, it gives me 01:02:30
    due to the rounding of 9/60=1.5 it gets 2 because the ABS(1.5), which it
    shoule turn to 1, but it gets 2.

    Any new thoughts?

    thank you!


    "GunPaq" <steve@kcfsi.com> wrote:
    >What Kris said on the calculation.
    >
    >An alternate method to pad the leading zeros would be to use the String
    >function or a do-while loop:
    >
    >nSec = String(2 - Len(nSec), "0") & nSec
    >
    >or
    >
    >do while Len(nSec) < 2
    > nSec = "0" & nSec
    >loop
    >
    >This may not be such a big deal for two digits, but would be for more.

    For
    >example if you wanted to display 27 as 000027 then:
    >
    >MyVal = 27
    >MyStr = String(6 - Len(MyVal), "0") & MyVal
    >
    >instead of:
    >
    >if ... elseif ... elseif ... elseif ......
    >
    >You could then create your own general function - something like:
    >
    >Function PadLeadingZeros(ValueToPad, TotalLength)
    > ValueToPad = Trim(ValueToPad)
    >
    > PadLeadingZeros = ValueToPad
    >
    > If Len(ValueToPad) > TotalLength Then Exit Function
    >
    > PadLeadingZeros = String(TotalLength - Len(ValueToPad), "0") &
    >ValueToPad
    >End Function
    >
    >Steve.
    >
    >"Kris Eiben" <eibenkthisisforspammers@yahoo.com> wrote in message
    >news:3d00dcf6@10.1.10.29...
    >> Try this:
    >>
    >> 'get raw values
    >> nSec = 3601
    >> nMin = cint(nSec/60)
    >> nHr = cint(nMin/60)
    >>
    >> 'take just the remainder to eliminate the part handled by larger
    >> increments
    >> nSec = nSec mod 60
    >> nMin = nMin mod 60
    >>
    >> 'format for display with leading zeroes
    >> if len(nSec) = 0 then nSec = "00" elseif len(nSec) = 1 then nSec = "0"

    &
    >> nSec
    >> ' repeat this line for nMin and nHr
    >>
    >> sTime = nDay & ":" & nHr & ":" & nMin & ":" & nSec
    >>
    >> > >> "convert" <zhang5gor@yahoo.com> wrote in message
    >> > >> news:3d00bf0a$1@10.1.10.29...
    >> > >> > I've got this problem: How to convert seconds into hh:mm:ss

    >> format?
    >> > >> > for example: I have 3601 seconds in integer, I want to convert

    it
    >> into
    >> > >> 01:00:01
    >> > >> > string format.

    >>
    >>
    >>

    >
    >



  9. #9
    Kris Eiben Guest

    Re: How to convert seconds to hh:mm:ss format?

    Try "int" instead of "cint."
    Both Int and Fix remove the fractional part of number and return the
    resulting integer value.

    "convert" <zhang5gor@yahoo.com> wrote in message
    news:3d0373b9$1@10.1.10.29...
    >
    > Hi guys,
    >
    > thank you for your responses.
    >
    > However, when I tested 3690, by using Kris's suggestion, it gives me

    01:02:30
    > due to the rounding of 9/60=1.5 it gets 2 because the ABS(1.5), which

    it
    > shoule turn to 1, but it gets 2.
    >
    > Any new thoughts?
    >
    > thank you!
    >
    >
    > "GunPaq" <steve@kcfsi.com> wrote:
    > >What Kris said on the calculation.
    > >
    > >An alternate method to pad the leading zeros would be to use the

    String
    > >function or a do-while loop:
    > >
    > >nSec = String(2 - Len(nSec), "0") & nSec
    > >
    > >or
    > >
    > >do while Len(nSec) < 2
    > > nSec = "0" & nSec
    > >loop
    > >
    > >This may not be such a big deal for two digits, but would be for

    more.
    > For
    > >example if you wanted to display 27 as 000027 then:
    > >
    > >MyVal = 27
    > >MyStr = String(6 - Len(MyVal), "0") & MyVal
    > >
    > >instead of:
    > >
    > >if ... elseif ... elseif ... elseif ......
    > >
    > >You could then create your own general function - something like:
    > >
    > >Function PadLeadingZeros(ValueToPad, TotalLength)
    > > ValueToPad = Trim(ValueToPad)
    > >
    > > PadLeadingZeros = ValueToPad
    > >
    > > If Len(ValueToPad) > TotalLength Then Exit Function
    > >
    > > PadLeadingZeros = String(TotalLength - Len(ValueToPad), "0") &
    > >ValueToPad
    > >End Function
    > >
    > >Steve.
    > >
    > >"Kris Eiben" <eibenkthisisforspammers@yahoo.com> wrote in message
    > >news:3d00dcf6@10.1.10.29...
    > >> Try this:
    > >>
    > >> 'get raw values
    > >> nSec = 3601
    > >> nMin = cint(nSec/60)
    > >> nHr = cint(nMin/60)
    > >>
    > >> 'take just the remainder to eliminate the part handled by larger
    > >> increments
    > >> nSec = nSec mod 60
    > >> nMin = nMin mod 60
    > >>
    > >> 'format for display with leading zeroes
    > >> if len(nSec) = 0 then nSec = "00" elseif len(nSec) = 1 then nSec =

    "0"
    > &
    > >> nSec
    > >> ' repeat this line for nMin and nHr
    > >>
    > >> sTime = nDay & ":" & nHr & ":" & nMin & ":" & nSec
    > >>
    > >> > >> "convert" <zhang5gor@yahoo.com> wrote in message
    > >> > >> news:3d00bf0a$1@10.1.10.29...
    > >> > >> > I've got this problem: How to convert seconds into hh:mm:ss
    > >> format?
    > >> > >> > for example: I have 3601 seconds in integer, I want to

    convert
    > it
    > >> into
    > >> > >> 01:00:01
    > >> > >> > string format.
    > >>
    > >>
    > >>

    > >
    > >

    >




Bookmarks

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


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links