gpt4 book ai didi

c# 根据当前日期获取开始日期和最后日期

转载 作者:IT王子 更新时间:2023-10-29 04:45:28 26 4
gpt4 key购买 nike

我正在做一个基于日期的事件监视器,类似于 stackoverflow Today,YesterDay,this week,Last week,this month,last Month..... 基于当前日期如何在 C# 中获取所有这些 Today,YesterDay,this week,Last week,this month,last Monthstart-dateend-date

最佳答案

DateTime baseDate = DateTime.Today;

var today = baseDate;
var yesterday = baseDate.AddDays(-1);
var thisWeekStart = baseDate.AddDays(-(int)baseDate.DayOfWeek);
var thisWeekEnd = thisWeekStart.AddDays(7).AddSeconds(-1);
var lastWeekStart = thisWeekStart.AddDays(-7);
var lastWeekEnd = thisWeekStart.AddSeconds(-1);
var thisMonthStart = baseDate.AddDays(1 - baseDate.Day);
var thisMonthEnd = thisMonthStart.AddMonths(1).AddSeconds(-1);
var lastMonthStart = thisMonthStart.AddMonths(-1);
var lastMonthEnd = thisMonthStart.AddSeconds(-1);

关于c# 根据当前日期获取开始日期和最后日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2821035/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com