Convert unix timestamp to date/time in a specified format

Discussion in 'ASP General' started by Andyza, Mar 4, 2009.

  1. Andyza

    Andyza Guest

    The sUnixDate variable below contains a unix timestamp. I use the
    VBScript DateAdd function to convert it to a date/time variable
    whenever I want to display the date on my .asp pages.

    sUnixDate = "1235999914"
    sNewDate = DateAdd("s",sUnixDate,#1970/1/1#)

    sNewDate now contains the date according to the regional settings on
    the computer eg:

    '3/2/2009 1:18:34'
    or
    '2009/03/02 01:18:34'

    Is there a way to specify a format for the date that is independent of
    the Regional Settings on the computer?
    I want the date to always be returned and displayed in the yyyy/mm/dd
    hh:mm:ss format.

    Thanks.
     
    Andyza, Mar 4, 2009
    #1
    1. Advertisements

  2. Andyza

    Evertjan. Guest

    Andyza wrote on 04 mrt 2009 in microsoft.public.inetserver.asp.general:
    No, it does not.

    sNewDate contains the date/time,
    independent of the regional settings.

    It does not contain the way it is transcribed into a string.
     
    Evertjan., Mar 4, 2009
    #2
    1. Advertisements

  3. Andyza

    Tim Slattery Guest

    True, but it doesn't answer the question. VBScript's FormatDate
    function can display a date in several formats, all of which refer to
    the computer's regional settings. AFAIK, there's no way to specify how
    you want the date to look independent of the regional settings.

    I suppose you could write your own function to do the necessary
    arithmetic on the date value and format it as you want.
     
    Tim Slattery, Mar 4, 2009
    #3
  4. Andyza

    DiamondEagle Guest

    Thanks.
    Is there a way to display the date/time in the yyyy/mm/dd
    hh:mm:ss format?
     
    DiamondEagle, Mar 4, 2009
    #4
  5. Andyza

    Bob Barrows Guest

    Bob Barrows, Mar 4, 2009
    #5
  6. Andyza

    DiamondEagle Guest

    Thanks Bob.

    This rought "first draft" seems to work for me:

    Function pd(n, totalDigits)
    if totalDigits > len(n) then
    pd = String(totalDigits-len(n),"0") & n
    else
    pd = n
    end if
    End Function

    sUnixDate = "1235999914"
    sNewDate = DateAdd("s",sUnixDate,#1970/1/1#)
    Response.Write("sNewDate : " & sNewDate & "<br>")
    Response.Write(Year(sNewDate) & "/" & pd(Month(sNewDate),2) & "/" & pd
    (Day(sNewDate),2))


    Gives me: 2009/03/02
     
    DiamondEagle, Mar 4, 2009
    #6
  7. Andyza

    Evertjan. Guest

    Tim Slattery wrote on 04 mrt 2009 in
    microsoft.public.inetserver.asp.general:
    At least it answers a conceptual mistake in the OP's thought.
    And that isn't a way, Tim?

    I think it is, it is the best way for my and the OP's purpose

    I never use FormatDate() but build the function myself.


    sUnixDate = "1235999914"
    sNewDate = DateAdd("s",sUnixDate,#1970/1/1#)


    timeDutchString = day(sNewDate)&"-"&month(sNewDate)&"-"&year(sNewDate)&_
    " "&minute(sNewDate)&":"&second(sNewDate)
     
    Evertjan., Mar 5, 2009
    #7
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.