How to format a date

F

Fatbob

Hi everyone,

In ASP, how can I format a date that's in the form of dd/mm/yyyy to
yyyy-mm-dd?

I know about the FormatDateTime function, but as far as I can tell, this
function won't allow me to customize exactly how I want the date formatted.

Thanks!
 
H

Himselff

Hi,

what about the "theDate = format(date,yyyy-mm-dd)" ?

using format and date function to get actual date and format it !

Good luck !

Fred
 
B

Bob Barrows [MVP]

Himselff said:
Hi,

what about the "theDate = format(date,yyyy-mm-dd)" ?

using format and date function to get actual date and format it !
Great answer if the language was vb/vba - not so good with vbscript ...

Bob Barrows
 
F

Fatbob

Works like a charm.

One little bug, your FormatDate function is not returning anything. I
changed it to FormatDate = strFormat instead of ISODate = strFormat.

Thanks!
 
B

Binoy

Hi!

You can alternatively try this.

<%
dt=date
yy = Year(dt)
mm = Month(dt)
dd = Day(dt)
v_date = yy & "-" & mm & "-" & dd
Response.Write v_date
%>

Binoy
 
B

Binoy

Hi!

Try this and let me know if this helps you

<%
dt=date
yy = Year(dt)
mm = Month(dt)
dd = Day(dt)
v_date = yy & "-" & mm & "-" & dd
Response.Write v_date
%>

Binoy
 
C

Chris Hohmann

Binoy said:
Hi!

You can alternatively try this.

<%
dt=date
yy = Year(dt)
mm = Month(dt)
dd = Day(dt)
v_date = yy & "-" & mm & "-" & dd
Response.Write v_date
%>

Binoy

ISO dates (yyyy-mm-dd) require that single digit months and days be
padded with zero.
 
R

Roland Hall

in message : In ASP, how can I format a date that's in the form of dd/mm/yyyy to
: yyyy-mm-dd?
:
: I know about the FormatDateTime function, but as far as I can tell, this
: function won't allow me to customize exactly how I want the date
formatted.

http://kiddanger.com/lab/dateformat.asp

<%@ Language=VBScript %>
<%
Option Explicit
Response.Buffer = True

sub prt(str)
Response.Write(str & vbCrLf)
end sub

function formatDate(str)
'yyyy-mm-dd
dim i, arr ' m/d/yyyy
arr = split(str,"/")
for i = 0 to 1
if len(arr(i)) = 1 then
arr(i) = "0" & arr(i)
end if
next
formatDate = arr(2) & "-" & arr(0) & "-" & arr(1)
end function

dim d
d = DateSerial(year(now()),month(now()),day(now()))
prt(d & " has been formatted as " & formatDate(d))
%>

HTH...

--
Roland Hall
/* This information is distributed in the hope that it will be useful, but
without any warranty; without even the implied warranty of merchantability
or fitness for a particular purpose. */
Technet Script Center - http://www.microsoft.com/technet/scriptcenter/
WSH 5.6 Documentation - http://msdn.microsoft.com/downloads/list/webdev.asp
MSDN Library - http://msdn.microsoft.com/library/default.asp
 
D

Dave Anderson

Fatbob said:
In ASP, how can I format a date that's in the form of dd/mm/yyyy to
yyyy-mm-dd?

Here's one example:

var myDate = "15/06/2003"
myNewDate = myDate.split("/").reverse().join("-")

http://msdn.microsoft.com/library/en-us/script56/html/js56jsmthsplit.asp
http://msdn.microsoft.com/library/en-us/script56/html/js56jsmthreverse.asp
http://msdn.microsoft.com/library/en-us/script56/html/js56jsmthjoin.asp

VBScript left as an exercise.


--
Dave Anderson

Unsolicited commercial email will be read at a cost of $500 per message. Use
of this email address implies consent to these terms. Please do not contact
me directly or ask me to contact you directly for assistance. If your
question is worth asking, it's worth posting.
 
C

CJM

Yeah, sorry... a bit clumsy with my cut and paste!

I don't tend to use the FormatDate function much - mostly I'm using ISODate
because I'm prepping an ASP date for insertion into SQL Server.

CJM
 
R

Roland Hall

in message
: Fatbob wrote:
: >
: > In ASP, how can I format a date that's in the form of dd/mm/yyyy to
: > yyyy-mm-dd?
:
: Here's one example:
:
: var myDate = "15/06/2003"
: myNewDate = myDate.split("/").reverse().join("-")
:
: http://msdn.microsoft.com/library/en-us/script56/html/js56jsmthsplit.asp
: http://msdn.microsoft.com/library/en-us/script56/html/js56jsmthreverse.asp
: http://msdn.microsoft.com/library/en-us/script56/html/js56jsmthjoin.asp
:
: VBScript left as an exercise.

dim d


d = "15/06/2003" ' set date
d = join(Reverse(split("15/06/2003","/")),"-")

' or current date
d = join(Reverse(split(right("0" & day(now()),2)& "/" & right("0" &
month(now()),2) & "/" & year(now()),"/")),"-")

function Reverse(arrRev)
dim i, length
dim arr2()
length = ubound(arrRev)
redim arr2(length)
for i = 0 to length
arr2(length - i) = arrRev(i)
next
erase arrRev
Reverse = arr2
end function

--
Roland Hall
/* This information is distributed in the hope that it will be useful, but
without any warranty; without even the implied warranty of merchantability
or fitness for a particular purpose. */
Technet Script Center - http://www.microsoft.com/technet/scriptcenter/
WSH 5.6 Documentation - http://msdn.microsoft.com/downloads/list/webdev.asp
MSDN Library - http://msdn.microsoft.com/library/default.asp
 

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
474,141
Messages
2,570,817
Members
47,364
Latest member
Stevanida

Latest Threads

Top