|
C# Problem:
I have a string: " DH#2010-04-30-13:45:33"
Within this string a have certain charactes I want to use, and some I dont want to use:
2010 is my year,04 is my month,30 is my day,13 is my Hour,45 is my Minutes,33 is my Seconds
That string can come to me in many ways but this is the example for now.
I want to spesify the string to be used with another string, like a format guide.
This string is in my textboxbox and looks like this:
"___yyyy-MM-dd HH:mm:ss"
The first 3 underscores tells it to ignore the DH# part (3 Charactors)'
Then it needs to recognize the 2010 as yyyy, then 04 as MM etc.
The string can also be "#04#2010#30#13#45#33#
Then my format string will look like this: "_MM_yyyy_dd_HH_mm_ss_"
or
"DT04042010-134533"
Then my Format string will look like this: "__ddMMyyyy_HHmmss"
The format string I can specify, but the original string comes from a PLC in anyway it likes.
My format string specifies what each charactor in the string represents.
The underscore basicly tells the code to ignore the charactor in that place, in either:
yyyy,MM,dd or etc takes that digit and place it in the format I specify.
So I can use it to place it into the DateTime object.
Please help
|
|
|
|
|
I know of no standard way of specifying a wildcard for parsing DateTime strings, so you would have to remove the unwanted characters explicitly.
This is what you could do:
create a new format string and a new data string skipping the irrelevant characters (i.e. the '_' positions in the original format string), then pass the results to DateTime.TryParseExact
Example:
turn ___yyyy-MM-dd HH:mm:ss into yyyyMMddHHmmss
and DH#2010-04-30-13:45:33 into 20100430134533
[ADDED]
This approach will not work well all the time; IMO it does under these circumstances:
- all fields are numeric
- all fields have 2 digits, except year which needs 4.
[/ADDED]
|
|
|
|
|
Sorry but that won't work for DateTime.TryParse(). It needs the delimiters between date and time components.
I know the language. I've read a book. - _Madmatt
|
|
|
|
|
No separators required. I ran a little test:
DateTime dt;
bool OK=DateTime.TryParseExact("20100430134533", "yyyyMMddHHmmss", null, DateTimeStyles.None, out dt);
log("OK="+OK+" dt="+dt.ToString());
result:
15:10:45.819 CPTest.log-57 OK=True dt=30-Apr-2010 13:45:33
so it works just fine for me.
|
|
|
|
|
I ran a test also that failed. Of course I didn't use the expanded version you did
DateTime dt = new DateTime();
if(DateTime.TryParse("20100430134533", out dt))
{
}
I know the language. I've read a book. - _Madmatt
|
|
|
|
|
without any format string, it won't work, however that is not how I understood the OP; AFAICT he wants to explicitly specify the format string in his syntax, so I suggested to modify it and the data itself by basically removing the non-digits.
My approach works fine in some cases, not in all; I now expect problems with:
- single-digit values well as single-letter format specifiers accept one or two digits
- 12-hour notations
- and maybe month names
So a much more elaborate scheme is in order; one must make sure to use a CultureInvariant to avoid problems with time and date separator characters that may have been regionally set to something special (DateTime.Parse treats ":" and "/" in a special way).
|
|
|
|
|
Looks like a good class assignment.
In about three weeks we should see and Urgent, Pleaze help posting.
I know the language. I've read a book. - _Madmatt
|
|
|
|
|
I do like it as an exercise, as it is a rather open question, the task definition is incomplete ("format could be anything"??) and there probably is no best approach.
BTW: I do like V's approach in another thread branch below.
|
|
|
|
|
The first thing would be to split the sting into date and time components. From there replace the delimiter characters with appropriate separators for the part, / or - for date and : for time. Of course where there is no delimiter you'll have to resort to substring parsing or regex replacement. You'll have teh problem though of trying to determine what format the date is in, mm/dd/yyyy, dd/mm/yyyy, yyyy/mm/dd or any combination. The way to control this is specify the format to use and reject if not in that format.
Once you have the date and time parts, use DateTime.TryParse()
string date = "04/15/2010";
string time = "13:54:32";
DateTime dt = new DateTime();
if(DateTime.TryParse(date + " " + time, out dt))
{
}
I know the language. I've read a book. - _Madmatt
|
|
|
|
|
Hi guys thank you for the reply:
The problem I have is not with putting my string into the DateTime.
I need to pinpoint each character in my original string, to match my Format string.
To extract that values with in the original string. that represents its character in my format string.
eg:
String 1= "#2011-04-30 15:23:22"
String 2= "_yyyy-MM-dd HH:mm:ss"
so that yyyy = 2010, and MM=04 etc.
Then I need to use that values and build my string to put into the DateTime.
But both these 2 strings can be in any order, so if that happens, I can change my format string (String2)
to match that format in String1 and still get the value that correspond to the value I need from String 1.
Hope this clears it up a bit.
Thanks so much for the help up to date.
|
|
|
|
|
As Luc has shown you should be able to parse the strings and use it in DateTime.TryParse
DateTime dt;
DateTime.TryParseExact(string1, string2, null, DateTimeStyles.None, out dt);
I know the language. I've read a book. - _Madmatt
|
|
|
|
|
I'm afraid I changed my mind about my approach, see my other reply to you.
|
|
|
|
|
Is it possible to replace every non - numerical character in the string to 1 special character you use for delimeting? then TryParse the string with that format to a date.
V.
|
|
|
|
|
yes, that seems the best so far; provided:
- you choose a special character other than ':' and '/'
- you don't need AM/PM nor month names
public override void Test(int arg) {
test("2010.04.30.13.45.33", "yyyy.MM.dd.HH.mm.ss");
test("2010.04.30.13.4.3", "yyyy.MM.dd.HH.m.s");
test("2010.4.2.3.4.3", "yyyy.M.d.H.m.s");
}
private void test(string data, string format) {
DateTime dt;
bool OK=DateTime.TryParseExact(data, format, null, DateTimeStyles.None, out dt);
log("data="+data.PadRight(20)+"format="+format.PadRight(20)+" OK="+OK+" dt="+dt.ToString());
}
yields
data=2010.04.30.13.45.33 format=yyyy.MM.dd.HH.mm.ss OK=True dt=30-Apr-2010 13:45:33
data=2010.04.30.13.4.3 format=yyyy.MM.dd.HH.m.s OK=True dt=30-Apr-2010 13:04:03
data=2010.4.2.3.4.3 format=yyyy.M.d.H.m.s OK=True dt=02-Apr-2010 3:04:03
|
|
|
|
|
Basically what I was saying "replace the delimiter characters with appropriate separators for the part". I just didn't understand the need to support multiple formats.
I know the language. I've read a book. - _Madmatt
|
|
|
|
|
I would just use a Regular Expression Replace to standardize the string:
outdate = System.Text.RegularExpressions.Regex.Replace
(
indate
,
@"(?ix)(?:(?:DH\#(?'Year'\d\d\d\d)-(?'Month'\d\d)-(?'Day'\d\d)-(?'Hour'\d\d):(?'Minute'\d\d):(?'Second'\d\d))|
(?:\#(?'Month'\d\d)\#(?'Year'\d\d\d\d)\#(?'Day'\d\d)\#(?'Hour'\d\d)\#(?'Minute'\d\d)\#(?'Second'\d\d)\#)|
(?:DT(?'Day'\d\d)(?'Month'\d\d)(?'Year'\d\d\d\d)-(?'Hour'\d\d)(?'Minute'\d\d)(?'Second'\d\d)))"
,
@"${Year}-${Month}-${Day}-${Hour}:${Minute}:${Second}"
) ;
and only use one parse format.
|
|
|
|
|
Please can any one tell me how i can get which gridviewcell(gridview asp.net) is clicked.
please send me complete code.
Thanks in advance
jitendra sandu
|
|
|
|
|
See here[^] for complete details. You will have to write the code for yourself.
It's time for a new signature.
|
|
|
|
|
Hi Folks,
I currently have an issue where I'm trying to write some code to allow me to return an object from a web service. When this object is based on a Struct, all's good. However, when I replace the Struct with a [Serializable]Class, compile, and run the service, on invoking the method which is to return the class object, I receive the below error:
======================================================================================================
Server Error in '/' Application.
--------------------------------------------------------------------------------
This type of page is not served.
Description: The type of page you have requested is not served because it has been explicitly forbidden. The extension '.asp' may be incorrect. Please review the URL below and make sure that it is spelled correctly.
Requested URL: /default.asp
======================================================================================================
The reason I'd like to move from using a Struct to a Class is this would allow me to take advantage of interfaces and generics, to move the code which currently populates the Struct's properties from a DbDataReader's values into a delegate function, which can take a reader and a list of each field's type, and convert each record to an instance of the Struct.
Thanks in advance.
JB
|
|
|
|
|
|
The type of object has nothing to do with your problem. Basically, you have something trying to serve up default.asp and this extension has not been allowed by your web server. I suspect that you should actually have default.aspx in there.
"WPF has many lovers. It's a veritable porn star!" - Josh Smith As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
My blog | My articles | MoXAML PowerToys | Onyx
|
|
|
|
|
Hi guys,
does anyone know where i can get a list of mssql error codes. i need to enumerate the errors and treat anyone specifically. For ex: if it generates a unique key violation or foreign key violation, i could work on that.
Thanx.
He who goes for revenge must first dig two graves.
|
|
|
|
|
|
I've had to do that, but I handle only the ones I've actually encountered, not all of them.
My DatabaseAccessor[^] article contains my SqlServerErrorInfo.cs and DatabaseException.cs files which may be of use to you.
|
|
|
|
|
Hi Experts,
I am having a datatable object which is getting populated dynamically. My requirement is that i need to check whether the datatable contains any column with the name of "recordid". I have achieved this by doing the following
m_dataTable.Columns.Contains("recordid")
But my problem is that the column names might not come as "recordid" but might contain special characters. So my column name might be any of (but not restricted to) the following
1. Record Id
2. RECORD ID
3. Record_Id
4. Record-Id
I have a solution of passing each string to a method which is posted on this link but this approach will hamper performance.
Can anyone please help?
Thanks in advance!
Regards,
Samar
|
|
|
|