In C#, DateTime is a struct. Thus it is of value type and used to represent an instant of time. It is used to represent the date and time of the day. The value of type DateTime ranges between 12:00:00 midnight, January 1, 0001 to 11:59:59 PM, December 31, 9999 A.D.Value of DateTime cannot be null because it is a value type. To initialize a DateTime value, we can call any of the overloads of the DateTime constructor. We can also assign values returned from a property or method to a DateTime object.
Syntax:
Below is the syntax to initialize a new instance of the DateTime structure:
DateTime date_time = new DateTime();
Here, date_time is the user-defined name given to the instance of type DateTime. We have initialized this instance using the ‘new’ operator. In the above syntax, we have used an implicit parameterless constructor to initialize DateTime to its default value. We can also initialize the DateTime instance using any of the overloads of the DateTime constructor.
In C#, we can work with DateTime and assign value to a DateTime variable in several ways.
DateTime dateTime = new DateTime(2020, 2, 8, 7, 32, 56);
The above statement initializes a new instance of the DateTime structure for a particular year, month, day, hour, minute, and second.
public DateTime(int year, int month, int day, int hour, int minute, int second);
public DateTime(long ticks);
public DateTime(long ticks, DateTimeKind kind);
public DateTime(int year, int month, int day);
public DateTime(int year, int month, int day, Calendar calendar);
public DateTime(int year, int month, int day, int hour, int minute, int second, DateTimeKind kind);
public DateTime(int year, int month, int day, int hour, int minute, int second, Calendar calendar);
public DateTime(int year, int month, int day, int hour, int minute, int second, int millisecond);
public DateTime(int year, int month, int day, int hour, int minute, int second, int millisecond, DateTimeKind kind);
public DateTime(int year, int month, int day, int hour, int minute, int second, int millisecond, Calendar calendar);
public DateTime(int year, int month, int day, int hour, int minute, int second, int millisecond, Calendar calendar, DateTimeKind kind);
DateTime dateTime = DateTime.Now;
This assigns the current date and time to the DateTime variable.
string str = "6/2/2020 9:20:40 AM"; DateTime dateTime = DateTime.Parse(str, System.Globalization.CultureInfo.InvariantCulture);
We can perform the above conversion using Parse(), ParseExact(), TryParse(), and TryParseExact() methods.
Here are a few examples of how to parse a string to a DateTime object:
Example showing current date and time with tomorrow’s date and time using property and method provided by DateTime:
Code:
using System; using System.IO; namespace ConsoleApp4 { class Program { public static DateTime GetNextDay() { //getting next day using AddDays() method return DateTime.Now.AddDays(1); } public static void Main() { //displaying current date and time using 'Now' property of DateTime Console.WriteLine("Current date and time: {0}", DateTime.Now); DateTime dateTime = GetNextDay(); Console.WriteLine("Tomorrow date and time: {0}", dateTime); Console.ReadLine(); } } }
Output:
For example, taking the year as input from the user and then checking if it is a leap year or not using DateTime.IsLeapYear() method.
Code:
using System; using System.IO; namespace ConsoleApp4 { class Program { public static void Main() { try { //taking year as input from user Console.WriteLine("Please enter a year"); int year = Convert.ToInt32(Console.ReadLine()); //checking if entered year is a leap year or not //using DateTime.IsLeapYear() method Console.WriteLine("\n Using IsLeapYear() method:"); if (DateTime.IsLeapYear(year)) { Console.WriteLine(year + " is a leap year"); } else { Console.WriteLine(year + " is not a leap year"); } //checking if entered year is a leap year or not //using DateTime.DaysInMonth() method Console.WriteLine("\n Using DaysInMonth() method:"); if (DateTime.DaysInMonth(year, 2) == 29) { Console.WriteLine(year + " is a leap year"); } else { Console.WriteLine(year + " is not a leap year"); } } catch(Exception ex) { Console.WriteLine(ex.Message); } Console.ReadLine(); } } }
Output:
For example, we are getting the first and the last day of the year.
Code:
using System; using System.IO; namespace ConsoleApp4 { class Program { public static void Main() { DateTime dateTime = DateTime.Now; //displaying first day of current year DateTime firstDay = new DateTime(dateTime.Year, 1, 1); Console.WriteLine("First day of {0} is {1}", dateTime.Year, firstDay); //getting first day of next year DateTime dateTimeNext = new DateTime(dateTime.Year + 1, 1, 1); //subtracting one day from the first day of next year //to get the last day of current year DateTime lastday = dateTimeNext.AddDays(-1); Console.WriteLine("Last day of {0} is {1}", dateTime.Year, lastday); Console.ReadLine(); } } }
Output:
The dateTime structure is used to work with date and time. It is used as a data type to store date and time. DateTime provides properties and methods to work with date and time. DateTime is a structure and of a value type; it cannot be null.
The above is the detailed content of DateTime in C#. For more information, please follow other related articles on the PHP Chinese website!