Home PC Games Linux Windows Database Network Programming Server Mobile  
  Home \ Programming \ C # DateTime structure common method     - Oracle Sql Loader tool has shown signs (Database)

- A brief description of Java 8 new features introduced syntax (Programming)

- Linux dynamic libraries and Guide (Programming)

- How to properly set up a Linux swap partition (Linux)

- How to avoid two Chrome icon appears in ELementary OS Freya (Linux)

- Running the open-source Swift under Linux platform (Linux)

- Teach you the Ubuntu Server enabled SSH multifactor authentication (Server)

- Redis logging system (Database)

- Linux upgrade GCC 4.8.1 clear and concise tutorials (Ubuntu 12.04 64-bit version as an example) (Linux)

- Batch kill processes using awk command (Linux)

- AngularJS application unit testing started (Programming)

- Linux rights management (Linux)

- Achieve camera preview by ffplay (Linux)

- Four safety delete files under Linux tools (Linux)

- Use Ansible installation NGINX and NGINX Plus (Server)

- Understand the security restore accidentally deleted critical system files (Linux)

- Linux mount command Detailed (Linux)

- About Hibernate cache, you want the latest data have trouble even session.clear (Database)

- Nginx caching using the official guide (Server)

- Android using shape drawable material production (Programming)

  C # DateTime structure common method
  Add Date : 2016-09-26      
  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:

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);
                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}",
            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));
                    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.
- Linux System Getting Started Tutorial: How to update outdated version of Ubuntu (Linux)
- How to view the Linux graphics hardware information (Linux)
- Linux System Getting Started Tutorial: mounted directly in Linux LVM partition (Linux)
- RedHat Linux 6.4 install Oracle 10g error (Database)
- Linux kernel socket protocol stack routing lookup cache mechanism (Linux)
- When Linux Detailed time zone and common function of time (Linux)
- Summary of Docker mounted directory (Server)
- Linux kernel VLAN study notes (Programming)
- Installation CD audio file extraction tool Flacon (Linux)
- Let Mac OS X dedicated high-speed mobile hard disk can also be read in Linux (Linux)
- Puppet 3.5 Source package Installation and Configuration (Server)
- Linux shell in back quotes (`) Application (Linux)
- Shell scripts get a snapshot of the page and generates thumbnails (Linux)
- 10 Best Swift Tutorial examples (Programming)
- MariaDB phpMyAdmin installation and configuration issues to resolve under CentOS7 (Database)
- Linux administrator should command: sed and awk (Linux)
- You may not know the Linux command-line network monitoring tool (Linux)
- init level settings for Linux / etc / inittab file into six (restart) or does not support the level of solution (Linux)
- Oracle 10046 Event (Database)
- Mass data storage application of MongoDB database (Database)
  CopyRight 2002-2020 newfreesoft.com, All Rights Reserved.