?
Solved

how to split/parse a string

Posted on 2006-10-27
10
Medium Priority
?
215 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
Industry Leaders: 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

Technology Partners: 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!

Question has a verified solution.

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

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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 …
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

764 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