Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to subtract a year from the datetime?

Tags:

c#

How to subtract a year from current datetime using c#?

like image 953
Jango Avatar asked Jun 07 '10 19:06

Jango


People also ask

How do I subtract years from a date in python?

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.

How do you subtract DateTime?

Use DateTime. Subtract which will return TimeSpan , then use TotalSeconds property of the result which is of type double. Show activity on this post.

How do you subtract month from DateTime?

Just substract a month by 'adding` -1: var lastmonth = DateTime. Today. AddMonths(-1);


2 Answers

var myDate = DateTime.Now; var newDate = myDate.AddYears(-1); 
like image 99
D'Arcy Rittich Avatar answered Sep 23 '22 21:09

D'Arcy Rittich


DateTime oneYearAgoToday = DateTime.Now.AddYears(-1); 

Subtracting a week:

DateTime weekago = DateTime.Now.AddDays(-7); 
like image 42
kemiller2002 Avatar answered Sep 23 '22 21:09

kemiller2002