Questions tagged [date-arithmetic]

The term 'date-arithmetic' refers to determining via code such information as how many days, hours, etc. between two calendar dates, and determining the last day of the month, or whether a date is a weekday or a weekend. Be sure to also include the appropriate programming or database tag.

Many computer languages provide for date arithmetic, including , , and languages. Most languages store data with a type of date internally as a number of days since a certain date, such as January 1st 1970 for Unix.

Typical Considerations

  • Basic calculations such as number of days between two dates, number of months between two dates, etc.
  • Scheduling problems often involve how to determine the fifth business day of the month, the last day of the month, the fourth Thursday of the month
  • Regional or Client-specific concerns include whether certain days are holidays, etc.
  • Week number of the year (ISO: Monday as first day of week, Unix: Sunday as first day of week)
1101 questions
63
votes
8 answers

How to subtract years?

I have a date in R, e.g.: dt = as.Date('2010/03/17') I would like to subtract 2 years from this date, without worrying about leap years and such issues, getting as.Date('2008-03-17'). How would I do that?
gt6989b
  • 4,125
  • 8
  • 46
  • 64
57
votes
5 answers

Oracle date "Between" Query

I am using oracle database. I want to execute one query to check the data between two dates. NAME START_DATE ------------- ------------- Small Widget 15-JAN-10 04.25.32.000000 PM Product 1 17-JAN-10…
Gnaniyar Zubair
  • 8,114
  • 23
  • 61
  • 72
46
votes
13 answers

Oracle: how to add minutes to a timestamp?

I need to add 30 minutes to values in a Oracle date column. I do this in my SELECT statement by specifying to_char(date_and_time + (.000694 * 31) which works fine most of the time. But not when the time is on the AM/PM border. For example,…
Sajee
  • 4,317
  • 14
  • 46
  • 54
45
votes
7 answers

Oracle Date - How to add years to date

I have a date field DATE = 10/10/2010 sum = 4 (this are number of years by calculation) is there a way to add four years to 10/10/2010 and make it 10/10/2014?
kalls
  • 2,797
  • 17
  • 55
  • 75
42
votes
1 answer

Oracle SQL Where clause to find date records older than 30 days

I want to find records in a (Oracle SQL) table using the creation date field where records are older than 30 days. It would be nice to find records using a operators like > but if anyone can suggest quick SQL where clause statement to find records…
anwarma
  • 2,005
  • 5
  • 21
  • 21
38
votes
6 answers

How can I calculate what date Good Friday falls on, given a year?

Does anyone have a good algorithm to calculate what date Good Friday falls on given the year as an input? Preferably in C#.
Bryan Denny
  • 27,363
  • 32
  • 109
  • 125
38
votes
4 answers

Adding months to a date in PostgreSQL shows syntax error

I am using Postgres 9.0 version. I want to add some months to a date to calculate the new date and update in a table. Here the number of months to be added will be in integer variable. My code is as follows: declare numberofmonths smallint =…
Haji
  • 1,999
  • 1
  • 15
  • 21
34
votes
6 answers

How to get the number of days in a month?

I am trying to get the following in Postgres: select day_in_month(2); Expected output: 28 Is there any built-in way in Postgres to do that?
mmrs151
  • 3,924
  • 2
  • 34
  • 38
31
votes
5 answers

How to subtract hours from a date in Oracle so it affects the day also

I'm trying to subtract date from Oracle so it even effect the day as well. For example, if the timestamp is 01/June/2015 00 hours and if I subtract 2 hours, I want to be able to go to to 31/May/2014 22 hours. I tried to_char(sysdate-(2/11),…
hi4ppl
  • 595
  • 1
  • 6
  • 21
30
votes
4 answers

How to calculate an age based on a birthday

Possible Duplicate: How do I calculate someone's age based on a DateTime type birthday? I want to write an ASP.NET helper method which returns the age of a person given his or her birthday. I've tried code like this: public static string Age(this…
nacho10f
  • 5,816
  • 6
  • 42
  • 73
25
votes
9 answers

Easy way to get day number of current quarter?

PHP provides ways to get the number of the current day of the month (date('j')) as well as the number of the current day of the year (date('z')). Is there a way to get the number of the current day of the current quarter? So right now, August 5, it…
Chad Johnson
  • 21,215
  • 34
  • 109
  • 207
24
votes
5 answers

Number of days between two dates C++

I saw examples for C#, Java, but for C++ i cant find solution to calculate how many days between two dates. For example between 2012-01-24 and 2013-01-08 Thanks!
Breakdown
  • 1,035
  • 4
  • 16
  • 26
23
votes
6 answers

How can I get a date after 15 days/1 month in PHP?

In my PHP code I have a date in my variable "$postedDate". Now I want to get the date after 7 days, 15 days, one month and 2 months have elapsed. Which date function should I use? Output date format should be in US format.
Shyju
  • 214,206
  • 104
  • 411
  • 497
23
votes
4 answers

MySQL: Average interval between records

Assume this table: id date ---------------- 1 2010-12-12 2 2010-12-13 3 2010-12-18 4 2010-12-22 5 2010-12-23 How do I find the average intervals between these dates, using MySQL queries only? For instance, the calculation on…
HyderA
  • 20,651
  • 42
  • 112
  • 180
21
votes
4 answers

Print the time an hour ago

Possible Duplicate: Given a time, how can I find the time one month ago How can I print an hour ago in PHP using Date? $date=date("Y-m-d H:i:s"); $time(-1, now); $result=$date.$time; So If I wanted to say "John visited last " Would print John…
TheBlackBenzKid
  • 26,324
  • 41
  • 139
  • 209
1
2
3
73 74