Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Convert unix timestamp to date/time in a specified format

516 views
Skip to first unread message

Andyza

unread,
Mar 4, 2009, 9:08:57 AM3/4/09
to
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.

Evertjan.

unread,
Mar 4, 2009, 10:30:20 AM3/4/09
to
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)

Tim Slattery

unread,
Mar 4, 2009, 12:22:57 PM3/4/09
to
"Evertjan." <exjxw.ha...@interxnl.net> 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)
Slatt...@bls.gov
http://members.cox.net/slatteryt

DiamondEagle

unread,
Mar 4, 2009, 2:16:41 PM3/4/09
to
On Mar 4, 5:30 pm, "Evertjan." <exjxw.hannivo...@interxnl.net> 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?

Bob Barrows

unread,
Mar 4, 2009, 2:38:43 PM3/4/09
to

DiamondEagle

unread,
Mar 4, 2009, 4:28:57 PM3/4/09
to
On Mar 4, 9:38 pm, "Bob Barrows" <reb01...@NOyahoo.SPAMcom> 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

Evertjan.

unread,
Mar 4, 2009, 7:24:44 PM3/4/09
to
Tim Slattery wrote on 04 mrt 2009 in
microsoft.public.inetserver.asp.general:

> "Evertjan." <exjxw.ha...@interxnl.net> 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)

0 new messages