[Solved] Adding a number to the day or month or year in a date [duplicate]


In .NET you could do use the AddMonths method:

DateTime date = new DateTime(2013, 5, 19);
DateTime newDate = date.AddMonths(14);

As far as parsing a date from a string using a specified format you could use the TryParseExact method:

string dateStr = "19/05/2013";
DateTime date;
if (DateTime.TryParseExact(dateStr, "dd/MM/yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out date))
{
    // successfully parsed the string into a DateTime instance =>
    // here we could add the desired number of months to it and construct
    // a new DateTime
    DateTime newDate = date.AddMonths(14);
}
else
{
    // parsing failed => the specified string was not in the correct format
    // you could inform the user about that here
}

1

solved Adding a number to the day or month or year in a date [duplicate]