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
4772
votes
43 answers

How do I get a timestamp in JavaScript?

I want a single number that represents the current date and time, like a Unix timestamp.
Pablo Fernandez
  • 279,434
  • 135
  • 377
  • 622
566
votes
21 answers

How do I find the time difference between two datetime objects in python?

How do I tell the time difference in minutes between two datetime objects?
Hobhouse
  • 15,463
  • 12
  • 35
  • 43
210
votes
17 answers

Calculating days between two dates with Java

I want a Java program that calculates days between two dates. Type the first date (German notation; with whitespaces: "dd mm yyyy") Type the second date. The program should calculates the number of days between the two dates. How can I include…
user3025448
  • 2,125
  • 2
  • 12
  • 7
150
votes
11 answers

How can I do time/hours arithmetic in Google Spreadsheet?

How do I do time/hour arithmetic in a Google spreadsheet? I have a value that is time (e.g., 36:00:00) and I want to divide it by another time (e.g., 3:00:00) and get 12. If I divide just one by the other, I get 288:00:00 when what I want is 12 (or…
user128807
  • 10,447
  • 17
  • 53
  • 72
120
votes
19 answers

How to find the duration of difference between two dates in java?

I have two objects of DateTime, which need to find the duration of their difference, I have the following code but not sure how to continue it to get to the expected results as following: Example: 11/03/14 09:30:58 11/03/14 09:33:43 …
J888
  • 1,944
  • 8
  • 42
  • 76
112
votes
21 answers

How to get difference between two dates in months using MySQL query?

I'm looking to calculate the number of months between 2 date time fields. Is there a better way than getting the Unix timestamp and then dividing by 2 592 000 (seconds) and rounding up within MySQL?
Darryl Hein
  • 142,451
  • 95
  • 218
  • 261
105
votes
5 answers

Oracle DateTime in Where Clause?

I have sql something like this: SELECT EMP_NAME, DEPT FROM EMPLOYEE WHERE TIME_CREATED >= TO_DATE('26/JAN/2011','dd/mon/yyyy') -> This returns 10 rows and TIME_CREATED = '26-JAN-2011' Now when i do this i don't get any rows back, SELECT EMP_NAME,…
sanjeev40084
  • 9,227
  • 18
  • 67
  • 99
105
votes
21 answers

Calculate difference between 2 date / times in Oracle SQL

I have a table as follows: Filename - varchar Creation Date - Date format dd/mm/yyyy hh24:mi:ss Oldest cdr date - Date format dd/mm/yyyy hh24:mi:ss How can I calcuate the difference in hours minutes and seconds (and possibly days) between the two…
Steve
  • 1,149
  • 3
  • 12
  • 13
98
votes
7 answers

Check if year is leap year in javascript

function leapYear(year){ var result; year = parseInt(document.getElementById("isYear").value); if (years/400){ result = true } else if(years/100){ result = false } else if(years/4){ result= true } …
BigBob
  • 1,015
  • 1
  • 8
  • 9
84
votes
9 answers

Delete rows with date older than 30 days with SQL Server query

I need a SQL statement to delete row that are older than 30 days. My table events has a field date that contains the date and the time it was inserted in the database. Will this work? SELECT * from Results WHERE [Date] >= DATEADD(d, -30, getdate())
Alex
  • 10,869
  • 28
  • 93
  • 165
81
votes
7 answers

MySQL how to fill missing dates in range?

I have a table with 2 columns, date and score. It has at most 30 entries, for each of the last 30 days one. date score ----------------- 1.8.2010 19 2.8.2010 21 4.8.2010 14 7.8.2010 10 10.8.2010 14 My problem is that some dates are…
Jerry2
  • 2,955
  • 5
  • 30
  • 39
77
votes
3 answers

How do you find results that occurred in the past week?

I have a books table with a returned_date column. I'd like to see the results for all of the books with a returned date that occurred in the past week. Any thoughts? I tried doing some date math, but Postgres wasn't happy with my attempt.
Huuuze
  • 15,528
  • 25
  • 72
  • 91
73
votes
4 answers

How to change the default time zone in R?

How can I change the default timezone in R? I'm working with time series. All my time series are defined in UTC time zone, but if I print a date it is always done in CET/CEST time zone.
Paul PUGET
  • 1,849
  • 2
  • 12
  • 7
73
votes
8 answers

Add a month to a Date

I am trying to add a month to a date i have. But then its not possible in a straight manner so far. Following is what i tried. d <- as.Date("2004-01-31") d + 60 # [1] "2004-03-31" Adding wont help as the month wont be…
Vineeth Mohan
  • 18,633
  • 8
  • 63
  • 77
70
votes
2 answers

MySQL Select last 7 days

I read some posts here and seems like nothing special but I can not still select the entries of the last days. SELECT p1.kArtikel, p1.cName, p1.cKurzBeschreibung, p1.dLetzteAktualisierung, p1.dErstellt, p1.cSeo, …
karadayi
  • 2,212
  • 2
  • 21
  • 36
1
2 3
73 74