DateTime operation, time range, plus and minus

Source: Internet
Author: User
Tags add hours

The DB has a time in the array of char types, for example 20151111 12171220000, which now needs to be reduced by 5s, forming a time range and then writing the stored procedure. the idea is to convert the time to datetime first, and then the DateTime itself has a way to add and subtract. There are corresponding methods when SQL is done.My solution ended up with:DateTime dt = datetime.parseexact ("20151111 12171220000". substring (0,15), "YYYYMMDDHHMMSS", null);or:DateTime.Parse (str. Substring (0, 4) + "-" + str. Substring (4, 2) + "-" + str. Substring (6, 2))discover a simple and understandable blog post:http://www.cnblogs.com/Pickuper/articles/2058880.html (sticky, easy to see)

Method One:convert.todatetime (String)

The string format is required and must be YYYY-MM-DD HH:MM:SS

================================================

Method Two: Convert.todatetime (String, IFormatProvider)

DateTime DT;

DateTimeFormatInfo Dtformat = new System.globalizationdatetimeformatinfo ();

Dtformat.shortdatepattern = "YYYY/MM/DD";

DT = Convert.todatetime ("2011/05/26", Dtformat);

================================================

Method Two:datetime.parseexact ()

String datestring = "20110526";

DateTime dt = DateTime.ParseExact (datestring, "YyyyMMdd", System. Globalization. CultureInfo. CurrentCulture);

Or

DateTime dt = DateTime.ParseExact (datestring, "YyyyMMdd", System. Globalization. CultureInfo. InvariantCulture);

Reference information:

CultureInfo CultureInfo = cultureinfo.createspecificculture ("en-us"); string format = "ddd MMM d HH:mm:ss zz00 yyyy"; strin G stringvalue = DateTime.Now.ToString (format, cultureInfo); Get date string datetime datetime = DateTime.ParseExact ("Wed 16:28:03 +0800", format, cultureInfo); Convert a string into a date
Other

Date format: YyyyMMdd HH:mm:ss (note the letter case of this string is strict)
YYYY: Year of representation
MM: On behalf of the month
DD: Representative Day
HH: Representative hour (24-hour system)
MM: Represents minutes
SS: Represents seconds
DateTime.Now.ToShortTimeString ()
DateTime dt = DateTime.Now;
Dt. ToString ();//2005-11-5 13:21:25
Dt. Tofiletime (). ToString ();//127756416859912816
Dt. TOFILETIMEUTC (). ToString ();//127756704859912816
Dt. ToLocalTime (). ToString ();//2005-11-5 21:21:25
Dt. Tolongdatestring (). ToString ();//November 5, 2005
Dt. Tolongtimestring (). ToString ();//13:21:25
Dt. ToOADate (). ToString ();//38661.5565508218
Dt. ToShortDateString (). ToString ();//2005-11-5
Dt. Toshorttimestring (). ToString ();//13:21
Dt. ToUniversalTime (). ToString ();//2005-11-5 5:21:25
Dt. Year.tostring ();//2005
Dt. Date.tostring ();//2005-11-5 0:00:00
Dt. Dayofweek.tostring ();//saturday
Dt. Dayofyear.tostring ();//309
Dt. Hour.tostring ();//13
Dt.Millisecond.ToString ();//441
Dt. Minute.tostring ();//30
Dt. Month.tostring ();//11
Dt. Second.tostring ();//28
Dt. Ticks.tostring ();//632667942284412864
Dt. Timeofday.tostring ();//13:30:28.4412864
Dt. ToString ();//2005-11-5 13:47:04
Dt. AddYears (1). ToString ();//2006-11-5 13:47:04
Dt. AddDays (1.1). ToString ();//2005-11-6 16:11:04
Dt. AddHours (1.1). ToString ();//2005-11-5 14:53:04
Dt. Addmilliseconds (1.1). ToString ();//2005-11-5 13:47:04
Dt. AddMonths (1). ToString ();//2005-12-5 13:47:04
Dt. AddSeconds (1.1). ToString ();//2005-11-5 13:47:05
Dt. AddMinutes (1.1). ToString ();//2005-11-5 13:48:10
Dt. Addticks (1000). ToString ();//2005-11-5 13:47:04
Dt.compareto (DT). ToString ();//0
Dt. ADD (?). ToString ();//question mark is a time period
Dt. Equals ("2005-11-6 16:11:04"). ToString ();//false
Dt. Equals (DT). ToString ();//true
Dt. GetHashCode (). ToString ();//1474088234
Dt. GetType (). ToString ();//system.datetime
Dt. GetTypeCode (). ToString ();//datetime

