5,693,062 members and growing! (15,094 online)
Email Password   helpLost your password?
Languages » C# » General     Beginner

Easy String to DateTime , DateTime to String and formating.

By Bertus Kruger

Easy String to DateTime , DateTime to String and formating.
C#, Windows, .NET, Visual Studio, Dev

Posted: 10 Jul 2006
Updated: 28 Aug 2007
Views: 84,752
Bookmarked: 26 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
19 votes for this Article.
Popularity: 3.97 Rating: 3.11 out of 5
4 votes, 21.1%
1
0 votes, 0.0%
2
4 votes, 21.1%
3
5 votes, 26.3%
4
6 votes, 31.6%
5
Note: This is an unedited contribution. If this article is inappropriate, needs attention or copies someone else's work without reference then please Report This Article

Introduction

In the few years that I have been a software developer I have work with plenty of different programming languages. And 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.

String to DateTime

 // String to DateTime

 String MyString;
 MyString = "1999-09-01 21:34 PM";

 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 communally 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 F's the higher the presision.

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

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. www.ChatBert.com

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Bertus Kruger



Occupation: Web Developer
Location: New Zealand New Zealand

Other popular C# articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 12 of 12 (Total in Forum: 12) (Refresh)FirstPrevNext
Generalit does not workmemberYakovb8:07 30 Jul '08  
GeneralRe: it does not workmemberBertus Kruger14:40 30 Jul '08  
QuestionExceptionmemberSoumini Ramakrishnan22:35 7 Apr '08  
AnswerRe: Exceptionmemberishakkulekci22:34 24 Jun '08  
GeneralHow to show month in capsmemberSabeessh P20:10 2 Jan '08  
AnswerRe: How to show month in capsmemberBertus Kruger11:06 3 Jan '08  
GeneralRe: How to show month in capsmemberSabeessh P18:14 3 Jan '08  
Generalit doesn't work man????memberprasonu1238:35 28 Aug '07  
GeneralRe: it doesn't work man????memberBertus Kruger10:51 28 Aug '07  
GeneralRe: it doesn't work man????memberBertus Kruger11:32 28 Aug '07  
GeneralThanks for the comment.memberBertus Kruger14:09 10 Jul '06  
GeneralSome supplemental info[sic]memberEnnis Ray Lynch, Jr.13:58 10 Jul '06  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 28 Aug 2007
Editor:
Copyright 2006 by Bertus Kruger
Everything else Copyright © CodeProject, 1999-2008
Web07 | Advertise on the Code Project