convert a string to a date and add 1 day in csharp

To convert a string to a date in C#, you can use the DateTime.Parse or DateTime.ParseExact method. Here is an example:

main.cs
string dateString = "2022-01-01";
DateTime date = DateTime.Parse(dateString);
78 chars
3 lines

To add 1 day to the date, you can use the AddDays method:

main.cs
date = date.AddDays(1);
24 chars
2 lines

Here is the complete code:

main.cs
string dateString = "2022-01-01";
DateTime date = DateTime.Parse(dateString);
date = date.AddDays(1);
102 chars
4 lines

Now, the date variable will have the value of "2022-01-02".

Note that the input string should be in a valid date format for successful conversion. Also, make sure you handle any exceptions that may occur during the conversion or date manipulation.

related categories

gistlibby LogSnag