Dt. Getdatetimeformats (' s ') [0]. ToString ();//2005-11-05t14:06:25
Dt. Getdatetimeformats (' t ') [0]. ToString ();//14:06
Dt. Getdatetimeformats (' y ') [0]. ToString ();//November 2005
Dt. Getdatetimeformats (' D ') [0]. ToString ();//November 5, 2005
Dt. Getdatetimeformats (' D ') [1]. ToString ();//2005 11 05
Dt. Getdatetimeformats (' D ') [2]. ToString ();//Saturday 2005 11 05
Dt. Getdatetimeformats (' D ') [3]. ToString ();//Saturday November 5, 2005
Dt. Getdatetimeformats (' M ') [0]. ToString ();//November 5
Dt. Getdatetimeformats (' f ') [0]. ToString ();//November 5, 2005 14:06
Dt. Getdatetimeformats (' g ') [0]. ToString ();//2005-11-5 14:06
Dt. Getdatetimeformats (' R ') [0]. ToString ();//sat, 2005 14:06:25 GMT
String. Format ("{0:d}", DT);//2005-11-5
String. Format ("{0}", dt);//November 5, 2005
String. Format ("{0:f}", dt);//November 5, 2005 14:23
String. Format ("{0:f}", dt);//November 5, 2005 14:23:23
String. Format ("{0:g}", DT);//2005-11-5 14:23
String. Format ("{0:g}", DT);//2005-11-5 14:23:23
String. Format ("{0:m}", dt);//November 5
String. Format ("{0:r}", DT);//sat, 2005 14:23:23 GMT
String. Format ("{0:s}", DT);//2005-11-05t14:23:23
String. Format ("{0:t}", DT);//14:23
String. Format ("{0:t}", DT);//14:23:23
String. Format ("{0:u}", DT);//2005-11-05 14:23:23z
String. Format ("{0:u}", dt);//November 5, 2005 6:23:23
String. Format ("{0:y}", dt);//November 2005
String. Format ("{0}", dt);//2005-11-5 14:23:23
String. Format ("{0:yyyymmddhhmmssffff}", DT);
Calculates the difference in the number of days between 2 dates
-----------------------------------------------
DateTime dt1 = Convert.datetime ("2007-8-1");
DateTime DT2 = Convert.datetime ("2007-8-15");
TimeSpan span = dt2. Subtract (DT1);
int Daydiff = span. Days + 1;
Calculate the number of days of the month in a year
-----------------------------------------------
int days = Datetime.daysinmonth (2007, 8);
days = 31;
Add one day to date, one day less
-----------------------------------------------
DateTime DT =datetime.now;
Dt. AddDays (1); Add one day
Dt. AddDays (-1);//Decrease One day
Other years method similar ...
Convert Date function in Oracle sql
-----------------------------------------------
To_date ("2007-6-6", "yyyy-mm-dd");
To_date ("2007/6/6", "yyyy/mm/dd");

Select Sysdate,to_char (sysdate+1/24/60/60, ' Yyyy-mm-dd hh24:mi: SS ') from dual; --plus 1 seconds Select Sysdate,to_char (sysdate-1/24/60/60, ' Yyyy-mm-dd hh24:mi: SS ') from dual; -Minus 1 seconds Select To_date (' 2004-05-07 13:23:44 ', ' Yyyy-mm-dd hh24:mi: ss ') from dual//

Both date and timestamp can be added and reduced by SQL.
There are three ways to manipulate different time types for the current date Gagnin, month, day, hour, minute, and second:

1 add hours, minutes and seconds using built-in functions Numtodsinterval
2 plus a simple number to increase the day
3 use built-in function add_months to increase year and month

Cases:
Add one hours to the current date:
Sql> Select Sysdate, Sysdate+numtodsinterval (1, ' Hour ') from dual;

Sysdate Sysdate+numtodsinte
——————- ——————-
2010-10-14 21:38:19 2010-10-14 22:38:19
Add 50 minutes to the current date
Sql> Select Sysdate, Sysdate+numtodsinterval (' minute ') from dual;

2010-10-14 21:39:12 2010-10-14 22:29:12
Add 45 seconds to the current date
Sql> Select Sysdate, Sysdate+numtodsinterval ("second") from dual;

2010-10-14 21:40:06 2010-10-14 21:40:51
Add 3 days to the current date
Sql> Select Sysdate, sysdate+3 from dual;

2010-10-14 21:40:46 2010-10-17 21:40:46
Add 4 months to the current date
Sql> Select Sysdate, Add_months (sysdate,4) from dual;

2010-10-14 21:41:43 2011-02-14 21:41:43

2 year increase in current date
Sql> Select Sysdate, Add_months (sysdate,12*2) from dual;

2010-10-14 21:42:17 2012-10-14 21:42:17

DateTime operation, time range, plus and minus

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.