Thursday, May 8, 2008

Date Time Formats Operations

DateTime Value Manipulation

Addition and Subtraction Operators

----------------------------------------------------------------------------
----------------------------------------------------------------------------

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
----------------------------------------------------------------------------
----------------------------------------------------------------------------

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

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

// Subtract twenty-five days
theDate -= TimeSpan.Parse("25.00:00:00"); // theDate = 8 Dec 2006 21:16:01
------------------------------------------------------------------------------
----------------------------------------------------------------------------
Add Methods

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

theDate = theDate.Add(duration); // theDate = 3 Jan 2007 21:16:01

----------------------------------------------------------------------------
----------------------------------------------------------------------------

DateTime theDate = DateTime.Parse("2 Jan 2007");

theDate = theDate.AddYears(1); // theDate = 2 Jan 2008
theDate = theDate.AddMonths(2); // theDate = 2 Mar 2008
theDate = theDate.AddDays(1.5); // theDate = 3 Mar 2008 12:00:00
theDate = theDate.AddHours(-6); // theDate = 2 Mar 2008 06:00:00
theDate = theDate.AddMinutes(150); // theDate = 2 Mar 2008 08:30:00
theDate = theDate.AddSeconds(10.5); // theDate = 2 Mar 2008 08:30:10.5
theDate = theDate.AddMilliseconds(499); // theDate = 2 Mar 2008 08:30:10.999
theDate = theDate.AddTicks(10000); // theDate = 2 Mar 2008 08:30:11

Also takes -ve value

E.g .AddYears(-1); etc

----------------------------------------------------------------------------
----------------------------------------------------------------------------
Subtract Method
----------------------------------------------------------------------------
----------------------------------------------------------------------------


DateTime theDate = DateTime.Parse("2 Jan 2007");
TimeSpan subtract = TimeSpan.Parse("1.01:00:00");
DateTime startDate = DateTime.Parse("1 Jan 2007");
DateTime endDate = DateTime.Parse("2 Jan 2007 12:00:00");
TimeSpan diff;

// Subtract a TimeSpan from a DateTime
theDate = theDate.Subtract(subtract); // theDate = 31 Dec 2006 23:00:00

// Find the difference between two dates
diff = endDate.Subtract(startDate); // diff = 1.12:00:00

----------------------------------------------------------------------------
----------------------------------------------------------------------------

UTC and Local DateTime Conversion

----------------------------------------------------------------------------
----------------------------------------------------------------------------


DateTime localDate = DateTime.Parse("1 Jul 2007 12:00");

DateTime utcDate = localDate.ToUniversalTime(); // 1 Jul 2007 05:30
DateTime rangoon = utcDate.ToLocalTime(); // 1 Jul 2007 12:00


----------------------------------------------------------------------------
----------------------------------------------------------------------------
Conversion from DateTime to String
----------------------------------------------------------------------------
----------------------------------------------------------------------------

Basic Conversions
----------------------------------------------------------------------------
----------------------------------------------------------------------------

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

result = theDate.ToLongDateString(); // result = "03 January 2007"
result = theDate.ToShortDateString(); // result = "03/01/2007"
result = theDate.ToLongTimeString(); // result = "21:25:30"
result = theDate.ToShortTimeString(); // result = "21:25"

Using ToString with Format Specifiers
----------------------------------------------------------------------------
----------------------------------------------------------------------------

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"

Using Picture Formats for Custom Formatting (hh:mm:dd etc.)

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

result = theDate.ToString("d-MM-yy"); // result = "3-01-07"
result = theDate.ToString("HH:mm"); // result = "21:25"
result = theDate.ToString("h:mm tt"); // result = "9:25 PM"



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.


We can implement like Response.Write(localDate.ToString("dddd , MMM , dd, yyyy"));

IMPORTANT NOTE

Some of the formatting codes use the same letter as a format specifier. When used within a format string that is greater than one character in length this does not present a problem. If you need to use a single character picture format string, the letter must be preceded by a percentage sign (%) to indicate that the standard format specifier is not to be used.


----------------------------------------------------------------------------
----------------------------------------------------------------------------

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"

----------------------------------------------------------------------------
----------------------------------------------------------------------------

No comments: