DateTime

D

DaveL

I have Data Comming In in the format
YYYYmmdd

01012008 does net have a conversion for this type of string to get it back
to a DateTime
or do i have to Regx or substring it out

Thanks
DaveL
 
H

Hans Kesting

After serious thinking DaveL wrote :
I have Data Comming In in the format
YYYYmmdd

01012008 does net have a conversion for this type of string to get it back
to a DateTime
or do i have to Regx or substring it out

Thanks
DaveL

string s = "01022008";
DateTime dt;
if (DateTime.TryParseExact(s, "ddMMyyyy", CultureInfo.CurrentCulture,
DateTimeStyles.None, out dt))
Console.WriteLine("Success: {0}", dt);
else
Console.WriteLine("fail");

Note the formatstring:
dd = day (2 digits)
MM = month (2 digits), "mm" would be minutes!
yyyy = year (4 digits)

Hans Kesting
 
D

DaveL

Hi Hans and thanks alot
Befor i got back here and read your post
I Tried Regx , But i will Use your solution

example below

string sdt0 = "660810";
sdt0 = System.Text.RegularExpressions.Regex.Replace(sdt0,
@"(\d{2})(\d{2})(\d{2})", "$2/$3/$1");
Console.WriteLine(DateTime.Parse(sdt0));
Console.WriteLine(sdt0);
//MMDDYY
string sdt1 = "081066";
sdt1 = System.Text.RegularExpressions.Regex.Replace(sdt1,
@"(\d{2})(\d{2})(\d{2})", "$1/$2/$3");
Console.WriteLine(DateTime.Parse(sdt1));
Console.WriteLine(sdt1);
//YYYYMMDD
string sdt3 = "19660810";
sdt3 = System.Text.RegularExpressions.Regex.Replace(sdt3,
@"(\d{4})(\d{2})(\d{2})","$2/$3/$1");
Console.WriteLine(DateTime.Parse(sdt3));
//MMDDYYYY
string sdt4 = "08101966";
sdt4 = System.Text.RegularExpressions.Regex.Replace(sdt4,
@"(\d{2})(\d{2})(\d{4})","$1/$2/$3");
Console.WriteLine(DateTime.Parse(sdt4));
Console.ReadKey();
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top