Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Subtract 1 year from datetime

I have seen a lot of info on how to subtract one datetime from the other and how to add years on to a datetime. but the following is giving me a headache....

When a user inserts a record a hidden field called subdate with a value of datetime.now is added to the db.

I then need to have an 'old records' page that lists all the entries that are over 1 year old and was hoping to use something (but using subtract method) similar to;

(DateTime.Now.AddYears(1))

but there is no SubtractYears available? Why?

Please can you let me know how I achieve the same result?

like image 372
Phil Avatar asked Apr 30 '10 09:04

Phil


People also ask

How do you subtract a year from a date?

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.

How do you subtract datetime?

The Subtract(DateTime) method determines the difference between two dates. To subtract a time interval from the current instance, call the Subtract(TimeSpan) method.


1 Answers

DateTime.Now.AddYears(-1)

From the documentation:

A number of years. The value parameter can be negative or positive.

like image 184
Kobi Avatar answered Oct 11 '22 05:10

Kobi