C # get current date Time

Source: Internet
Author: User
Tags getdate time and date
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 day    //248
DateTime.Now.Hour.ToString ();            Get Hours    //
DateTime.Now.Minute.ToString ();      Get minutes    //
DateTime.Now.Second.ToString ();      gets the number of seconds    / /a

N is a number, you can count integers, you can do decimal
Dt. Addyears (n).   ToString (); Time Plus n years
Dt. AddDays (n).   ToString (); Plus n days
Dt. AddHours (n).   ToString (); Plus n Hours
Dt. Addmonths (n).   ToString (); Add N a month
Dt. AddSeconds (n).   ToString (); Plus n seconds
Dt. AddMinutes (n).   ToString (); Plus n points


function of time and date used by SQL statements


GETDATE (): Get System Current time
DATEADD (datepart,number,date): Calculates a new time value after a time based on a time, such as: DateAdd (Yy,30,getdate ())
DateDiff (datepart,startdate,enddate): Calculates the difference of two times, for example: DateDiff (Yy,getdate (), ' 2008-08-08 ')
Dataname (datepart,date): Gets the value of the different parts of time, the return value is a string
DatePart (datepart,date): Similar to Datename, except that the return value is an integral type
Day (date): Gets the number of days specified
Month (date): Gets the month of the specified time
Year (date): Gets the years of the specified time

Select year (GETDATE ()): Current Years

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

Related Article

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.