C # Current system time acquisition and time format detailed _c# tutorial

Source: Internet
Author: User
Tags datetime iso 8601 local time

C # Current system time acquisition and time format

Recently learned C # knowledge, to get the system time and time format is summarized, this article in the online collation of detailed information, we look down!

--datetime Digital Type System.DateTime currenttime=new System.DateTime ();  
Take the current month and day time currenttime=system.datetime.now seconds;  
Take the current year int year =currenttime.year;  
Take the current month int month =currenttime.month;  
Take the current day int day =currenttime.day;  
=currenttime.hour when the current time int is taken;  
Taking the current sub-int =currenttime.minute;  
takes the current second int second =currenttime.second; takes the current millisecond int millisecond =currenttime.millisecond; (variable can be used in Chinese) to take the Chinese date display--month and day time string stry=currenttime.tostring ("F"); 
Do not display seconds to the Chinese date display _ Month string strym=currenttime.tostring ("Y"); 
Take Chinese date to display _ Month Day string strmd=currenttime.tostring ("M"); 
Take the current month day, the format is: 2003-9-23 string strymd=currenttime.tostring ("D"); 
Taking the current time, the format is: 14:24 string strt=currenttime.tostring ("T"); 
DateTime.Now.ToString ()///Get the current system time complete date and time DateTime.Now.ToLongDateString ()//Show only date xxxx xx month xx days, one is a long date 
DateTime.Now.ToShortDateString ()///Only display date xxxx-xx-xx one is a short date//Today DateTime.Now.Date.ToShortDateString (); Yesterday's DateTime.Now.AddDays (-1). 
ToShortDateString (); Tomorrow's DateTime.Now.AddDays (1). 
 
ToShortDateString (); This week (noteEvery week here is from Sunday to Saturday DateTime.Now.AddDays (convert.todouble (0-convert.toint16 (DateTime.Now.DayOfWeek))). 
ToShortDateString (); DateTime.Now.AddDays (Convert.todouble (6-convert.toint16 (DateTime.Now.DayOfWeek))). 
ToShortDateString (); Last week, this week was minus 7 days DateTime.Now.AddDays (Convert.todouble (0-convert.toint16 (DateTime.Now.DayOfWeek))-7). 
ToShortDateString (); DateTime.Now.AddDays (Convert.todouble (6-convert.toint16 (DateTime.Now.DayOfWeek))-7). 
ToShortDateString (); Add 7 days DateTime.Now.AddDays (Convert.todouble (0-convert.toint16 (DateTime.Now.DayOfWeek)) + 7 next week. 
  ToShortDateString (); DateTime.Now.AddDays (Convert.todouble (6-convert.toint16 (DateTime.Now.DayOfWeek)) + 7). 
ToShortDateString (); 
The first day of this month is number 1th, the last day is the next month, one more day. DateTime.Now.Year.ToString () + DateTime.Now.Month.ToString () + "1"; First day DateTime.Parse (DateTime.Now.Year.ToString () + DateTime.Now.Month.ToString () + "1"). Addmonths (1). AddDays (-1).  ToShortDateString ()//Last Day Another method: DateTime now = DateTime.Now;
datetime d1 = new DateTime (now. Year, now. Month, 1); The first day of the month DateTime d2 = D1. Addmonths (1). AddDays (-1); The last day of the Month PS:DateTime.Now.DayOfWeek.ToString ()//English week display, Wednesday (int) DateTime.Now.DayOfWeek number, if Wednesday, the result corresponds to 3 Dateti Me. Now.tostring ("dddd", New System.Globalization.CultureInfo ("ZH-CN")); Chinese Week display DateTime.Now.ToString ("dddd");//Chinese Week display DateTime.Now.ToString ("Dddd,mmmm,dd, yyyy", new System.Globalization.DateTimeFormatInfo ());//Display date format friday,july, 01,2009 DateTime.Now.ToString ("Dddd,dd mmmm,yyyy"  //Output Wednesday, 301 months, 2008 Source: http://msdn.microsoft.com/zh-cn/vstudio/bb762911 (vs.95). aspx, how to: Extract the day of the week from a specific date

The datetime type is formatted in the ToString () format

Parameter format format verbose usage
Format Character Association attribute/description
D ShortDatePattern
D Longdatepattern
F Full Date and time (long date and short time)
F Fulldatetimepattern (long date and long time)
G General (short date and short time)
G General (short date and long time)
M, M Monthdaypattern
R, R Rfc1123pattern
s use local time Sortabledatetimepattern (based on ISO 8601)
T Shorttimepattern
T Longtimepattern
U universalsortabledatetimepattern format for displaying Universal Time
U use the full date and time of the universal Time (long date and long time)
Y, y Yearmonthpattern

The following table lists the patterns that can be merged to construct custom schemas. These patterns are case-sensitive

D One day of the month. The date of one number does not have a leading zero.
One day of DD month. The date of a single number has a leading zero.
The abbreviated name of a day in the DDD week, defined in Abbreviateddaynames.
dddd the full name of the day of the week, defined in DayNames.
M-month numbers. A single number of months does not have a leading zero.
MM month number. A one-digit month has a leading zero.
The abbreviated name of the MMM month, defined in AbbreviatedMonthNames.
The full name of the MMMM month, defined in MonthNames.
Y does not contain the year of the era. If the year that does not contain the era is less than 10, the year is displayed with no leading zeros.
YY does not contain years of the era. If the year that does not contain the era is less than 10, the year with leading zeros is displayed.
YYYY includes the four-digit year of the era.
GG period or ERA. If the date to be formatted does not have an associated period or era string, the pattern is ignored.
H 12 Hour system. The number of hours in a single digit does not have a leading zero.
HH 12-hour hour. The number of hours in a single digit has a leading zero.
H 24 hour system. The number of hours in a single digit does not have a leading zero.
HH 24-hour hour. The number of hours in a single digit has a leading zero.

Thank you for reading, I hope to help you, thank you for your support for this site!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.