Solved

how to split/parse a string

Posted on 2006-10-27
10
208 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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 

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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

708 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

13 Experts available now in Live!

Get 1:1 Help Now