Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

how to split/parse a string

Posted on 2006-10-27
10
Medium Priority
?
216 Views
Last Modified: 2010-04-16
How can I get the day portion out of the following date:

I have: 10/20/2006 12:00:00 AM
want: 10/20/2006

Thanks :-)
0
Comment
Question by:acdagirl
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 35

Expert Comment

by:Raynard7
ID: 17822489
So this is a date variable or a string vaiable?

If this is always a data and it is always in the form mm/dd/yyyy you could just do

String str1 = "10/20/2006 12:00:00 AM";
String str2 = str1.Substring(0,10);

Otherwise you may consider grabbing this date out from the string with a regular expression
0
 

Author Comment

by:acdagirl
ID: 17822546
it's actually a string that has been cast from a Calendar control... so it'll be in the datetime format... so that's not always going to be 10 long? e.g. 10/9/2006...
0
 
LVL 35

Expert Comment

by:Raynard7
ID: 17822594
Hi,

Down the regular expression path you could use

^(\d{1,2}/\d{1,2}/\d{4})

which would match the date
http://www.experts-exchange.com/Programming/Programming_Languages/C_Sharp/Q_20790472.html
and
www.codeproject.com/useritems/RegularExpressionTester.asp
give examples on how to use regular expressions

alternativley have you tried casting it as a date (using the convert function) then using the tostring method to display this in mm/dd/yyyy format?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 35

Expert Comment

by:Raynard7
ID: 17822605
Also just an aside the above regular expression will find 1 or 2 digits followed by / followed by 1 or two digits followed by / followed by 4 digits,

now you potentially could do more validatioin (ie month if 2 digits can only be proceeded with 0,1,2) but since this is from a calendar control it should not be necessary
0
 

Author Comment

by:acdagirl
ID: 17822656
it will only convert.todatetime which is not what I want...
no time to play with Regex right now...

0
 

Author Comment

by:acdagirl
ID: 17822726
actually here's what I need - maybe you'll have better strategy as I'm seeing that what I'm doing/asking won't actually help me.

I get a date from asp:calendar, which I need to compare to sql table datetime. The problem is that the asp:calendar date always has time value 12:00:00 AM and the sql table field has specific timestamp e.g. 1:23:00 PM.

So I'm trying to only get part of the asp:calendar date e.g. 10/20/2006 from 10/20/2006 12:00:00 AM and compare to the day part of the sql field e.g. 10/20/2006 1:23:45 PM...

there might be a much better solution than what I'm attempting here with these casts, splits, etc.

any thoughts?
0
 
LVL 86

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 200 total points
ID: 17822839
Can't you just split it or find the first space?

string origDate = "10/20/2006 12:00:00 AM";
string[] parts = origDate.Split(' ');
string myDate = parts[0] + " 12:00:00 AM";
0
 
LVL 17

Expert Comment

by:Daniel Reynolds
ID: 17824091
Use System.Convert to do the following

string sdate = "10/20/2006 12:00:00 AM";
DateTime oDate = System.Convert.ToDateTime(sdate);
                  
int iDay = oDate.Day;

There are many other options once you get it to a DateTime variable.

hth,

Dan
0
 
LVL 17

Accepted Solution

by:
Daniel Reynolds earned 800 total points
ID: 17824097

this appears to be the one you want after reading the initial post.

      oDate.ToShortDateString()
0
 

Author Comment

by:acdagirl
ID: 17825339
the problem is when casting back to a datetime to compare to the sql field?
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Suggested Courses

609 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question