473,405 Members | 2,349 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Getting Date from DateTime

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
Aug 10 '05 #1
5 8109
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
Aug 10 '05 #2
"scott" wrote in message news:eD**************@tk2msftngp13.phx.gbl...
: 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
Aug 10 '05 #3
"scott" <sc***@sbailey.com> wrote in message
news:eD**************@tk2msftngp13.phx.gbl...
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.
Aug 10 '05 #4
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(m ydate))
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.
Aug 10 '05 #5
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:dd**********@yarrow.open.ac.uk...
"scott" <sc***@sbailey.com> wrote in message
news:eD**************@tk2msftngp13.phx.gbl...
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.

Aug 10 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Gerrit Holl | last post by:
Posted with permission from the author. I have some comments on this PEP, see the (coming) followup to this message. PEP: 321 Title: Date/Time Parsing and Formatting Version: $Revision: 1.3 $...
4
by: Russell | last post by:
I'm having a fit with a query for a range of dates. The dates are being returned from a view. The table/field that they are being selected from stores them as varchar and that same field also...
15
by: Philip Mette | last post by:
I am begginner at best so I hope someone that is better can help. I have a stored procedure that updates a view that I wrote using 2 cursors.(Kind of a Inner Loop) I wrote it this way Because I...
7
by: Adrian | last post by:
I hit on this problem converting a VB.NET insurance application to C#. Age next birthday calculated from date of birth is often needed in insurance premium calculations. Originally done using...
8
by: John Slate | last post by:
I have a situation where users will input a start date and an end date, then return the number of weekdays only. Is this possible with the DateTime class? *** Sent via Developersdex...
7
by: Drygast | last post by:
I have a form where I would like to get the todays date and when the date is 15 or less I would like to assign a textfield the last date of previous month and when the date is larger then 15 I...
10
by: kyosohma | last post by:
Hi, I am working on a timesheet application in which I need to to find the first pay period in a month that is entirely contained in that month to calculate vacation time. Below are some example...
0
yasirmturk
by: yasirmturk | last post by:
Standard Date and Time Functions The essential date and time functions that every SQL Server database should have to ensure that you can easily manipulate dates and times without the need for any...
3
by: sailaja71185 | last post by:
Here is my code: using System.Data; using System.Linq; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls;
0
by: sumansms | last post by:
Hi All, I am getting wrong date when I am using DateTime.Parse function. The following is the code.. DateValue = DateTime.Parse(datarow.ToString()).ToString("MM/dd/yyyy"); I am taking the data...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.