C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Communities   Interview   Jobs   Projects   Offshore Development    
Silverlight Tutorials | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Revenue Sharing |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...

New Feature: Community Sites: Create your own .NET community website and start earning from Google AdSense ! It's Free !




Formatting Date and Time using the DateTime object


Posted Date: 18 Mar 2004    Resource Type: Articles    Category: .NET Framework

Posted By: Tony John       Member Level: Gold
Rating:     Points: 10



.Net provides the class called 'DateTime' to work with date and time. There are several useful methods and properties exposed by this class. The most commonly used ones may the two static properties .Today and .Now.

You can access a static property of a class without instantiating it. See the following examples:

DateTime.Today - returns the current date.
DateTime.Now - returns the current time.


You can use the following methods to convert a date object to a string.

ToShortDateString()
ToLongDateString()
ToShortTimeString()
ToLongTimeString()

Custom formatting for datetime

The ToString() method of the DateTime class can be used to format the
date & time the way you want.

ToString() method of the DateTime class takes a 'formatter string' as
parameter. Here you can specify the format you want. There are various
formatting patterns you can use to format the date and time.


string formattedDate;

// Format : 07 / 03 / 2004
formattedDate = DateTime.Now.ToString("dd / MM / yyyy");

// Format : 7 / 3 / 2004 (without the preceding zeroes)
formattedDate = DateTime.Now.ToString("d / M / yyyy");

// Format : 07 / Mar / 2004
formattedDate = DateTime.Now.ToString("dd / MMM / yy");

// How to get the name of the day ?
// Format : Wednesday
formattedDate = DateTime.Today.DayOfWeek.ToString();

Formatting Patterns

The following list shows the various patterns that can be used to
convert the datetime object to custom formatted string. The patterns are
case-sensitive; for example, "MM" is different from "mm".

If the custom pattern contains white-space characters or characters
enclosed in single quotation marks, the output string will also contain
those characters. Characters not part of a format pattern or not format
characters are reproduced 'as it is'.

Pre-defined Format Patterns and Description (From the MSDN Documentation)

d The day of the month. Single-digit days will not have a leading
zero.

dd The day of the month. Single-digit days will have a leading zero.

ddd The abbreviated name of the day of the week, as defined in AbbreviatedDayNames.

dddd The full name of the day of the week, as defined in DayNames.

M The numeric month. Single-digit months will not have a leading zero.

MM The numeric month. Single-digit months will have a leading zero.

MMM The abbreviated name of the month, as defined in AbbreviatedMonthNames.

MMMM The full name of the month, as defined in MonthNames.

y The year without the century. If the year without the century is less than 10,
the year is displayed with no leading zero.

yy The year without the century. If the year without the century is less than 10,
the year is displayed with a leading zero.

yyyy The year in four digits, including the century.

gg The period or era. This pattern is ignored if the date to be formatted does
not have an associated period or era string.

h The hour in a 12-hour clock. Single-digit hours will not have a leading zero.

hh The hour in a 12-hour clock. Single-digit hours will have a leading zero.

H The hour in a 24-hour clock. Single-digit hours will not have a leading zero.

HH The hour in a 24-hour clock. Single-digit hours will have a leading zero.

m The minute. Single-digit minutes will not have a leading zero.

mm The minute. Single-digit minutes will have a leading zero.

s The second. Single-digit seconds will not have a leading zero.

ss The second. Single-digit seconds will have a leading zero.

f The fraction of a second in single-digit precision. The remaining digits are truncated.

ff The fraction of a second in double-digit precision. The remaining digits are truncated.

fff The fraction of a second in three-digit precision. The remaining digits are truncated.

ffff The fraction of a second in four-digit precision. The remaining digits are truncated.

fffff The fraction of a second in five-digit precision. The remaining digits are truncated.

ffffff The fraction of a second in six-digit precision. The remaining digits are truncated.

fffffff The fraction of a second in seven-digit precision. The remaining digits are truncated.

t The first character in the AM/PM designator defined in AMDesignator or PMDesignator, if any.

tt The AM/PM designator defined in AMDesignator or PMDesignator, if any.

z The time zone offset ("+" or "-" followed by the hour only). Single-digit hours will
not have a leading zero. For example, Pacific Standard Time is "-8".

zz The time zone offset ("+" or "-" followed by the hour only). Single-digit hours
will have a leading zero. For example, Pacific Standard Time is
"-08".

zzz The full time zone offset ("+" or "-" followed by the hour and minutes). Single-digit
hours and minutes will have leading zeros. For example, Pacific Standard Time is "-08:00".

: The default time separator defined in TimeSeparator.

/ The default date separator defined in DateSeparator.

% c - Where c is a format pattern if used alone. The "%" character can be omitted if the
format pattern is combined with literal characters or other format patterns.

\ c - Where c is any character. Displays the character literally. To display the backslash
character, use "\\".




Responses

Author: Detka    17 Aug 2007Member Level: Bronze   Points : 0
Very helpful and comprehensive


Feedbacks      
Popular Tags   What are tags ?   Search Tags  
(No tags found.)

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: How to get the name of the week day in any language
Previous Resource: Adding and subtracting date and time
Return to Discussion Resource Index
Post New Resource
Category: .NET Framework


Post resources and earn money!
 
Related Resources



dotNet Slackers   BizTalk Adaptors    Web Design

doors in nj

Contact Us    Privacy Policy    Terms Of Use