Click here to Skip to main content
11,412,834 members (74,318 online)
Click here to Skip to main content

Easy String to DateTime, DateTime to String and Formatting

, 28 Aug 2007 CPOL
Rate this:
Please Sign up or sign in to vote.
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!!!Smile | :) )

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)

Share

About the Author

Bertus Kruger
Web Developer
New Zealand New Zealand
No Biography provided

Comments and Discussions

 
GeneralMy vote of 4 Pin
Heriberto Lugo at 12-Oct-14 15:19
memberHeriberto Lugo12-Oct-14 15:19 
GeneralMy vote of 1 Pin
josephmundadan at 4-Aug-14 12:56
memberjosephmundadan4-Aug-14 12:56 
GeneralRe: My vote of 1 Pin
warnerlego at 10-Oct-14 21:17
memberwarnerlego10-Oct-14 21:17 
GeneralMy vote of 3 Pin
luckyrockstar505 at 18-Jul-14 4:10
memberluckyrockstar50518-Jul-14 4:10 
GeneralWorks perfectly Pin
Member 8342340 at 8-Apr-13 5:40
memberMember 83423408-Apr-13 5:40 
GeneralGood one. Pin
bilal78699 at 17-Mar-13 23:01
memberbilal7869917-Mar-13 23:01 
QuestionAbout time zone Pin
Romain TAILLANDIER at 21-Nov-12 0:24
memberRomain TAILLANDIER21-Nov-12 0:24 
QuestionDatetimepicker culture Pin
Raghavendra Rao 558 at 19-Nov-12 2:51
memberRaghavendra Rao 55819-Nov-12 2:51 
GeneralMy vote of 5 Pin
small2 at 19-Sep-12 22:04
membersmall219-Sep-12 22:04 
GeneralMy vote of 5 Pin
Menon Santosh at 24-Jan-12 0:47
memberMenon Santosh24-Jan-12 0:47 
NewsIts been years... Pin
Marcel Valdez at 6-Jul-11 15:07
memberMarcel Valdez6-Jul-11 15:07 
GeneralNice Pin
adriancs at 6-Jun-11 6:57
memberadriancs6-Jun-11 6:57 
GeneralThanx Pin
rudedevil09 at 13-May-11 22:09
memberrudedevil0913-May-11 22:09 
QuestionWhat about this? Pin
Ali Taghvajou at 6-Apr-11 2:58
memberAli Taghvajou6-Apr-11 2:58 
GeneralMy vote of 5 Pin
Phillip Quinlan at 12-Sep-10 20:38
memberPhillip Quinlan12-Sep-10 20:38 
Generalthaks Pin
mahmoud_samour at 27-Feb-10 23:56
membermahmoud_samour27-Feb-10 23:56 
GeneralIt was usefull. Pin
Member 1667071 at 24-Feb-10 9:07
memberMember 166707124-Feb-10 9:07 
GeneralThankyou :) Pin
Mcdonger at 12-Feb-10 13:38
memberMcdonger12-Feb-10 13:38 
General10x Pin
hadaryona at 15-Sep-09 6:22
memberhadaryona15-Sep-09 6:22 
Generalit does not work Pin
Yakovb at 30-Jul-08 8:07
memberYakovb30-Jul-08 8:07 
GeneralRe: it does not work Pin
Bertus Kruger at 30-Jul-08 14:40
memberBertus Kruger30-Jul-08 14:40 
QuestionException Pin
Soumini Ramakrishnan at 7-Apr-08 22:35
memberSoumini Ramakrishnan7-Apr-08 22:35 
AnswerRe: Exception Pin
ishakkulekci at 24-Jun-08 22:34
memberishakkulekci24-Jun-08 22:34 
QuestionHow to show month in caps Pin
Sabeessh P at 2-Jan-08 20:10
memberSabeessh P2-Jan-08 20:10 
AnswerRe: How to show month in caps Pin
Bertus Kruger at 3-Jan-08 11:06
memberBertus Kruger3-Jan-08 11:06 
AnswerRe: How to show month in caps Pin
Sabeessh P at 3-Jan-08 18:14
memberSabeessh P3-Jan-08 18:14 
Questionit doesn't work man???? Pin
prasonu123 at 28-Aug-07 8:35
memberprasonu12328-Aug-07 8:35 
AnswerRe: it doesn't work man???? Pin
Bertus Kruger at 28-Aug-07 10:51
memberBertus Kruger28-Aug-07 10:51 
AnswerRe: it doesn't work man???? Pin
Bertus Kruger at 28-Aug-07 11:32
memberBertus Kruger28-Aug-07 11:32 
GeneralThanks for the comment. Pin
Bertus Kruger at 10-Jul-06 14:09
memberBertus Kruger10-Jul-06 14:09 
GeneralSome supplemental info[sic] Pin
Ennis Ray Lynch, Jr. at 10-Jul-06 13:58
memberEnnis Ray Lynch, Jr.10-Jul-06 13:58 

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.

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