/*
Example9_4.cs illustrates the use of DateTime properties and methods
*/
using System;
class Example9_4
{
public static void Main()
{
// use the Now and UtcNow properties to get the currrent
// date and time
Console.WriteLine("DateTime.Now = " + DateTime.Now);
Console.WriteLine("DateTime.UtcNow = " + DateTime.UtcNow);
DateTime myDateTime = DateTime.Now;
Console.WriteLine("myDateTime = " + myDateTime);
DateTime myDateTime2 = DateTime.UtcNow;
Console.WriteLine("myDateTime = " + myDateTime);
// display the Date, Day, DayOfWeek, DayOfYear, Ticks, and
// TimeOfDayProperties of myDateTime
Console.WriteLine("myDateTime.Date = " + myDateTime.Date);
Console.WriteLine("myDateTime.Day = " + myDateTime.Day);
Console.WriteLine("myDateTime.DayOfWeek = " + myDateTime.DayOfWeek);
Console.WriteLine("myDateTime.DayOfYear = " + myDateTime.DayOfYear);
Console.WriteLine("myDateTime.Ticks = " + myDateTime.Ticks);
Console.WriteLine("myDateTime.TimeOfDay = " + myDateTime.TimeOfDay);
// use the Compare() method to compare DateTime instances
DateTime myDateTime3 = new DateTime(2004, 1, 13);
DateTime myDateTime4 = new DateTime(2004, 1, 14);
Console.WriteLine("myDateTime3 = " + myDateTime3);
Console.WriteLine("myDateTime4 = " + myDateTime4);
int intResult = DateTime.Compare(myDateTime3, myDateTime4);
Console.WriteLine("DateTime.Compare(myDateTime3, myDateTime4) = " +
DateTime.Compare(myDateTime, myDateTime2));
// use the overloaded less than operator (<) to compare two
// DateTime instances
bool boolResult = myDateTime3 < myDateTime4;
Console.WriteLine("myDateTime3 < myDateTime4 is " +
boolResult);
// use the Equals() method to compare DateTime instances
boolResult = DateTime.Equals(myDateTime3, myDateTime4);
Console.WriteLine("DateTime.Equals(myDateTime3, myDateTime4) = " +
boolResult);
boolResult = myDateTime3.Equals(myDateTime4);
Console.WriteLine("myDateTime3.Equals(myDateTime4) is " +
boolResult);
// use the DaysInMonth() method to retrieve the number of days
// in a particular month and year
int days = DateTime.DaysInMonth(2004, 1);
Console.WriteLine("DateTime.DaysInMonth(2004, 1) = " +
days);
// use the IsLeapYear() method to determine if a particular
// year is a leap year
boolResult = DateTime.IsLeapYear(2004);
Console.WriteLine("DateTime.IsLeapYear(2004) = " +
boolResult);
// use the Parse() method to convert strings to DateTime instances
DateTime myDateTime5 = DateTime.Parse("1/13/2004");
DateTime myDateTime6 = DateTime.Parse("1/13/2004 23:10:30");
Console.WriteLine("myDateTime5 = " + myDateTime5);
Console.WriteLine("myDateTime6 = " + myDateTime6);
// use the Add() method to add a TimeSpan to a DateTime
TimeSpan myTimeSpan = new TimeSpan(1, 2, 4, 10);
DateTime myDateTime7 = myDateTime6.Add(myTimeSpan);
Console.WriteLine("myTimeSpan = " + myTimeSpan);
Console.WriteLine("myDateTime6.Add(myTimeSpan) = " +
myDateTime7);
// use the Subtract() method to subtract a TimeSpan from a DateTime
myDateTime7 = myDateTime6.Subtract(myTimeSpan);
Console.WriteLine("myDateTime6.Subtract(myTimeSpan) = " +
myDateTime7);
// use the overloaded addition operator (+) to add a TimeSpan
// to a DateTime
DateTime myDateTime8 = myDateTime6 + myTimeSpan;
Console.WriteLine("myDateTime6 + myTimeSpan = " + myDateTime8);
// use the overloaded subtraction operator (-) to subtract
// a TimeSpan from a DateTime
myDateTime8 = myDateTime6 - myTimeSpan;
Console.WriteLine("myDateTime6 - myTimeSpan = " + myDateTime8);
// use the AddYears(), AddMonths(), AddDays(), AddMinutes(), and
// AddSeconds() methods to add periods to a DateTime
DateTime myDateTime9 = new DateTime(2004, 1, 1);
Console.WriteLine("Initial myDateTime9 = " + myDateTime9);
myDateTime9 = myDateTime9.AddYears(1);
myDateTime9 = myDateTime9.AddMonths(5);
myDateTime9 = myDateTime9.AddDays(3);
myDateTime9 = myDateTime9.AddMinutes(30);
myDateTime9 = myDateTime9.AddSeconds(15);
Console.WriteLine("Final myDateTime9 = " + myDateTime9);
// use the ToFileTime() method to convert a DateTime to
// an operating system file timestamp
DateTime myDateTime10 = new DateTime(2004, 1, 15, 23, 2, 5);
long myFileTime = myDateTime10.ToFileTime();
Console.WriteLine("myDateTime10.ToFileTime() = " +
myDateTime10.ToFileTime());
// use the FromFileTime() method to convert
// an operating system file timestamp to a DateTime
DateTime myDateTime11 = DateTime.FromFileTime(myFileTime);
Console.WriteLine("DateTime.FromFileTime() = " +
myDateTime11);
// use the ToLongDateString() and ToShortDateString() methods
// to convert the date parts of a DateTime to long and short
// date strings
Console.WriteLine("myDateTime11 = " + myDateTime11);
Console.WriteLine("myDateTime11.ToLongDateString() = " +
myDateTime11.ToLongDateString());
Console.WriteLine("myDateTime11.ToShortDateString() = " +
myDateTime11.ToShortDateString());
// use the ToLongTimeString() and ToShortTimeString() methods
// to convert the time parts of a DateTime to long and short
// time strings
Console.WriteLine("myDateTime11.ToLongTimeString() = " +
myDateTime11.ToLongTimeString());
Console.WriteLine("myDateTime11.ToShortTimeString() = " +
myDateTime11.ToShortTimeString());
// use the ToString() method to convert a DateTime
// to a string
DateTime myDateTime12 = new DateTime(2004, 1, 12, 22, 2, 10);
Console.WriteLine("myDateTime12.ToString() = " +
myDateTime12.ToString());
Console.WriteLine("myDateTime12.ToString(\"MMMM dd, yyyy\") = " +
myDateTime12.ToString("MMMM dd, yyyy"));
Console.WriteLine("myDateTime12.ToString(\"d\") = " +
myDateTime12.ToString("d"));
Console.WriteLine("myDateTime12.ToString(\"D\") = " +
myDateTime12.ToString("D"));
Console.WriteLine("myDateTime12.ToString(\"f\") = " +
myDateTime12.ToString("f"));
Console.WriteLine("myDateTime12.ToString(\"F\") = " +
myDateTime12.ToString("F"));
Console.WriteLine("myDateTime12.ToString(\"g\") = " +
myDateTime12.ToString("g"));
Console.WriteLine("myDateTime12.ToString(\"G\") = " +
myDateTime12.ToString("G"));
Console.WriteLine("myDateTime12.ToString(\"m\") = " +
myDateTime12.ToString("m"));
Console.WriteLine("myDateTime12.ToString(\"r\") = " +
myDateTime12.ToString("r"));
Console.WriteLine("myDateTime12.ToString(\"s\") = " +
myDateTime12.ToString("s"));
Console.WriteLine("myDateTime12.ToString(\"t\") = " +
myDateTime12.ToString("t"));
Console.WriteLine("myDateTime12.ToString(\"T\") = " +
myDateTime12.ToString("T"));
Console.WriteLine("myDateTime12.ToString(\"u\") = " +
myDateTime12.ToString("u"));
Console.WriteLine("myDateTime12.ToString(\"U\") = " +
myDateTime12.ToString("U"));
Console.WriteLine("myDateTime12.ToString(\"y\") = " +
myDateTime12.ToString("y"));
}
}
DateTime属性使用
最新推荐文章于 2021-11-16 22:09:19 发布