date-arithmetic

Oracle Date - How to add years to date

天涯浪子 提交于 2019-11-26 17:47:44
问题 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? 回答1: Try adding months (12 * number of years) instead. Like this- add_months(date'2010-10-10', 48) 回答2: Use add_months Example: SELECT add_months( to_date('10-OCT-2010'), 48 ) FROM DUAL; Warning add_months, returns the last day of the resulting month if you input the last day of a month to begin with. So add_months(to_date('28-feb-2011'

Add a month to a Date [duplicate]

Deadly 提交于 2019-11-26 17:25:57
This question already has an answer here: How to subtract months from a date in R? 2 answers 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 overlapped. seq(as.Date("2004-01-31"), by = "month", length = 2) # [1] "2004-01-31" "2004-03-02" Above might work , but again its not straight forward. Also its also adding 30 days or something to the date which has issues like the below seq(as.Date("2004-01-31"), by = "month", length =

Date comparison returns unusual result - SQL Oracle

前提是你 提交于 2019-11-26 17:25:11
问题 I have a table of the structure: +---------+--------------+-----------------+---------------+-------+------+ | week_no | long_week_no | week_start_date | week_end_date | month | year | +---------+--------------+-----------------+---------------+-------+------+ | 1 | 1A | 01/01/2015 | 03/01/2015 | JAN | 2015 | | 1 | 1B | 04/01/2015 | 10/01/2015 | JAN | 2015 | | 2 | 2 | 11/01/2015 | 17/01/2015 | JAN | 2015 | | 3 | 3 | 18/01/2015 | 24/01/2015 | JAN | 2015 | | .. | .. | .. | .. | .. | .. | | 51 |

Calculate working hours between 2 dates in PostgreSQL

瘦欲@ 提交于 2019-11-26 16:33:21
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 15pm. Examples: From Dec 3rd at 14pm to Dec 4th at 9am should count 2 hours: 3rd = 1, 4th = 1 From Dec 3rd at 15pm to Dec 7th at 8am should count 8 hours: 3rd = 0, 4th = 8, 5th = 0, 6th = 0, 7th = 0 It would be great to consider hour fractions as well. Erwin Brandstetter According to your question working hours are: Mo–Fr, 08:00–15:00 . Rounded results For just two

How to subtract years?

痞子三分冷 提交于 2019-11-26 13:05:18
问题 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(\'2010-03-17\') . How would I do that? 回答1: The easiest thing to do is to convert it into POSIXlt and subtract 2 from the years slot. > d <- as.POSIXlt(as.Date('2010/03/17')) > d$year <- d$year-2 > as.Date(d) [1] "2008-03-17" See this related question: How to subtract days in R?. 回答2: With lubridate library(lubridate) ymd(

Calculating difference in dates in Java

只谈情不闲聊 提交于 2019-11-26 12:31:16
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 simple way is to take the difference of the time in milliseconds and then convert that into days. However, I wanted to know if this works in all cases (with daylight saving, etc.). I know the simple way is to take the difference of the time in milliseconds and then convert that into days. However, i wanted to know if this works in all cases (with

Number of days between two dates C++

江枫思渺然 提交于 2019-11-26 09:09:41
问题 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! 回答1: This is one way. #include <iostream> #include <ctime> int main() { struct std::tm a = {0,0,0,24,5,104}; /* June 24, 2004 */ struct std::tm b = {0,0,0,5,6,104}; /* July 5, 2004 */ std::time_t x = std::mktime(&a); std::time_t y = std::mktime(&b); if ( x != (std::time_t)(-1) && y != (std::time_t)(-1) ) { double difference = std:

Calculate business days in Oracle SQL(no functions or procedure)

别等时光非礼了梦想. 提交于 2019-11-26 07:45:50
问题 I am trying to calculate business days between two dates in Oracle select. I got to the point when my calculation gives most results correct for given dates (I compare it with NETWORKDAYS in excel) but sometimes it varies from 2 days to -2 days - and I don\'t know why... Here\'s my code: SELECT ((to_char(CompleteDate,\'J\') - to_char(InstallDate,\'J\'))+1) - (((to_char(CompleteDate,\'WW\')+ (52 * ((to_char(CompleteDate,\'YYYY\') - to_char(InstallDate,\'YYYY\'))))) - to_char(InstallDate,\'WW\'

How to change the default time zone in R?

倾然丶 夕夏残阳落幕 提交于 2019-11-26 07:28:12
问题 This question was migrated from Cross Validated because it can be answered on Stack Overflow. Migrated 8 years ago . 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. 回答1: Another way to do it, without changing the whole computer time is using the setenv command like this : Sys.setenv(TZ='GMT') 回答2: See this good article on changing time zone in R: http:/

The difference in months between dates in MySQL

[亡魂溺海] 提交于 2019-11-26 06:28:52
问题 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 the dividing by 2 592 000 (seconds) and rounding up whithin MySQL? 回答1: The DATEDIFF function can give you the number of days between two dates. Which is more accurate, since... how do you define a month? (28, 29, 30, or 31 days?) 回答2: Month-difference between any given two dates: I'm surprised this hasn't been mentioned yet: Have a look at the TIMESTAMPDIFF()