Get today's date using DateTime.Today function in C#
Use the DateTime.Today function in C# to get today’s date, specific code examples are required
C# is an object-oriented programming language that provides many built-in Classes and methods to handle dates and times. Among them, the DateTime class has some very useful methods, such as the Today property, which can be used to obtain today's date.
The following is a sample code that demonstrates how to use the DateTime.Today function in C# to get today's date:
using System; class Program { static void Main(string[] args) { // 使用DateTime.Today获取今天的日期 DateTime today = DateTime.Today; // 输出今天的日期 Console.WriteLine("今天的日期是:" + today.ToString("yyyy年MM月dd日")); // 获取今天是星期几 string dayOfWeek = today.ToString("dddd"); Console.WriteLine("今天是星期:" + dayOfWeek); // 获取今年的第几天 int dayOfYear = today.DayOfYear; Console.WriteLine("今天是今年的第" + dayOfYear + "天"); // 判断今天是否为闰年 bool isLeapYear = DateTime.IsLeapYear(today.Year); if (isLeapYear) { Console.WriteLine("今年是闰年"); } else { Console.WriteLine("今年不是闰年"); } // 增加一天后的日期 DateTime tomorrow = today.AddDays(1); Console.WriteLine("明天的日期是:" + tomorrow.ToString("yyyy年MM月dd日")); } }
The above code first uses DateTime.Today to get today's date and save it In a variable today of DateTime type. Next, use the ToString method to format the date into a string of "yyyy year MM month dd day" and use Console.WriteLine to output it. Then, using the same method, you can get the day of the week today is, the day of the year, and determine whether this year is a leap year. Finally, you can use the AddDays method to add one day to today's date, get tomorrow's date, and output it.
Running the above code will output results similar to the following:
今天的日期是:2022年01月01日 今天是星期:星期六 今天是今年的第1天 今年不是闰年 明天的日期是:2022年01月02日
Through this example, you can learn how to use the DateTime.Today function in C# to get today's date, and some others Common operations on date and time. Hope it helps you!
The above is the detailed content of Get today's date using DateTime.Today function in C#. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Guide to Active Directory with C#. Here we discuss the introduction and how Active Directory works in C# along with the syntax and example.

Guide to the Access Modifiers in C#. We have discussed the Introduction Types of Access Modifiers in C# along with examples and outputs.

Guide to Random Number Generator in C#. Here we discuss how Random Number Generator work, concept of pseudo-random and secure numbers.

Guide to C# Data Grid View. Here we discuss the examples of how a data grid view can be loaded and exported from the SQL database or an excel file.

Guide to C# StringReader. Here we discuss a brief overview on C# StringReader and its working along with different Examples and Code.

Guide to Patterns in C#. Here we discuss the introduction and top 3 types of Patterns in C# along with its examples and code implementation.

Guide to C# Serialization. Here we discuss the introduction, steps of C# serialization object, working, and example respectively.

Guide to BinaryWriter in C#. Here we discuss syntax and explanation, how it works with examples to implement with proper codes.
