Convert unix timestamp to date/time in a specified format

A

Andyza

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

Evertjan.

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

Tim Slattery

Evertjan. said:
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.

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

DiamondEagle

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?
 
D

DiamondEagle


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
 
E

Evertjan.

Tim Slattery wrote on 04 mrt 2009 in
microsoft.public.inetserver.asp.general:
True, but it doesn't answer the question.

At least it answers a conceptual mistake in the OP's thought.
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)
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top