asp. net Detailed explanation of the method of obtaining the current time of the system

  • 2021-10-25 07:43:04
  • OfStack

In this paper, the method of obtaining the current time of the system by asp. net is described as an example. Share it for your reference, as follows:

In c #/ASP. net we can get the current time by using the class DataTime. 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 
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(); // 2008 Year 9 Month 4 Day 
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
// Acquisition 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
// Others 
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(); // Acquisition week  Thursday
DateTime.Now.DayOfYear.ToString(); // Get the day  248
DateTime.Now.Hour.ToString(); // Acquisition hour  20
DateTime.Now.Minute.ToString(); // Get minutes  31
DateTime.Now.Second.ToString(); // Get seconds  45
//n For 1 Number , You can count integers , You can also do decimal things 
dt.AddYears(n).ToString();  // Time plus n Year 
dt.AddDays(n).ToString();  // Plus n Days 
dt.AddHours(n).ToString();  // Plus n Hours 
dt.AddMonths(n).ToString();  // Plus n Months 
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 the date and get the format as 2005-02-18


DateTime.Now.ToString("hh:mm:ss");

Take the time and 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

(:) Time separator. In some locales, you can use other characters to represent time separators. Time separators separate hours, minutes, and seconds when formatting time values. The actual characters used as time separators in the formatted output are determined by the system's LocaleID value.
(/) Date separator. In some locales, you can use other characters to represent date separators. The date separator separates the day, month, and year when formatting a 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 letter follows, 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 the following.

d displays the day as a number without a leading zero (such as 1). If this is the only 1 character in a user-defined number format, use% d.
dd displays the day as a number with a leading zero (such as 01).
ddd displays days as abbreviations (for example, Sun).
dddd displays the day as its full name (for example, Sunday).
M displays the month as a number without a leading zero (for example, January is represented as 1). If this is the only 1 character in a user-defined number format, use% M.
MM displays the month as a number with a leading zero (for example, 01/12/01).
MMM displays months as abbreviations (for example, Jan).
MMMM displays the month as the full month name (for example, January).
gg displays time/era strings (for example, A. D.)
h uses a 12-hour scale to display hours as numbers without leading zeros (for example, 1:15:15 PM). If this is the only 1 character in a user-defined number format, use% h.
hh displays hours as numbers with leading zeros using a 12-hour scale (for example, 01:15:15 PM).
H uses a 24-hour scale to display hours as numbers without leading zeros (for example, 1:15:15). If this is the only 1 character in a user-defined number format, use% H.
HH uses a 24-hour scale to display hours as numbers with leading zeros (for example, 01:15:15).
m displays minutes as numbers without leading zeros (for example, 12: 1:15). If this is the only 1 character in a user-defined number format, use% m.
mm displays minutes as numbers with leading zeros (for example, 12:01:15).
s displays the second as a number without a leading zero (for example, 12:15: 5). If this is the only 1 character in a user-defined number format, use% s.
ss displays the second as a number with a leading zero (for example, 12:15:05).
f displays the fractional part of the second. For example, ff will display exactly to 1 hundredth of a second, while ffff will display exactly to 1 hundredth of a second. Up to 7 f symbols can be used in the user-defined format. If this is the only 1 character in a user-defined number format, use% f.
t uses a 12-hour scale and displays A in capital for any hour before noon and P in capital for any hour between noon and 11:59 P. M. If this is the only 1 character in a user-defined number format, use% t.
tt uses a 12-hour system and displays AM in capital for any hour before noon; Displays PM in capital for any hour between noon and 11:59 P. M.
y displays the year (0-9) as a number without leading zeros. If this is the only 1 character in a user-defined number format, use% y.
yy displays the year in two-digit format with leading zeros, if applicable.
yyy displays the year in 3-digit format.
yyyy displays the year in 4-digit format.
z displays a time zone offset (e.g.-8) without a leading zero. If this is the only 1 character in a user-defined number format, use% z.
zz displays the time zone offset with leading zeros (for example-08)
zzz displays the full time zone offset (for example-08:00)

For more readers interested in C # related content, please check the topics on this site: "Summary of C # Operating Excel", "Summary of XML File Operating Skills in C #", "Tutorial on Common Control Usage of C #", "Tutorial on Control Usage of WinForm #", "Tutorial on Data Structure and Algorithm of C #", "Introduction to C # Object-Oriented Programming" and "Summary of Thread Usage Skills of C # Programming"

I hope this article is helpful to everyone's C # programming.


Related articles: