且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

从服务器获取时间和日期!!!(实时)

更新时间:2023-02-26 17:56:42

DateTime today = DateTime.Now;

这将获取当前日期和时间放入变量today中-然后可以使用所需的部分:
today.Day是一个月中的某天,
today.Month是一年中的月份,
today.Hour是一天中的小时,
today.Minute是小时中的分钟.

可能是您希望将其转换为字符串以显示,所以请使用ToString方法:

This gets the current date and time into the variable today - you can then use what parts of it you need:
today.Day is the day of the month,
today.Month is the month of the year,
today.Hour is the hour of the day,
today.Minute is the minute of the hour.

Probably, you want to convert this to a string for display, so use the ToString method:

string timeIn12HourClock = today.ToString("hh:mm");
string timeIn24HourClock = today.ToString("HH:mm");



您可以使用或构造许多其他格式:格式化a显示的日期时间-格式字符串说明 [



There are load of other formats you can use or construct: Formatting a DateTime for display - format string description[^]