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. Advertising

  2. Andyza

    Evertjan. Guest

    Andyza wrote on 04 mrt 2009 in microsoft.public.inetserver.asp.general:

    > 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'


    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.
    The Netherlands.
    (Please change the x'es to dots in my emailaddress)
     
    Evertjan., Mar 4, 2009
    #2
    1. Advertising

  3. Andyza

    Tim Slattery Guest

    "Evertjan." <> wrote:

    >Andyza wrote on 04 mrt 2009 in microsoft.public.inetserver.asp.general:
    >
    >> 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'

    >
    >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.


    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
    MS MVP(Shell/User)

    http://members.cox.net/slatteryt
     
    Tim Slattery, Mar 4, 2009
    #3
  4. Andyza

    DiamondEagle Guest

    On Mar 4, 5:30 pm, "Evertjan." <> wrote:
    >
    > 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.


    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

    DiamondEagle wrote:
    > On Mar 4, 5:30 pm, "Evertjan." <> wrote:
    >>
    >> 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.

    >
    > Thanks.
    > Is there a way to display the date/time in the yyyy/mm/dd
    > hh:mm:ss format?


    http://classicasp.aspfaq.com/date-t.../can-i-make-vbscript-format-dates-for-me.html

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

    DiamondEagle Guest

    On Mar 4, 9:38 pm, "Bob Barrows" <> wrote:
    >
    > http://classicasp.aspfaq.com/date-time-routines-manipulation/can-i-ma...
    >


    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:

    > "Evertjan." <> wrote:
    >
    >>Andyza wrote on 04 mrt 2009 in microsoft.public.inetserver.asp.general:
    >>
    >>> 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'

    >>
    >>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.

    >
    > True, but it doesn't answer the question.


    At least it answers a conceptual mistake in the OP's thought.

    >>> Is there a way to specify a format for the date that
    >>> is independent of the Regional Settings on the computer?



    > 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.


    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.
    The Netherlands.
    (Please change the x'es to dots in my emailaddress)
     
    Evertjan., Mar 5, 2009
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. islamelnaggar
    Replies:
    4
    Views:
    3,073
    islamelnaggar
    Feb 24, 2008
  2. islamelnaggar
    Replies:
    0
    Views:
    511
    islamelnaggar
    Feb 24, 2008
  3. Phillip B Oldham

    Convert date/time to unix timestamp?

    Phillip B Oldham, Feb 10, 2009, in forum: Python
    Replies:
    2
    Views:
    4,500
    M.-A. Lemburg
    Feb 10, 2009
  4. Replies:
    1
    Views:
    992
    Chris Rebert
    Jul 22, 2010
  5. Sadia Bashir
    Replies:
    0
    Views:
    147
    Sadia Bashir
    Jan 30, 2014
Loading...

Share This Page