Click here to Skip to main content
15,881,757 members
Articles / Programming Languages / C#
Article

Easy String to DateTime, DateTime to String and Formatting

Rate me:
Please Sign up or sign in to vote.
4.49/5 (70 votes)
28 Aug 2007CPOL 1.1M   111   33
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

C#
// 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

C#
//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)


Written By
Web Developer
New Zealand New Zealand
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionConversion issue Pin
ziaulh21-Sep-17 4:23
ziaulh21-Sep-17 4:23 
AnswerRe: Conversion issue Pin
Manuele Camilletti21-Sep-17 5:29
professionalManuele Camilletti21-Sep-17 5:29 
Try this
C#
string strDate = "09/20/2017 03:29:50 PM";
DateTime ltt = DateTime.ParseExact(strDate , "MM/dd/yyyy hh:mm:ss tt", CultureInfo.InvariantCulture);

GeneralMy vote of 4 Pin
Heriberto Lugo12-Oct-14 14:19
Heriberto Lugo12-Oct-14 14:19 
GeneralMy vote of 1 Pin
josephmundadan4-Aug-14 11:56
josephmundadan4-Aug-14 11:56 
GeneralRe: My vote of 1 Pin
warnerlego10-Oct-14 20:17
warnerlego10-Oct-14 20:17 
GeneralMy vote of 3 Pin
luckyrockstar50518-Jul-14 3:10
luckyrockstar50518-Jul-14 3:10 
GeneralWorks perfectly Pin
Member 83423408-Apr-13 4:40
Member 83423408-Apr-13 4:40 
QuestionAbout time zone Pin
Romain TAILLANDIER20-Nov-12 23:24
Romain TAILLANDIER20-Nov-12 23:24 
QuestionDatetimepicker culture Pin
Raghavendra Rao 55819-Nov-12 1:51
Raghavendra Rao 55819-Nov-12 1:51 
GeneralMy vote of 5 Pin
small219-Sep-12 21:04
small219-Sep-12 21:04 
GeneralMy vote of 5 Pin
Menon Santosh23-Jan-12 23:47
professionalMenon Santosh23-Jan-12 23:47 
NewsIts been years... Pin
Marcel Valdez6-Jul-11 14:07
Marcel Valdez6-Jul-11 14:07 
GeneralNice Pin
adriancs6-Jun-11 5:57
mvaadriancs6-Jun-11 5:57 
GeneralThanx Pin
rudedevil0913-May-11 21:09
rudedevil0913-May-11 21:09 
QuestionWhat about this? Pin
Ali Taghvajou6-Apr-11 1:58
professionalAli Taghvajou6-Apr-11 1:58 
GeneralMy vote of 5 Pin
Phillip Quinlan12-Sep-10 19:38
Phillip Quinlan12-Sep-10 19:38 
Generalthaks Pin
mahmoud_samour27-Feb-10 22:56
mahmoud_samour27-Feb-10 22:56 
GeneralIt was usefull. Pin
Member 166707124-Feb-10 8:07
Member 166707124-Feb-10 8:07 
GeneralThankyou :) Pin
JackBradford12-Feb-10 12:38
JackBradford12-Feb-10 12:38 
General10x Pin
hadaryona15-Sep-09 5:22
hadaryona15-Sep-09 5:22 
Generalit does not work Pin
Yakovb30-Jul-08 7:07
Yakovb30-Jul-08 7:07 
GeneralRe: it does not work Pin
Bertus Kruger30-Jul-08 13:40
Bertus Kruger30-Jul-08 13:40 
QuestionException Pin
Soumini Ramakrishnan7-Apr-08 21:35
Soumini Ramakrishnan7-Apr-08 21:35 
AnswerRe: Exception Pin
ishakkulekci24-Jun-08 21:34
ishakkulekci24-Jun-08 21:34 
QuestionHow to show month in caps Pin
Sabeessh P2-Jan-08 19:10
Sabeessh P2-Jan-08 19:10 

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

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