I've got a date in this format:
2009-01-01
How do I return the same date but 1 year earlier?
Add years to or subtract years from a date In cell A6, type =DATE(YEAR(A2)+B2,MONTH(A2),DAY(A2)), and then press RETURN . This formula adds the value in cell B2 (3 years) to the value in cell A2, for a result of 6/9/2012.
The easiest way to subtract years from a date in Python is to use the dateutil extension. The relativedelta object from the dateutil. relativedelta module allows you to subtract any number of years from a date object.
function subtractYears(numOfYears, date = new Date()) { date. setFullYear(date. getFullYear() - numOfYears); return date; } // 👇️ subtract 1 year from current Date const result = subtractYears(1); // 👇️ Subtract 2 years from another Date const date = new Date('2022-04-26'); // 👇️ Sun Apr 26 2020 console.
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'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With