Date object to string formatting

Posted on: Thu Jul 10 18:26:48 -0700 2008. Updated on: Thu Jul 10 18:27:35 -0700 2008.
Category: DotNet

To get a DateTime object as a string, or get different parts of it, you can do something like this:

DateTime date = DateTime.Now;

string dirName = String.Format("{0:yyyy}{0:MM}{0:dd}", date);
This would output: YYYYMMDD with leading zeroes. To see a full list of formatting parameters got to: http://msdn.microsoft.com/en-us/library/8kb3ddd4(VS.71).aspx

Comments:

Add a Comment:

simple_captcha.jpg
(human authentication)


Ads

Categories

About

Random foliage

This website is meant to be a reference for ASP Dot Net developers. The entries are a compilation of things I've figured out how to do and that I deem useful to keep of track for future reference. Assumptions: web development with: C Sharp (vb sucks), visual studio 05/08, .net 3.5, meant for programmers. Written by: James Reategui.