How to add date

Hi All,

I have a input as 11/02/2019 which is 11 th month 02 date.

I need to get the output as 11/01/2020

I have to get the output as same month but 1 date before from the output.

If adddays 365 for some time I am not getting the expected output…

Thanks

example :
DateTime.Now.Add(-3).ToString(“dd-MM-yy”)

use.Add(-1) to get todays date minus 1
as @fayis.cm example using → .addDays(-1) will give diffrent result format
please refer to this

regards
ahmad

1 Like

Try This:
Convert.ToDateTime(“11/02/2019”).AddDays(-1).AddYears(1).ToShortDateString

Hi @vaishali

Try the below expression

DateTime.ParseExact("11/02/2019",("dd/MM/yyyy"),Globalization.CultureInfo.InvariantCulture).AddMonths(-1).AddYears(1).ToString("dd/MM/yyyy")

Have a view on the link for more information

Hope it will help you.

Regards
Gokul

Thanks for your reply…

Hi,

Instead of adding addyear (1)
Need to pass current t year

Addyear(1)

Shall we pass datetime.now.year?

Hi @vaishali

Try this below expression

System.Text.RegularExpressions.Regex.Replace(DateTime.ParseExact("11/02/2019",("dd/MM/yyyy"),Globalization.CultureInfo.InvariantCulture).AddMonths(-1).ToString("dd/MM/yyyy"),"\d{4}",DateTime.Now.Year.ToString)

Hope it will work

If it is work mark the appropriate post as solved. it will help for other user

Regards
Gokul

Hi,

The DateTime.AddYears() method used to add the specified number of years to the value of this instance. we can only pass integer values , which will increment years from the current year.
if you want to subtract years, then use a negative value.

Thank you.

You can see the result you need @vaishali

Regards
Gokul

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.