C # gets the current date and time (turn)

 http://blog.163.com/ljq086@126/blog/static/549639712010112921658843/

We can get the current time by using DataTime this class. We can get different times by various method calls class: such as: date (2008-09-04), time (12:12:12), date + time (2008-09-04 12:11:10) Wait.


// get the date + time
DateTime.Now.ToString (); // 2008-9-4 20:02:10
DateTime.Now.ToLocalTime () ToString ();. // 2008-9-4 20:12:12

//获取日期
DateTime.Now.ToLongDateString().ToString();    // 2008年9月4日
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

//获取时间
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

//其他
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 (); Gets Year 2008 //
DateTime.Now.Month.ToString (); Gets March 9 //
DateTime.Now.DayOfWeek.ToString (); Gets catalog on Thursday week //
DateTime.Now.DayOfYear .ToString (); get the first 248 days //
DateTime.Now.Hour.ToString (); Gets 20 hours //
DateTime.Now.Minute.ToString (); Gets 31 minutes //
DateTime.Now.Second.ToString ( ); // get the number of seconds 45

// n is a number, the number can be an integer, decimal things can
dt.AddYears (n) .ToString (); // add n years time
dt.AddDays (n) .ToString (); // add n days
dt. AddHours (n) .ToString (); // add n h
dt.AddMonths (n) .ToString (); // add n months
dt.AddSeconds (n) .ToString (); // add n seconds
dt.AddMinutes (n) .ToString (); // add n points


SQL statement using a function of time and date


getdate (): Get the current system time
dateadd (datepart, number, date) : calculates a new time value increases a time based on a time of the post, such as: DATEADD (YY, 30, getdate ())
DATEDIFF (the datepart, StartDate , enddate): calculating the difference between two time, such as: DATEDIFF (YY, getdate (), '2008-08-08')
DataName (datepart, DATE): Get value different time portion of the return value is a string
datepart (datepart, date): and datename similar, but the return value is an integer
day (date): Gets the specified time the number of days
month (date): Gets the month specified time
year (date): Gets the specified time of the year

select year (getdate ()): current year

 

c # how to get the current time contain week, I do not want it to appear a week, as long as the date and time,

DateTime dt=DateTime.Now.Date;
MessageBox.Show(dt.ToString("yyyy-MM-dd"));
或者直接:
MessageBox.Show(DateTime.Now.ToString("yyyy-MM-dd"));
可以参考:

Guess you like

Origin blog.csdn.net/oshan2012/article/details/91950324