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
21
votes
3 answers

Add Day to Timestamp

How do I add days to a timestamp? If my timestamp is 01-JAN-2011 11-09-05 and I add 2 days, I want 03-JAN-2011 11-09-05.
Nadir
  • 211
  • 1
  • 2
  • 3
21
votes
8 answers

T-SQL to trim a datetime to the nearest date?

Duplicate of What's the BEST way to remove the time portion of a datetime value (SQL Server)? I have a column that tracks when things are created using a datetime, but I'd like to generate a report that groups them by day, so I need a way of nulling…
Allain Lalonde
  • 91,574
  • 70
  • 187
  • 238
21
votes
1 answer

Calculating time (adding minutes) bash

I got stuck in part of the script. I have time: for example "16:00" and duration in minutes like: 410. Is there any easy way to add those two values? I've tried a lot of combinations with date -d, but can't solve it.
Kysu
  • 225
  • 1
  • 2
  • 6
20
votes
2 answers

Oracle get records updated in last one hour

Below is the query I am running to get updates in last one hour. select count(*) from my_table where last_updated_date between to_date(to_char(sysdate,'YYYY-MM-DD HH24'))-1/24 and to_date(to_char(sysdate,'YYYY-MM-DD HH24')); Our DB is oracle and…
Sirish
  • 9,183
  • 22
  • 72
  • 107
18
votes
2 answers

Subtract hours from the now() function

We have a machine running 24x7. Every day I report the number of pieces it produced per hour. In our case one working day means '2015-06-16 06:00:00' to '2015-06-17 06:00:00' for example. Here is my code: select date_trunc('hour',…
Chanti
  • 525
  • 1
  • 5
  • 15
18
votes
3 answers

Calculate working hours between 2 dates in PostgreSQL

I am developing an algorithm with Postgres (PL/pgSQL) and I need to calculate the number of working hours between 2 timestamps, taking into account that weekends are not working and the rest of the days are counted only from 8am to…
18
votes
6 answers

Bug in WeekNumber calculation .NET?

I have a rather weird problem. I live in denmark and here the first week (Week 1) of 2013 starts the 31th of december 2012 and lasts for 7 days - as weeks normally do :) According to .NET however the 30th of december is Week 52, the 31th is Week 53…
17
votes
1 answer

Getting current date in JSTL EL and doing arithmetic on it

Without using scriptlets, what's the correct way for doing date arithmetic in JSP? Here are examples what I'm trying to do: Get current year (YYYY) Subtract current year by one to get previous year (YYYY) Thanks!
Shawn
  • 7,235
  • 6
  • 33
  • 45
17
votes
7 answers

Calculating difference in dates in Java

I find it funny that Java (or the java.util library) does not have a built-in function to calculate difference in dates. I want to subtract one date from another to get the elapsed time between them. What is the best way to do this? I know the…
Anirudh
  • 2,209
  • 4
  • 25
  • 32
17
votes
2 answers

Why is Date is being returned as type 'double'?

I'm having some trouble working with the as.Date function in R. I have a vector of dates that I'm reading in from a .csv file that are coming in as a factor of integers or as character (depending on how I read in the file, but this doesn't seem to…
drRussClay
  • 175
  • 1
  • 1
  • 7
16
votes
6 answers

java calculate time between two timestamps

I need to calculate the time passed between two dates. The catch here is that I need to show it as YouTube does with its video comments timestamps. That is, to show it by just the largest measure. For example, if the time is 50 seconds ago it…
DArkO
  • 15,880
  • 12
  • 60
  • 88
15
votes
1 answer

ORACLE SQL Date range intersections

I have a table T1, it contains a NAME value (not unique), and a date range (D1 and D2 which are dates) When NAME are the same, we make a union of the date ranges (e.g. B). But as a result (X), we need to make intersection of all the date…
guigui42
  • 2,441
  • 8
  • 35
  • 48
15
votes
9 answers

Get the difference between two dates both In Months and days in sql

I need to get the difference between two dates say if the difference is 84 days, I should probably have output as 2 months and 14 days, the code I have just gives the totals. Here is the code SELECT Months_between(To_date('20120325', 'YYYYMMDD'), …
Stanley Mungai
  • 4,044
  • 30
  • 100
  • 168
14
votes
2 answers

How to get the end of a day?

I'm using PostgreSQL 8.4. I have a column of the table my_tbl which contains dates (timestamp without timezone). For instance: date ------------------- 2014-05-27 12:03:20 2014-10-30 01:20:03 2013-10-19 16:34:34 2013-07-10 15:24:26 2013-06-24…
St.Antario
  • 26,175
  • 41
  • 130
  • 318
14
votes
4 answers

Get the last day of the current year as date

How can I get the last day (Dec 31) of the current year as a date using PHP? I tried the following but this doesn't work: $year = date('Y'); $yearEnd = strtotime($year . '-12-31'); What I need is a date that looks like 2014-12-31 for the current…
user2571510
  • 11,167
  • 39
  • 92
  • 138
1 2
3
73 74