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
10
votes
9 answers

How to subtract 2 dates in oracle to get the result in hour and minute

I want to subtract 2 dates and represent the result in hour and minute in one decimal figure. I have the following table and I am doing it in this way but the result is not as desired. There is some slight variation, I'm sure this is simple…
Macky
  • 121
  • 1
  • 2
  • 7
10
votes
1 answer

How do I a convert a number to an INTERVAL of minutes?

In pseudo-Oracle, I want to do the following: select systimestamp + to_interval(select NUMERIC_COLUMN from SOME_TABLE where SOME_TABLE_PK = :stuff) from dual; If the number of minutes were always the same, I could use an interval literal a la…
Hank Gay
  • 70,339
  • 36
  • 160
  • 222
10
votes
3 answers

Date arithmetic in Erlang

I have a date in this format {Y,M,D}. Is there any good supporting libraries or, tricks I can use to simply, say subtract three months from this date without running into problem with invalid dates, leap years, etc. My latest similar use is in MySql…
Magnus Kronqvist
  • 1,559
  • 12
  • 22
10
votes
4 answers

Getting Last Day of Previous Month in Oracle Function

I need a function in Oracle like this. When i giving a parameter a simple date. Then function should getting me last day of the previous month. Example: FunctionName(10.02.2011) Result should be 31.01.2011 FunctionName(21.03.2011) Result should be…
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
9
votes
6 answers

How to get a list of months between 2 given dates using a query?

I have 2 dates, say 28-Mar-2011 and 29-Jun-2011. I need an sql query that will display the months between these 2 dates including the months containing the dates, ie. June, May, April and March.
Nitish
  • 1,686
  • 8
  • 23
  • 42
9
votes
10 answers

ADD_MONTHS function does not return the correct date in Oracle

See the results of below queries: >> SELECT ADD_MONTHS(TO_DATE('30-MAR-11','DD-MON-RR'),-4) FROM DUAL; 30-NOV-10 >> SELECT ADD_MONTHS(TO_DATE('30-NOV-10','DD-MON-RR'),4) FROM DUAL; 31-MAR-11 How can I get '30-MAR-11' when adding 4 months to some…
Mohamed Saligh
  • 12,029
  • 19
  • 65
  • 84
9
votes
3 answers

Group dates by week in R

I wish to label my data based on their week. This is my data: df2 <- structure(list(Order_Date = structure(c(16735, 16805, 16753, 16830, 17075, 17009, 17085, 16740, 16891, 16750, 16820, 16849, 16906, 16929, 16746, 16731, 16786, 16873, 16895,…
MFR
  • 2,049
  • 3
  • 29
  • 53
9
votes
4 answers

What is the recommended way of doing date arithmetics in Perl?

What is the recommended way of doing date arithmetics in Perl? Say for example that I want to know the date three days ago from today (where today = 2010-10-17 and today - 3 days = 2010-10-13). How would you do that in Perl?
knorv
  • 49,059
  • 74
  • 210
  • 294
8
votes
3 answers

Comparing date with sysdate in oracle

I have a column which is of "DATE" type and I want to run a query on it comparing it with sysdate. But I am getting following error, Can someone please let me know what I am missing here? SQL> select distinct file_name as r from table_1 …
TopCoder
  • 4,206
  • 19
  • 52
  • 64
8
votes
3 answers

How to do date calculations in Shell Scripting?

I have a shell script that runs every night to backup my EC2 sites database and html to S3, and when it backs the folders up, it appends the date to it for easier viewing. But I want it to also be able to delete the relevant backup folders from 3…
cfrederich
  • 1,790
  • 6
  • 26
  • 35
8
votes
6 answers

Comparing dates in Oracle using the decode function

I need to compare two dates using the Oracle decode function to see if one is less than or equal to the other. I found this article - http://www.techonthenet.com/oracle/functions/decode.php Which states (at the bottom) that the below decode function…
Freddy
  • 442
  • 1
  • 6
  • 13
8
votes
3 answers

Find NSDate for the next closest specific day of week for any date

Let's suppose that today is Wednesday. I can break an NSDate down into NSDateComponents, but I need to find the NSDate with the next upcoming Monday. If today is Monday, then the next upcoming Monday is today. What's the right way to achieve this?
Bartłomiej Semańczyk
  • 59,234
  • 49
  • 233
  • 358
8
votes
8 answers

About "5"-1 & "5"+1 in Javascript (plus and minus signs)

I read a book about operators in Javascript, and this confused me. console.log("5"+1); This would make "5" as a string. So the result would be 51. console.log("5"-1); This result would be 4. I know it converts "5" to 5, but why it isn't shown…
chenghuayang
  • 1,424
  • 1
  • 17
  • 35
8
votes
1 answer

Calculate days between date and today in PHP

If I have a given date in the format yyyy-mm-dd, how can I calculate the difference in days to the current date ? I just want to check whether this date is more than one week (7 days) old compared to the current date.
user2571510
  • 11,167
  • 39
  • 92
  • 138
8
votes
8 answers

Arithmetics on calendar dates in C or C++ (add N days to given date)

I have been given a date, Which I am taking as an input like (day, month, year): 12, 03, 87. Now I need to find out the date after n days. I have written code for this, But its not efficient. Can you please tell me any good logic which works faster…
vivek jain
  • 591
  • 4
  • 13
  • 28