Tuesday, August 11, 2009

DateTime Value Manipulation


TimeSpan:
In the following examples, a TimeSpan value is created as required using the new keyword and a TimeSpan constructor.

DateTime theDate = DateTime.Parse("2 Jan 2007 20:15:00");

// Add one hour, one minute and one second
theDate = theDate + new TimeSpan(1, 1, 1); // theDate = 2 Jan 2007 21:16:01

// Subtract twenty-five days
theDate = theDate - new TimeSpan(25, 0, 0, 0);// theDate = 8 Dec 2006 21:16:01


Using ToString with Format Specifiers:
This format specifier permits the use of a wider range of date and time styles.
It does, however, limit the user's control over how they view an application's output.

DateTime theDate = DateTime.Parse("3 Jan 2007 21:25:30");
string result;

result = theDate.ToString("d"); // result = "03/01/2007"
result = theDate.ToString("f"); // result = "03 January 2007 21:25"
result = theDate.ToString("y"); // result = "January 2007"


Available Format Specifiers

The full list of format specifiers for DateTime conversion is as follows:
Specifier Description Example
d Short date format. This is equivalent to using ToShortDateString."03/01/2007"
D Long date format. This is equivalent to using ToLongDateString. "03 January 2007"
f Date and time using long date and short time format. "03 January 2007 21:25"
F Date and time using long date and time format. "03 January 2007 21:25:30"
g Date and time using short date and time format. "03/01/2007 21:25"
G Date and time using short date and long time format. "03/01/2007 21:25:30"
m Day and month only. "03 January"
r Date and time in standard Greenwich Mean Time (GMT) format. "Wed, 03 Jan 2007 21:25:30 GMT"
s Sortable date and time format. The date elements start at the highest magnitude (year) and reduce along the string to the smallest magnitude (seconds). "2007-01-03T21:25:30"
t Short time format. This is equivalent to using ToShortTimeString."21:25"
T Long time format. This is equivalent to using ToLongTimeString. "21:25:30"
u Short format, sortable co-ordinated universal time. "2007-01-03 21:25:30Z"
U Long format date and time. "03 January 2007 17:25:30"
y Month and year only. "January 2007"


Available Picture Formatting Codes:

The above example shows several formats and the results. The full list of available formatting codes is as follows:
Specifier Description Examples
y One-digit year. If the year cannot be specified in one digit then two digits are used automatically. "7"
"95"
yy Two-digit year with leading zeroes if required. "07"
yyyy Full four-digit year. "2007"
g or gg Indicator of Anno Domini (AD). "A.D."
M One-digit month number. If the month cannot be specified in one digit then two digits are used automatically. "1"
"12"
MM Two-digit month number with leading zeroes if required."01"
MMM Three letter month abbreviation. "Jan"
MMMM Month name. "January"
d One-digit day number. If the day cannot be specified in one digit then two digits are used automatically. "3"
"31"
dd Two-digit day number with leading zeroes if required."03"
ddd Three letter day name abbreviation. "Wed"
dddd Day name. "Wednesday"
h One-digit hour using the twelve hour clock. If the hour cannot be specified in one digit then two digits are used automatically. "9" "12"
hh Two-digit hour using the twelve hour clock with leading zeroes if required. "09"
H One-digit hour using the twenty four hour clock. If the hour cannot be specified in one digit then two digits are used automatically. "1" "21"
HH Two-digit hour using the twenty four hour clock with leading zeroes if required. "09"
t Single letter indicator of AM or PM, generally for use with twelve hour clock values. "A"
"P"
tt Two letter indicator of AM or PM, generally for use with twelve hour clock values. "AM"
"PM"
m One-digit minute. If the minute cannot be specified in one digit then two digits are used automatically. "1"
"15"
mm Two-digit minute with leading zeroes if required. "01"
s One-digit second. If the second cannot be specified in one digit then two digits are used automatically. "1"
"59"
ss Two-digit second with leading zeroes if required. "01"
f Fraction of a second. Up to seven f's can be included to determine the number of decimal places to display. "0"
"0000000"
z One-digit time zone offset indicating the difference in hours between local time and UTC time. If the offset cannot be specified in one digit then two digits are used automatically. "+6"
"-1"
zz Two-digit time zone offset indicating the difference in hours between local time and UTC time with leading zeroes if required. "+06"


DateTime theDate = DateTime.Parse("3 Jan 2007 21:25:30");
string result;

result = theDate.ToString("d"); // result = "03/01/2007"
result = theDate.ToString("%d"); // result = "3"



ex: string DMYformat = DateTime.Today.ToString("dd-MM-yyyy"); //11-08-2009

..

No comments:

Post a Comment

Followers