Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
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 Los158.6K
v3
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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[^]
  Permalink  
Comments
Mohi Uddin Sumon at 15-Apr-13 3:53am
   
Thanks
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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[^].
  Permalink  
Comments
Mohi Uddin Sumon at 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)

  Print Answers RSS
0 OriginalGriff 325
1 DamithSL 265
2 CPallini 235
3 Maciej Los 190
4 Sergey Alexandrovich Kryukov 189
0 OriginalGriff 5,455
1 DamithSL 4,422
2 Maciej Los 3,860
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,010


Advertise | Privacy | Mobile
Web02 | 2.8.141216.1 | Last Updated 15 Apr 2013
Copyright © CodeProject, 1999-2014
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