Обчислення дати у майбутньому чи минулому

C#:
Обчислення дати у майбутньому чи минулому

How to: (Як це зробити:)

C# makes date manipulation straightforward. Use DateTime and TimeSpan. Here’s how:

using System;

class Program
{
    static void Main()
    {
        DateTime today = DateTime.Now;
        
        // Future date - 30 days from today
        DateTime futureDate = today.AddDays(30);
        Console.WriteLine("Future Date: " + futureDate.ToString("dd/MM/yyyy"));
        
        // Past date - 60 days ago
        DateTime pastDate = today.AddDays(-60);
        Console.WriteLine("Past Date: " + pastDate.ToString("dd/MM/yyyy"));
    }
}

Sample output:

Future Date: 30/04/2023 // this will be +30 days from the current date
Past Date: 30/01/2023 // this will be -60 days from the current date

Deep Dive (Глибоке Занурення):

In the past, date manipulation was tied to complex calendar systems. C# eases this with DateTime and TimeSpan. But remember, time zones and leap years can complicate things.

Alternatives? NodaTime library is robust for complex scenarios. AddDays is good for simple cases. You can also manipulate hours, minutes, and seconds with AddHours, AddMinutes, and AddSeconds.

Implementation details? DateTime is part of the .NET base class library — accurate, but not timezone-aware. For that, use DateTimeOffset.

See Also (Дивіться також):