Click here to Skip to main content
Licence CPOL
First Posted 10 Jul 2006
Views 462,881
Bookmarked 72 times

Easy String to DateTime, DateTime to String and Formatting

By | 28 Aug 2007 | Article
Easy String to DateTime, DateTime to String and formatting

Introduction

In the few years that I have been a software developer, I have work with plenty of different programming languages. The first thing that causes you headaches in all of those languages are dates and how to work with them. In this little tutorial, I would like to show you how to work with dates in C# .NET 2.0.

String to DateTime

 // String to DateTime
 String MyString;
 MyString = "1999-09-01 21:34 PM";
 //MyString = "1999-09-01 21:34 p.m.";  //Depends on your regional settings

 DateTime MyDateTime;
 MyDateTime = new DateTime();
 MyDateTime = DateTime.ParseExact(MyString, "yyyy-MM-dd HH:mm tt",
                                  null);

DateTime to String

 //DateTime to String
 MyDateTime = new DateTime(1999, 09, 01, 21, 34, 00);
 String MyString;
 MyString = MyDateTime.ToString("yyyy-MM-dd HH:mm tt");

Format String For Dates

Your format string is your most important key. In most of my projects, I make it a constant and then refer to the constant value in my code.

The following is the most commonly used format characters:

d - Numeric day of the month without a leading zero.
dd - Numeric day of the month with a leading zero.
ddd - Abbreviated name of the day of the week.
dddd - Full name of the day of the week.

f,ff,fff,ffff,fffff,ffffff,fffffff - 
	Fraction of a second. The more Fs the higher the precision.

h - 12 Hour clock, no leading zero.
hh - 12 Hour clock with leading zero.
H - 24 Hour clock, no leading zero.
HH - 24 Hour clock with leading zero.

m - Minutes with no leading zero.
mm - Minutes with leading zero.

M - Numeric month with no leading zero.
MM - Numeric month with a leading zero.
MMM - Abbreviated name of month.
MMMM - Full month name.

s - Seconds with no leading zero.
ss - Seconds with leading zero.

t - AM/PM but only the first letter. 
tt - AM/PM ( a.m. / p.m.)

y - Year with out century and leading zero.
yy - Year with out century, with leading zero.
yyyy - Year with century.

zz - Time zone off set with +/-.

Conclusion

I hope that this helped a bit, even if it is only as a reference to create new format strings. (Always handy!!!:))

For more information, please visit my site.

License

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

About the Author

Bertus Kruger

Web Developer

New Zealand New Zealand

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 5 PinmemberMenon Santosh23:47 23 Jan '12  
NewsIts been years... PinmemberMarcel Valdez14:07 6 Jul '11  
GeneralNice Pinmemberadriancs5:57 6 Jun '11  
GeneralThanx Pinmemberrudedevil0921:09 13 May '11  
QuestionWhat about this? PinmemberAli Taghvajou1:58 6 Apr '11  
GeneralMy vote of 5 PinmemberPhillip Quinlan19:38 12 Sep '10  
Generalthaks Pinmembermahmoud_samour22:56 27 Feb '10  
GeneralIt was usefull. PinmemberMember 16670718:07 24 Feb '10  
GeneralThankyou :) PinmemberMcdonger12:38 12 Feb '10  
General10x Pinmemberhadaryona5:22 15 Sep '09  
Generalit does not work PinmemberYakovb7:07 30 Jul '08  
GeneralRe: it does not work PinmemberBertus Kruger13:40 30 Jul '08  
QuestionException PinmemberSoumini Ramakrishnan21:35 7 Apr '08  
AnswerRe: Exception Pinmemberishakkulekci21:34 24 Jun '08  
If you're using DateTime.ParseExact with a custom format string including slashes, don't forget to escape them.
 
After I've catched FormatExceptions several times, I've found this explanation:
 
'/' is the default date separator defined in DateTimeFormatInfo.DateSeparator. Therefore you have to escape '/' with '\' if you want to use it:
 
System.DateTime.ParseExact("2004/05/31 16:19:43", @"yyyy\/MM\/dd HH:mm:ss", null);
QuestionHow to show month in caps PinmemberSabeessh P19:10 2 Jan '08  
AnswerRe: How to show month in caps PinmemberBertus Kruger10:06 3 Jan '08  
AnswerRe: How to show month in caps PinmemberSabeessh P17:14 3 Jan '08  
Questionit doesn't work man???? Pinmemberprasonu1237:35 28 Aug '07  
AnswerRe: it doesn't work man???? PinmemberBertus Kruger9:51 28 Aug '07  
AnswerRe: it doesn't work man???? PinmemberBertus Kruger10:32 28 Aug '07  
GeneralThanks for the comment. PinmemberBertus Kruger13:09 10 Jul '06  
GeneralSome supplemental info[sic] PinmemberEnnis Ray Lynch, Jr.12:58 10 Jul '06  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120604.1 | Last Updated 28 Aug 2007
Article Copyright 2006 by Bertus Kruger
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid