C#/javascript/sqlserver a summary of the operation of the date time

Source: Internet
Author: User
Tags getdate

foreword: For c#/javascript/sqlserver commonly used to date time operation function pumping time to do a collation, the network has many, but many are not all, these are the time date commonly used some operation!

First, C # Common date-time operations

//Get date + time
DateTime.Now.ToString (); 2008-9-4 20:02:10
DateTime.Now.ToLocalTime ().        ToString (); 2008-9-4 20:12:12
DateTime.Now.ToString ("Yyyy-mm-dd HH:mm:ss") 24-hour system
DateTime.Now.ToString ("Yyyy-mm-dd hh:mm:ss") 12-hour system
//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
N is a number, can be a number of integers, can also matter a decimal
DateTime.Now.AddYears (n).   ToString (); Time Plus n years
DateTime.Now.AddDays (n).   ToString (); Plus n days
DateTime.Now.AddHours (n).   ToString (); Add N Hours
DateTime.Now.AddMonths (n).   ToString (); Add n months
DateTime.Now.AddSeconds (n).   ToString (); Plus n seconds
DateTime.Now.AddMinutes (n).   ToString (); Add n Points

DateTime Lastmonth = DateTime.Now.AddMonths (-1);
String lastmonth_firstday = Lastmonth.adddays (1-lastmonth.day). ToString ("Yyyy-mm-dd");//Get the first day of the month
String lastmonth_lastday = Lastmonth.adddays (1-lastmonth.day). AddMonths (1). AddDays (-1). ToString ("Yyyy-mm-dd"); Get the last day of last month

DateTime startweek = dt. AddDays (1-convert.toint32 (dt.  Dayofweek.tostring ("D"))); This Monday
DateTime Endweek = startweek.adddays (6); This Sunday

DateTime startmonth = dt. AddDays (1-DT.  Day); Early this month
DateTime endmonth = startmonth.addmonths (1).  AddDays (-1); Month End
DateTime endmonth = startmonth.adddays (dt. AddMonths (1)-DT).  DAYS-1); Month End

DateTime startquarter = dt. AddMonths (0-(dt. MONTH-1)% 3). AddDays (1-DT.  Day); At the beginning of the quarter
DateTime Endquarter = startquarter.addmonths (3).  AddDays (-1); At the end of the quarter

DateTime startyear = new datetime (dt.  Year, 1, 1); Early this year
DateTime endyear = new datetime (dt.  Year, 12, 31); End of year

Second, JavaScript common date time operation

var mydate = new Date (); Get Current date
Mydate.getyear (); Get Current year (2-bit)
Mydate.getfullyear (); Get the full year (4-bit, 1970)
Mydate.getmonth (); Get the current month (0-11, 0 for January)
Mydate.getdate (); Get current day (1-31)
Mydate.getday (); Get Current week x (0-6, 0 for Sunday)
Mydate.gettime (); Gets the current time (the number of milliseconds since 1970.1.1)
Mydate.gethours (); Get current number of hours (0-23)
Mydate.getminutes (); Gets the current number of minutes (0-59)
Mydate.getseconds (); Gets the current number of seconds (0-59)
Mydate.getmilliseconds (); Gets the current number of milliseconds (0-999)
Mydate.tolocaledatestring (); Get Current date
var mytime=mydate.tolocaletimestring (); Get current time
Mydate.tolocalestring (); Get Date and time

Third, SQL Server date time operation

1. Get the current time

Select GETDATE ()

2, intercept the required value

Select DATEPART (year,getdate ())

Select DATEPART (month,getdate ())

Select DATEPART (day,getdate ())

Select DATEPART (hour,getdate ())

Select DATEPART (minute,getdate ())

Select DATEPART (second,getdate ())

Select DATEPART (week,getdate ())

3. Add or subtract a specified time interval from a date

Select DATEADD (Year,3,getdate ())--Get the current time and defer three years in the future

Select DATEADD (Month,3,getdate ())--Get the current time and defer three months later

Select DATEADD (Day,3,getdate ())--Get the current time and defer three days in the future

Select DATEADD (Hour,3,getdate ())--Get the current time and defer three hours later

Select DATEADD (Minute,3,getdate ())--Get the current time and defer three minutes later

Select DATEADD (Second,3,getdate ())--Get the current time and defer three seconds later

4. Returns the time between two dates

Select DateDiff (year, ' 2001-08-19 ', GETDATE ())--2001-08-19 and current time difference between the young

Number of months between select DateDiff (month, ' 2001-08-19 ', GETDATE ())--2001-08-19 and current time

Select DateDiff (Day, ' 2001-08-19 ', GETDATE ()) How many days difference between--2001-08-19 and current time

5. Display Date/time in different format

Select CONVERT (Char,getdate (), 8)--show current time-minute-second

Select CONVERT (Char,getdate (), 10)--show current month-day-year, display form "08-19-11"

Select CONVERT (Char,getdate (), 11)--show current year-month-day, display form "11/08/19"

Select CONVERT (Char,getdate (), 14)--show current time-minutes-seconds-milliseconds, display form "14:54:57:090"

6. Other

SELECT CONVERT (Datetime,convert (char (8), GETDATE (), 120) + ' 1 ')--the first day of the month

Select DATEADD (D,-day (getdate ()), DateAdd (M,1,getdate ()))--the last day of the month

SELECT DATEADD (Mm,datediff (Mm,0,dateadd (month,-1,getdate)), 0)--first day of last month

Select DATEADD (Ms,-3,dateadd (Mm,datediff (Mm,0,getdate ()), 0)--last day of last month

Select DATEADD (Ss,-1,dateadd (day,1,convert (varchar), GETDATE (), 102))--Get the last moment of the day

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.