Click here to Skip to main content
11,636,778 members (76,646 online)
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 21:36pm
Sumon5621.1K
Edited 14-Apr-13 21:39pm
Maciej Los187.3K
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 jyo.net 263
1 stibee 249
2 Sergey Alexandrovich Kryukov 229
3 Suvendu Shekhar Giri 225
4 OriginalGriff 183
0 OriginalGriff 8,941
1 Sergey Alexandrovich Kryukov 8,493
2 Mika Wendelius 6,909
3 F-ES Sitecore 2,388
4 Suvendu Shekhar Giri 2,320


Advertise | Privacy | Mobile
Web04 | 2.8.150728.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