Getting Date from DateTime

Discussion in 'ASP General' started by scott, Aug 10, 2005.

  1. scott

    scott Guest

    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
     
    scott, Aug 10, 2005
    #1
    1. Advertising

  2. scott

    Curt_C [MVP] Guest

    scott wrote:
    > 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 " "

    --
    Curt Christianson
    site: http://www.darkfalz.com
    blog: http://blog.darkfalz.com
     
    Curt_C [MVP], Aug 10, 2005
    #2
    1. Advertising

  3. scott

    Roland Hall Guest

    "scott" wrote in message news:...
    : 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
     
    Roland Hall, Aug 10, 2005
    #3
  4. scott

    Phill. W Guest

    "scott" <> wrote in message
    news:...
    > 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.
     
    Phill. W, Aug 10, 2005
    #4
  5. scott wrote:
    > 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
    --
    Microsoft MVP -- ASP/ASP.NET
    Please reply to the newsgroup. The email account listed in my From
    header is my spam trap, so I don't check it very often. You will get a
    quicker response by posting to the newsgroup.
     
    Bob Barrows [MVP], Aug 10, 2005
    #5
  6. scott

    scott Guest

    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.


    "Phill. W" <P.A.Ward@o-p-e-n-.-a-c-.-u-k> wrote in message
    news:ddcrs2$bq3$...
    > "scott" <> wrote in message
    > news:...
    >> 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.
    >
    >
     
    scott, Aug 10, 2005
    #6
    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. Christos TZOTZIOY Georgiou
    Replies:
    3
    Views:
    732
    Christos TZOTZIOY Georgiou
    Sep 13, 2003
  2. Tim Peters
    Replies:
    0
    Views:
    555
    Tim Peters
    Sep 9, 2003
  3. mp
    Replies:
    1
    Views:
    403
    John Machin
    Jul 28, 2006
  4. Martin
    Replies:
    0
    Views:
    350
    Martin
    Dec 27, 2008
  5. Replies:
    2
    Views:
    774
    M.-A. Lemburg
    Jan 6, 2009
Loading...

Share This Page