Click here to Skip to main content
15,919,245 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I got an array with a date set as 071712... no / slash characters for the date, no dash. nothing...just plain 071712 (coming from a text file).

I need to conver the date so I can include it on a sql server insert. I'm calling a stored procedure for the insert. So far I got this:



C#
DateTime  date = Convert.ToDateTime(fileLines[4]);  // This is not working so far.


(date will be used as a parm for the stored procedure)

Pls share some code. Thank you.
Posted
Updated 3-Sep-12 17:59pm
v2

C#
CultureInfo provider = CultureInfo.InvariantCulture;
DateTime date= DateTime.ParseExact(fileLines[4], "MMddyy", provider);
 
Share this answer
 
v2
Comments
Kumar, Ravikant 3-Sep-12 23:53pm    
Correct Format (MMddyy) is essential. Otherwise it gives FormatException: String was not recognized as a valid DateTime.
Use string format to parse it correctly.
C#
string date = "071712";
string format = "MMddyy";
DateTime parsedDate = DateTime.ParseExact(date, format, CultureInfo.InvariantCulture);

Hope it will help you.

Different DateTime Formats are:

d - Numeric day of the month without a leading zero.
dd - Numeric day of the month with a leading zero.
ddd - Abbreviated name of the day of the week.
dddd - Full name of the day of the week.

f,ff,fff,ffff,fffff,ffffff,fffffff - Fraction of a second. The more Fs the higher the precision.

h - 12 Hour clock, no leading zero.
hh - 12 Hour clock with leading zero.
H - 24 Hour clock, no leading zero.
HH - 24 Hour clock with leading zero.

m - Minutes with no leading zero.
mm - Minutes with leading zero.

M - Numeric month with no leading zero.
MM - Numeric month with a leading zero.
MMM - Abbreviated name of month.
MMMM - Full month name.

s - Seconds with no leading zero.
ss - Seconds with leading zero.

t - AM/PM but only the first letter.
tt - AM/PM ( a.m. / p.m.)

y - Year with out century and leading zero.
yy - Year with out century, with leading zero.
yyyy - Year with century.

zz - Time zone off set with +/-.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900