开发者

i have a date (eg: 2010-04-17 ) i need the date of after 20 days from this date

i have a date (eg: 2010-04-17 ) i need the开发者_如何学运维 date of after 20 days from this date How to get the date after 20 days i.e next month some date.

either in sql or in c#


This is quite simple in C#

            DateTime date = new DateTime(2010, 04, 17);
            DateTime newDate = date.AddDays(20);

You can construct the original date variable in whatever way is easiest to you, then use the AddDays method to create a new variable (or update the existing one) with the date any number of days afterwards.


In C# you use the AddDays method:

DateTime someDate = new DateTime(2010, 4, 17);
DateTime later = someDate.AddDays(20);

In SQL you would use some date manipulation function, which is specific to the different dialects of SQL. In MS SQL Server for example you would use the dateadd function:

dateadd(day, 20, someDate)


If the date is already a DateTime object, then you can call

var nextDate = myDate.AddDays(20);

If it's a string then you will need to firt convert it to a DateTime:

var myDate = DateTime.Parse("2010-04-17");
var nextDate = myDate.AddDays(20);

Note that the AddDays method returns a new DateTime, it doesn't add days to the original DateTime.


T-SQL: DATEADD(DAY, 20, thedate)

C#: DateTime.Add()


Oracle:

SELECT DATE_COLUMN + INTERVAL '20' DAY FROM MY_TABLE;

or

SELECT DATE_COLUMN + 20 FROM MY_TABLE;

PL/SQL:

BEGIN
  dtMy_date  DATE;

  SELECT DATE_COLUMN INTO dtMy_date FROM MY_TABLE;

  dtMy_date := dtMy_date + INTERVAL '20' DAY;
  -- or
  dtMy_date := dtMy_date + 20;
END;

Share and enjoy.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