Getting Date from DateTime

S

scott

I'm trying to isolate just the m/d/yyyy part of a datetime string like
below. Can someone tell me why my string modification fails on some dates? I
thought byb basing my code on where the blank space between the date and
time would work. my code works on some dates that are single digit days and
months.


mydate = "12/30/2005 10:00 AM"

newDate = Left(mydate,len(mydate)-Instr(mydate," "))
response.write newDate
' trying the return 12/30/2005
 
C

Curt_C [MVP]

scott said:
I'm trying to isolate just the m/d/yyyy part of a datetime string like
below. Can someone tell me why my string modification fails on some dates? I
thought byb basing my code on where the blank space between the date and
time would work. my code works on some dates that are single digit days and
months.


mydate = "12/30/2005 10:00 AM"

newDate = Left(mydate,len(mydate)-Instr(mydate," "))
response.write newDate
' trying the return 12/30/2005

Split on the " "
 
R

Roland Hall

in message : I'm trying to isolate just the m/d/yyyy part of a datetime string like
: below. Can someone tell me why my string modification fails on some dates?
I
: thought byb basing my code on where the blank space between the date and
: time would work. my code works on some dates that are single digit days
and
: months.
:
:
: mydate = "12/30/2005 10:00 AM"
:
: newDate = Left(mydate,len(mydate)-Instr(mydate," "))
: response.write newDate
: ' trying the return 12/30/2005

Following Curt's suggestion:

newDate = split(mydate)(0)

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

Phill. W

scott said:
I'm trying to isolate just the m/d/yyyy part of a datetime string like
below. Can someone tell me why my string modification fails on
some dates?
mydate = "12/30/2005 10:00 AM"

newDate = Left(mydate,len(mydate)-Instr(mydate," "))

Looking at what this is doing :

newDate = Left( mydate, len( mydate ) - Instr( mydate," " ) )
newDate = Left( mydate, 19 - 11 )
newDate = Left( mydate, 8 )
newData = "12/30/20"

Might be able to get a date from that but, I guess not ...

I think this might work a little better :

newDate = Left( mydate, Instr( mydate & " "," " ) - 1 )

(adding a trailing space, just in case the date doesn't have one of its
own).

HTH,
Phill W.
 
B

Bob Barrows [MVP]

scott said:
I'm trying to isolate just the m/d/yyyy part of a datetime string like
below. Can someone tell me why my string modification fails on some
dates? I thought byb basing my code on where the blank space between
the date and time would work. my code works on some dates that are
single digit days and months.


mydate = "12/30/2005 10:00 AM"

newDate = Left(mydate,len(mydate)-Instr(mydate," "))
response.write newDate
' trying the return 12/30/2005

Just to add a new wrinkle, and perhaps to make it a little more robust:

mydatestring = "12/30/2005 10:00 AM"
if isdate(mydatestring) then
mydate=cdate(mydatestring)
mydate=dateserial(year(mydate),month(mydate),day(mydate))
response.write formatdatetime(mydate,2)
else
response.write mydatestring & " is not a valid date<BR>"
end if

To make it even more robust, you should require dates to be supplied in
yyyy-mm-dd format.

Bob Barrows
 
S

scott

thanks to everyone, i knew split would work, but wanted to do it with string
functions for learning purposes. i have it now thanks big.
 

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,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top