Date Minus 1 Year

Date minus 1 year?

You can use strtotime:

$date = strtotime('2010-01-01 -1 year');

The strtotime function returns a unix timestamp, to get a formatted string you can use date:

echo date('Y-m-d', $date); // echoes '2009-01-01'

Subtract days, months, years from a date in JavaScript

You are simply reducing the values from a number. So substracting 6 from 3 (date) will return -3 only.

You need to individually add/remove unit of time in date object

var date = new Date();
date.setDate( date.getDate() - 6 );
date.setFullYear( date.getFullYear() - 1 );
$("#searchDateFrom").val((date.getMonth() ) + '/' + (date.getDate()) + '/' + (date.getFullYear()));

How to subtract 1 year from a date object in R?

It can be done by converting to yearmon class and then subtract 1

library(zoo)
format(as.yearmon(str1) - 1, '%Y-%m')
#[1] "2012-01" "2012-02"

Similarly, for subtracting a month, use 1/12

format(as.yearmon(str1) - 1/12, '%Y-%m')

data

str1 <- c('2013-01', '2013-02')

Subtracting 1 Year from Date without Lubridate

You are correct that lubridate and dbplyr to not play nicely together (right now). As a result of this, I do most of my dbplyr date manipulation using fragments of sql.

Based on this answer and this site, the postgresql syntax to add/subtract time from a date is:

SELECT old_date + INTERVAL '1 day' AS new_date;

Based on this I would try the following:

output = base_data %>% mutate(lookback_date = date - sql("INTERVAL '1 year'"))

When I do this with a simulated connection, it produces the correct syntax:

library(dplyr)
library(dbplyr)

df = data.frame(my_num = c(1,2,3), my_dates = as.Date(c('2000-01-01','2000-02-02','2000-03-03')))
df = tbl_lazy(df, con = simulate_postgres())

output = df %>% mutate(new_date = my_dates - sql("INTERVAL '1 year'"))

show_query(output)
# <SQL>
# SELECT `my_num`, `my_dates`, `my_dates` - INTERVAL '1 year' AS `new_date`
# FROM `df`

UPDATE: From comment, you first want to convert from date-time to date.

It appears that dbplyr does support the translation of as.Date to PostgreSQL (as.Date is part of base R, not part of lubridate). Hence you can use the following to cast (convert) a column to date:

library(dplyr)
library(dbplyr)

df = data.frame(my_str = c('2000-01-01','2000-02-02','2000-03-03'))
df = tbl_lazy(df, con = simulate_postgres())

output = df %>% mutate(my_date = as.Date(my_str))

show_query(output)
# <SQL>
# SELECT `my_str`, CAST(`my_str` AS DATE) AS `my_date`
# FROM `df`

It also appears that PostgreSQL does not allow you to add an interval of one year. One alternative to this is to extract the year, month, and day from the date, add one to the year and then recombine.

Following these two references (postgre date references and date_part fuction) and this answer, you probablywant something like the following:

output = df %>%
mutate(the_year = DATE_PART('year', my_date),
the_month = DATE_PART('month', my_date),
the_day = DATE_PART('day', my_date)) %>%
mutate(new_date = MAKE_DATE(the_year + 1, the_month, the_day)

Is it possible to subtract days, months or years from current date in swift

You should use Calendar to do these calculations instead of hard coding 86400 for a day.

if let date = Calendar.current.date(byAdding: .day, value: -7, to: Date()) {
// Use this date
}

How to subtract a year from the datetime?

var myDate = DateTime.Now;
var newDate = myDate.AddYears(-1);

Add 1 year to Date and minus 1 Day at the same time

DateTime() accounts for leap years:

// PHP 5.2+
$dt = new DateTime($rows['CoverStartDate']);
$dt->modify('+1 year');
$dt->modify('-1 day');
$renewalDate = $dt->format('d F Y');

or:

// PHP 5.3+
$dt = new DateTime($rows['CoverStartDate']);
$dt->add(new DateInterval('P1Y'));
$dt->sub(new DateInterval('P1D'));
$renewalDate = $dt->format('d F Y');

or:

// PHP 5.5+
$renewalDate = (new DateTime('04/09/2013'))->add(new DateInterval('P1Y'))
->sub(new DateInterval('P1D'))
->format('d F Y');


Related Topics



Leave a reply



Submit