This is more of a "mental" note for me than anything else, but I would like to share it with everyone as well. The DateTime.ToString method has 2 overloads, one with no parameters and the other with a string parameter. I would like to talk about the second one (hence the title of this post). You can pass a custom format pattern as the parameter to show the date anyway you want. Here are some of the possible values:
| Pattern | Description | Example |
| yyyy | Full Year | 2008 |
| yy | Year | 08 |
| MM | Month | 05 |
| MMM | Month | May |
| dd | Day | 02 |
| d | Day | 2 |
| HH | Hour Military Notation | 21 |
| h | Hour | 9 |
| mm | Minutes | 59 |
| ss | Seconds | 34 |
| ffff | Milliseconds | 0589 |
| zzz | Zone | -04:00 |
| t | Meridiem | P |
| tt | Full Meridiem | PM |
Of course you can combine any of these to form a pattern for example:
yyyyMMddHHmmssffff would yield something like 200805221122350587 and you would do so like this:
DateTime.Now.ToString("yyyyMMddHHmmssffff");
There are also some predefined patterns
| Pattern |
Example |
| d |
5/22/2008 |
| D |
Thursday, May 22, 2008 |
| f |
Thursday, May 22, 2008 11:22 AM |
| F |
Thursday, May 22, 2008 11:22:34 AM |
| g |
5/22/2008 11:22 AM |
| G |
5/22/2008 11:22:34 AM |
| m |
May 22 |
| o |
2008-05-22T11:22:34:04.0000000 |
| R |
Thu, 22 May 2008 11:22:34 EST |
| s |
2008-05-22T11:22:34 |
| t |
11:22 AM |
| T |
11:22:34 AM |
| u |
2008-05-22 11:22:04Z |
| U |
Thursday, May 22, 2008 11:22:34 AM |
| y |
May, 2008 |
Happy Programming!