This example describes how asp.net gets the current time of the system. Share to everyone for your reference, specific as follows:
In C #/asp.net we can get the current time by using the Datatime class. We can get different time by calling various methods in the class: Date (2008-09-04), Time (12:12:12), date + time (2008-09-04 12:11:10), and so on.
Get Date + time DateTime.Now.ToString (); 2008-9-4 20:02:10 DateTime.Now.ToLocalTime (). ToString (); 2008-9-4 20:12:12//Get Date DateTime.Now.ToLongDateString (). ToString (); September 4, 2008 DateTime.Now.ToShortDateString (). ToString (); 2008-9-4 DateTime.Now.ToString ("Yyyy-mm-dd"); 2008-09-04 DateTime.Now.Date.ToString (); 2008-9-4 0:00:00//Get Time DateTime.Now.ToLongTimeString (). ToString (); 20:16:16 DateTime.Now.ToShortTimeString (). ToString (); 20:16 DateTime.Now.ToString ("Hh:mm:ss"); 08:05:57 DateTime.Now.TimeOfDay.ToString (); 20:33:50.7187500//Other Datetime.tofiletime (). ToString (); 128650040212500000 DATETIME.NOW.TOFILETIMEUTC (). ToString (); 128650040772968750 DateTime.Now.ToOADate (). ToString (); 39695.8461709606 DateTime.Now.ToUniversalTime (). ToString (); 2008-9-4 12:19:14 DateTime.Now.Year.ToString (); Get year 2008 DateTime.Now.Month.ToString (); Get month 9 DateTime.Now.DayOfWeek.ToString (); Get Week Thursday DateTime.Now.DayOfYear.ToString (); Get the first days 248 DateTime.Now.Hour.ToString (); Get DateTime.Now.Minute.ToString hours (); Get minutes DateTime.Now.Second.ToString (); Gets the number of seconds//n is a number, can count integers, can also matter decimal dt. Addyears (n). ToString (); Time plus n years dt. AddDays (n). ToString (); Plus n days dt. AddHours (n). ToString (); Plus n-hour dt. Addmonths (n). ToString (); Plus n a month dt. AddSeconds (n). ToString (); Plus n seconds dt. AddMinutes (n). ToString ();
Plus n points
1.
String strtime = DateTime.Now.ToLongTimeString ();
2.
DateTime dt = DateTime.Now;
String str = dt. ToString ("Yyyy-mm-dd");
Or:
String str = DateTime.Now.ToString ("Yyyy-mm-dd");
Or
String str = DateTime.Now.ToShortDateString ();
3.
DateTime.Now.ToString ("Yyyy-mm-dd");
Take a date and get a format like 2005-02-18
DateTime.Now.ToString ("Hh:mm:ss");
Take time to get a format like 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
(:) The time separator. In some locales, you can use other characters to represent the time separator. The time separator separates hours, minutes, and seconds when formatting time values. The actual character used as the time separator in the formatted output is determined by the LocaleID value of the system.
(/) Date separator. In some locales, you can use other characters to represent a date separator. The date separator separates day, month, and year when formatting date values. The actual characters used as date separators in the formatted output are determined by your locale settings.
(%) Used to indicate that characters should be read in one-letter format, regardless of the letter followed. Also used to indicate that a single letter format should be read in a user-defined format. For more information, see the following section.
D Displays the day as a number with no leading zeros (for example, 1). If this is the only character in the user-defined number format, use%d.
DD Displays the day as a number with a leading zero (for example, 01).
DDD Displays the day as an abbreviated form (for example, Sun).
DDDD Displays the day as a full name (for example, Sunday).
M Displays the month as a number without a leading zero (as indicated in January as 1). If this is the only character in the user-defined number format, use%m.
MM Displays the month as a number with a leading zero (for example, 01/12/01).
MMM Displays the month as an abbreviated form, such as a few.
MMMM Displays the month as the full month name (for example, January).
GG Displays the era/era string (for example, A.D.)
H displays the hour as a number with no leading zeros (for example, 1:15:15 PM) using a 12-hour system. If this is the only character in the user-defined number format, use%h.
HH uses a 12-hour system to display hours as numbers with leading zeros (for example, 01:15:15 PM).
H displays the hour as a number with no leading zeros (for example, 1:15:15) using a 24-hour system. If this is the only character in the user-defined number format, use%H.
HH uses a 24-hour system to display hours as a number with a leading zero (for example, 01:15:15).
M displays the minute as a number without a leading zero (for example, 12:1:15). If this is the only character in the user-defined number format, use%m.
MM Displays the minute as a number with a leading zero (for example, 12:01:15).
s displays the second as a number with no leading zeros (for example, 12:15:5). If this is the only character in the user-defined number format, use%s.
SS Displays the second as a number with a leading zero (for example, 12:15:05).
The f displays the fraction of the second part. For example, FF will display exactly 1% seconds, and FFFF will be displayed exactly to one out of 10,000 seconds. You can use up to seven F symbols in a user-defined format. If this is the only character in the user-defined number format, use%f.
T uses a 12-hour system and displays an uppercase a for any hour before noon, with an uppercase P for any hour between noon and 11:59 p.m. If this is the only character in the user-defined number format, use%t.
TT uses a 12-hour system and displays an uppercase AM for any hour before noon, and an uppercase PM for any hour between noon and 11:59 p.m.
Y Displays the year (0-9) as a number with no leading zeros. If this is the only character in the user-defined number format, use%y.
YY Displays the year in two-digit format with a leading zero, if applicable.
YYY Displays the year in three-digit number format.
YYYY displays the year in four-digit number format.
Z shows the time zone offset (for example, 8) without a leading zero. If this is the only character in the user-defined number format, use%z.
ZZ shows the time zone offset with a leading zero (for example-08)
ZZZ Displays the full time zone offset (for example, -08:00)
Read more about C # Interested readers can view the site topics: "C # Operations Excel Skills Summary", "C # XML file Operation Tips Summary", "C # Common control usage Tutorial", "WinForm Control Usage Summary", "C # Data structure and algorithm tutorial", "C # An introductory course on object-oriented programming and a summary of thread usage tips for C # programming
I hope this article will help you with C # programming.