Click here to Skip to main content
11,496,146 members (355 online)
The site is currently in read-only mode for maintenance. Posting of new items will be available again shortly.
See more: C#4.0
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.

DateTime datt = Convert.ToDateTime(DTLocalSc.Rows[0][4]);
dra["dat"] = string.Format("{0:dd-MM-yyyy}", datt);
Posted 14-Apr-13 22:36pm
Edited 14-Apr-13 22:39pm
Maciej Los183.5K
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:
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[^]
Mohi Uddin Sumon at 15-Apr-13 3:53am
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[^].
Mohi Uddin Sumon at 15-Apr-13 3:53am

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 10,401
1 OriginalGriff 8,910
2 Sascha Lefèvre 3,899
3 Maciej Los 3,422
4 Richard Deeming 2,600

Advertise | Privacy | Mobile
Web04 | 2.8.150520.1 | Last Updated 15 Apr 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100