|
In project development, often encounter the date of treatment. For example, a query, you may often encounter query by time period, sometimes a month removed the default data. When we submit the data will need to record the current date, and so on. Here's a look at some of the commonly used methods.
First, DateTime is a struct. In many cases, it will as a class. But it really is not described on MSDN as follows:
DateTime structure: that moment in time, generally indicates the date and time of day. grammar:
[SerializableAttribute]
public struct DateTime: IComparable, IFormattable,
IConvertible, ISerializable, IComparable , IEquatable
MSDN connection: MSDN DateTime structure
One, DateTime.Now property
Instantiate a DateTime object, you can specify the number as the date to get a DateTime object. The property is available DateTime.Now current time. If you want to by year, month, day respectively, the statistics can also be used DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day get. Similarly, the current minutes and seconds can also get in such a manner. You can also add a period of time and other operations at the current time.
static void Main (string [] args)
{
DateTime newChina = new DateTime (1949, 10, 1);
Console.WriteLine (newChina);
Console.WriteLine ( "Current time:");
Console.WriteLine ( "{0} of {1} months {2} day", DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day);
Console.WriteLine ( "{0} when {1} min, {2} seconds", DateTime.Now.Hour, DateTime.Now.Minute, DateTime.Now.Second);
Console.WriteLine ( "Three days later: {0}", DateTime.Now.AddDays (3));
Console.ReadLine ();
}
Two, ToString method
There are four ways overloads of the ToString method DateTime. One way to allow incoming overloaded String, which means you can convert the current DateTime object to an equivalent string form. For example, we output the current time, date according to yyyy-mm-dd format, press time hh: mm: ss format.
Console.WriteLine (DateTime.Now.ToString ( "yyyy-MM-dd"));
Console.WriteLine (DateTime.Now.ToString ( "hh: mm: ss"));
There is a need to provide an overloaded form IFormatProvider, using the specified culture-specific formatting information to convert the value of the current DateTime object to its equivalent string representation.
static void Main (string [] args)
{
CultureInfo jaJP = new CultureInfo ( "ja-JP");
jaJP.DateTimeFormat.Calendar = new JapaneseCalendar ();
DateTime date1 = new DateTime (1867, 1, 1);
DateTime date2 = new DateTime (1967, 1, 1);
try
{
Console.WriteLine (date2.ToString (jaJP));
Console.WriteLine (date1.ToString (jaJP));
}
catch (ArgumentOutOfRangeException)
{
Console.WriteLine ( "{0: d} is earlier than {1: d} or later than {2: d}",
date1,
jaJP.DateTimeFormat.Calendar.MinSupportedDateTime,
jaJP.DateTimeFormat.Calendar.MaxSupportedDateTime);
}
Console.ReadLine ();
}
I did not quite understand, the history of Japan so short? Baidu a moment after September 8, 1868, the Meiji Restoration.
Pay DateTimeFormatInfo class, there are relatively wide date and time format corresponding string.
Three, DaysInMonth method and method IsLeapYear
DaysInMonth method requires two parameters of type Int32 Returns specified number of days specified year month. About the number of days of the month, the majority only in February need special care about. The rest of the month, no matter what the number of days in the year are fixed. And then in February, not only for other months 30 days or 31 days, she points a leap year non-leap years.
static void Main (string [] args)
{
Console.WriteLine ( "2000 to 2015 the number of days in February");
for (int i = 2000; i <2015; i ++)
{
Console.WriteLine ( "{0} February are: {1} days", i, DateTime.DaysInMonth (i, 2));
}
Console.ReadLine ();
}
As can be seen from the output results, February 29 days of the year is a leap year. But in fact DateTime also provides a method of judging a leap year IsLeapYear, which as long as a Int32 parameter, if the input is a leap year returns true, otherwise returns false. (.Net Framework is so close, you want to give you something good package, the direct use of them as well.) If this method does not give themselves to the leap year in accordance with the rules to write a small method to judge.
static void Main (string [] args)
{
Console.WriteLine ( "2000 to 2015 the number of days in February");
for (int i = 2000; i <2015; i ++)
{
if (DateTime.IsLeapYear (i))
Console.WriteLine ( "{0} is a leap year, February has {1} days", i, DateTime.DaysInMonth (i, 2));
else
Console.WriteLine ( "{0} years is leap year, February has {1} days", i, DateTime.DaysInMonth (i, 2));
}
Console.ReadLine ();
}
Microsoft now has .NetFramework open source, which means you can view the source code themselves. DateTime.cs attach source link, and IsLeapYear method source code. Although only two or three lines of code, but in the actual development, you may remember a time leap formula, or not allowed to draw. Packaged good way for you to save a lot of time.
DateTime.cs source code IsLeapYear method
// Checks whether a given year is a leap year. This method returns true if
// Year is a leap year, or false if not.
//
public static bool IsLeapYear (int year) {
if (year <1 || year> 9999) {
throw new ArgumentOutOfRangeException ( "year", Environment.GetResourceString ( "ArgumentOutOfRange_Year"));
}
Contract.EndContractBlock ();
return year% 4 == 0 && (year% 100 = 0 || year% 400 == 0!);
}
to sum up
It introduced a relatively few common methods. Date operations encountered in their own projects, see more .NetFramework give us what kind of method. Many times with the methods provided patchwork look, you can easily get to the date or time we want. |
|
|
|