Click here to Skip to main content
15,894,343 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Please explain the following code. Here dra is row of a temp table. Can I use convert, cast or parse method instead of string.Format. If possible please explain with example.


C#
DateTime datt = Convert.ToDateTime(DTLocalSc.Rows[0][4]);
dra["dat"] = string.Format("{0:dd-MM-yyyy}", datt);
Posted
Updated 14-Apr-13 21:39pm
v3

If the type of dat field is string (e.g. varchar) then you have to use String.Format. I strongly suggest you reading the documentation[^].
 
Share this answer
 
Comments
Sumon562 15-Apr-13 3:53am    
Thanks
What's to explain?
It gets a string date from a control, or array, converts it to a DateTime value, then formats it for the display as day-month-year. The only bit that you might not have seen before is the use of a formatting expression in a string.Format string:
{0:dd-MM-yyyy}
the 0 numbers the parameter that will supply the info
The colon specifies a format string is following
The dd-MM-yyyy part specifies the actual format to use for the DateTime value.

There is a list of the possible values for the DateTime formatting string here: Formatting a DateTime for display - format string description[^]
 
Share this answer
 
Comments
Sumon562 15-Apr-13 3:53am    
Thanks

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