Solved

how to split/parse a string

Posted on 2006-10-27
10
212 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
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 
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 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: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 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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
C# Formatting DateTime String Value 5 67
c# ftp code 3 54
Load XML element 3 42
Why am I losing my session variable value 11 35
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
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.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

739 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