Solved

how to split/parse a string

Posted on 2006-10-27
10
209 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
  • 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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 85

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 50 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:xDJR1875
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:
xDJR1875 earned 200 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Search by two fields at the same time using c# radio button 5 39
Need help on C# Linq query on object 6 53
Server Error 11 48
SQL Exceptions 3 40
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…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

863 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now