 # Need help to calculate number of years in Razor code. RSS

## 2 replies

Last post Oct 30, 2018 03:20 AM by Zhi Lv - MSFT

• ### Need help to calculate number of years in Razor code.

Hi ,

I need to calculate number of years between current date and specified field in database in Razor. We use razor code to build email content.

I'm looking for something like:

@Convert.ToInt32((DateTime.Now - (Convert.ToDateTime(Model.Listings.LatestSubscriptionEndDate))).Years) Years

but this is not working..

• ### Re: Need help to calculate number of years in Razor code.

I was able to figure out years part . I need to calculate it only for values which exist only for month of november.

@Convert.ToInt32(((DateTime.Now - (Convert.ToDateTime(Model.FirstLiveDate))).TotalDays)/365)

• ### Re: Need help to calculate number of years in Razor code.

Oct 30, 2018 03:20 AM|Zhi Lv - MSFT|LINK

Hi cVishal259,

#### cVishal259

I was able to figure out years part . I need to calculate it only for values which exist only for month of november.

@Convert.ToInt32(((DateTime.Now - (Convert.ToDateTime(Model.FirstLiveDate))).TotalDays)/365)

It seems that you have already solved the problem. You could also refer to the following code to calculate the time span:

` @Convert.ToInt32((DateTime.Now.Year - (Convert.ToDateTime(Model.LatestSubscriptionEndDate).Year))) Years`

or

```        @{
var timespan = DateTime.Now - (Convert.ToDateTime(Model.LatestSubscriptionEndDate));
var tempdate = DateTime.MinValue + timespan;
var year = tempdate.Year - 1;
var month = tempdate.Month - 1;
var day = tempdate.Day - 1;
}
@year  Years @month Months @day Days ago```

Best regards,
Dillion

MSDN Community Support