假设我有 678 天,如何计算从那一刻起有多少年、月和日?

Duration duration = Duration.FromStandardDays(678);
Instant now = SystemClock.Instance.Now;
Instant future = now + duration;

// I have to convert NodaTime.Instant to NodaTime.LocalDate but don't know how

Period period = Period.Between(now, future);
Console.WriteLine("{0} years, {1} months, {2} days", period.Years, period.Months, period.Days);

最佳答案

您只需要将天数添加到当前时间:

var now = DateTime.Now;
var future = now.AddDays(678);

int years = future.Year - now.Year;
int months = future.Month - now.Month;
if (months < 0)
{
    years--;
    months += 12;
}
int days = future.Day + DateTime.DaysInMonth(now.Year, now.Month) - now.Day;

关于c# - X 天中年/月/日的 Nodatime 计算,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22795924/

10-11 22:32
查看更多