This article describes the method of obtaining the current time of the system. Share it for your reference, as follows:
In c# / we can get the current time by using the DataTime class. By calling various methods in the class, we can get different times: such as: date (2008-09-04), time (12:12:12), date + time (2008-09-04 12:11:10), etc.
//Get date + time(); // 2008-9-4 20:02:10 ().ToString(); // 2008-9-4 20:12:12 //Get date().ToString(); // September 4, 2008().ToString(); // 2008-9-4 ("yyyy-MM-dd"); // 2008-09-04 (); // 2008-9-4 0:00:00 //Get time().ToString(); // 20:16:16 ().ToString(); // 20:16 ("hh:mm:ss"); // 08:05:57 (); // 20:33:50.7187500 //other().ToString(); // 128650040212500000 ().ToString(); // 128650040772968750 ().ToString(); // 39695.8461709606 ().ToString(); // 2008-9-4 12:19:14 (); //Get year 2008(); //Get month 9(); //Get the week Thursday(); //How long does it take to get it? 248(); //Get hours 20(); //Get minutes 31(); //Get seconds 45//n is a number, you can count integers or decimals(n).ToString(); //Time plus n years(n).ToString(); //Add n days(n).ToString(); //Add n hours(n).ToString(); //Add n months(n).ToString(); //Add n seconds(n).ToString(); //Add n points
1.
string strTime = ();
2.
DateTime dt = ; String str = ("yyyy-MM-dd");
or:
string str = ("yyyy-MM-dd");
or
string str = ();
3.
("yyyy-MM-dd");
Take the date and get the format as in 2005-02-18
("hh:mm:ss");
Take the time and get the format as 10:45:30
Format Display
M/d/yy 12/7/58
d-MMM 7-Dec
d-MMMM-yy 7-December-58
d MMMM 7 December
MMMM yy December 58
hh:mm tt 08:50 PM
h:mm:ss t 8:50:35 P
H:mm 20:50
H:mm:ss 20:50:35
M/d/yyyy H:mm 12/7/1958 20:50
Character Description
(:) Time separator. In some locales, other characters can be used to represent the time separator. The time separator separates hours, minutes, and seconds when formatting the time value. The actual characters used as time separators in the formatted output are determined by the system's LocaleID value.
(/) Date separator. In some locales, other characters can be used to represent date separators. The date separator separates the day, month, and year when formatting the date value. The actual characters used as date separators in the formatted output are determined by your locale.
(%) is used to indicate that no matter what letters are trailing, the subsequent characters should be read in single letter format. Also used to indicate that the single letter format should be read in a user-defined format. For more information, see below.
d Displays day as a number without leading zeros (such as 1). If this is a unique character in a user-defined numeric format, use %d.
dd Displays day as a number with leading zeros (such as 01).
ddd displays the day as an abbreviation (for example, Sun).
dddd Displays the day as its full name (for example Sunday).
M Displays the month as a number without leading zeros (as expressed by January as 1). If this is a unique character in a user-defined numeric format, use %M.
MM displays the month as a number with leading zeros (for example, 01/12/01).
MMM displays the month as an abbreviation (for example, Jan).
MMMM displays the month as the full month name (for example, January).
gg displays epoch/epoch strings (e.g. .)
h Use a 12-hour clock to display the hours as a number without leading zeros (for example, 1:15:15 PM). If this is a unique character in a user-defined numeric format, use %h.
hh Displays the hours as a number with leading zeros using a 12-hour clock (for example, 01:15:15 PM).
H Displays the hours as a number without leading zeros using a 24-hour clock (for example, 1:15:15). If this is a unique character in a user-defined numeric format, use %H.
HH Displays the hours as a number with leading zeros using a 24-hour clock (for example, 01:15:15).
m Displays minutes as numbers without leading zeros (for example, 12:1:15). If this is a unique character in a user-defined numeric format, use %m.
mm Displays minutes as a number with leading zeros (for example, 12:01:15).
s displays seconds as a number without leading zeros (for example, 12:15:5). If this is a unique character in a user-defined numeric format, use %s.
ss displays seconds as a number with leading zeros (for example, 12:15:05).
f Displays the fractional part of the second. For example, ff will be displayed exactly to one hundredth of a second, while ffff will be displayed exactly to one ten thousandth of a second. Up to seven f symbols can be used in user-defined formats. If this is a unique character in a user-defined numeric format, use %f.
t Use a 12-hour system and display capital A for any hour before noon and capital P for any hour between noon and 11:59. If this is a unique character in a user-defined numeric format, use %t.
tt uses a 12-hour system and displays capital AM for any hour before noon; capital PM for any hour between noon and 11:59.
y Displays year (0-9) as a number without leading zeros. If this is a unique character in a user-defined numeric format, use %y.
yy Displays the year in two-digit format with leading zeros (if applicable).
yyy Displays the year in three-digit format.
yyyy Displays the year in four digit format.
z Displays the time zone offset without leading zeros (such as -8). If this is a unique character in a user-defined numeric format, use %z.
zz Displays the time zone offset with leading zeros (for example -08)
zzz Shows the full time zone offset (for example -08:00)
For more information about C# related content, please check out the topic of this site:Summary of C# operation skills》、《Summary of XML file operation skills in C#》、《Tutorial on the usage of common C# controls》、《Summary of WinForm control usage》、《C# data structure and algorithm tutorial》、《Introduction to C# object-oriented programming tutorial"and"Summary of thread usage techniques for C# programming》
I hope this article will be helpful to everyone's C# programming.