使用当前日期作为参考获取特定日期

发布于 2024-12-11 21:27:28 字数 658 浏览 0 评论 0原文

我想通过使用当前日期来获取两个特定日期,让我解释一下。

例如,如果今天是 10/27/2011 那么我想要 7/01/2011 和 9/30/2011。请注意,这是三个月的期限(不包括当月),我该怎么做?

目前我正在遵循自己设计的方法,但我认为它还很不好。这是代码。

    TimeSpan TSFrom = new TimeSpan(90 + DateTime.Now.Day, 0, 0, 0, 0);
    TimeSpan TSTo = new TimeSpan(DateTime.Now.Day, 0, 0, 0, 0);
    Response.Write(DateTime.Now.Subtract(TSFrom).ToShortDateString());
    Response.Write(DateTime.Now.Subtract(TSTo).ToShortDateString());

此代码返回这些值

7/2/2011 - 9/30/2011

,虽然它有些可以接受,但它看起来仍然不是一个完美的方式,看起来第一个日期是从每月的第二天开始,而它应该从第一天开始日,我认为这是因为有些月份在 29 日结束,而有些月份在 30 日结束。那么我怎样才能获得像 7/1/2011 到 9/30/2011 这样的完美日期。

谢谢。

I want to get two particular dates by using current date, let me explain more.

For example if today is 10/27/2011 then I would like to have 7/01/2011 and 9/30/2011. Note that its a three month period (excluding current month) How can I do that ?

Currently I am following a self designed method but I think its far from good. Here is the code.

    TimeSpan TSFrom = new TimeSpan(90 + DateTime.Now.Day, 0, 0, 0, 0);
    TimeSpan TSTo = new TimeSpan(DateTime.Now.Day, 0, 0, 0, 0);
    Response.Write(DateTime.Now.Subtract(TSFrom).ToShortDateString());
    Response.Write(DateTime.Now.Subtract(TSTo).ToShortDateString());

This code return these values

7/2/2011 - 9/30/2011

while its some what acceptable its still looks like not a perfect way to go and look the first date is starting from the second day of month while it should start from first day, I think its because some months end on 29 while some on 30. So how can I get the perfect dates like 7/1/2011 to 9/30/2011.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

待"谢繁草 2024-12-18 21:27:28
var now = DateTime.Now;
var end = new DateTime(now.Year, now.Month, 1).AddDays(-1); // Last day of previous month
var start = new DateTime(now.Year, now.Month, 1).AddMonths(-3); // First day of third-last month

(您可以将 new DateTime(now.Year, now.Month, 1) 存储在局部变量中,我想这是个人品味的问题......)

var now = DateTime.Now;
var end = new DateTime(now.Year, now.Month, 1).AddDays(-1); // Last day of previous month
var start = new DateTime(now.Year, now.Month, 1).AddMonths(-3); // First day of third-last month

(You could store new DateTime(now.Year, now.Month, 1) in a local variable, this is a matter of personal taste, I guess ...)

柳若烟 2024-12-18 21:27:28
DateTime now = DateTime.Today;
DateTime firstOfMonth = now.AddDays(-now.Day + 1);
DateTime beginning = firstOfMonth.AddMonths(-3);
DateTime end = firstOfMonth.AddDays(-1);

我们通过减去(当前日期 - 1)来“回滚”到月初,
周期结束时间为 firstOfMonth.AddDays(-1);,周期开始时间为 firstOfMonth.AddMonths(-3);

DateTime now = DateTime.Today;
DateTime firstOfMonth = now.AddDays(-now.Day + 1);
DateTime beginning = firstOfMonth.AddMonths(-3);
DateTime end = firstOfMonth.AddDays(-1);

We "roll back" to the beginning of the month by subtracting the (current day - 1),
the end of the period is firstOfMonth.AddDays(-1);, the beginning of the period is firstOfMonth.AddMonths(-3);

成熟稳重的好男人 2024-12-18 21:27:28
var fromWithDay = DateTime.Today.AddMonths(-3);
var from = new DateTime(fromWithDay.Year, fromWithDay.Month, 1);
var toWithDay = DateTime.Today;
var to = new DateTime(toWithDay.Year, toWithDay.Month, 1).AddDays(-1);

它可以更短但可读性较差

var fromWithDay = DateTime.Today.AddMonths(-3);
var from = new DateTime(fromWithDay.Year, fromWithDay.Month, 1);
var toWithDay = DateTime.Today;
var to = new DateTime(toWithDay.Year, toWithDay.Month, 1).AddDays(-1);

It could be even shorter but less readable

酒儿 2024-12-18 21:27:28
DateTime now = DateTime.Now;
DateTime firstDayOfThisMonth = new DateTime(now.Year, now.Month, 1);
DateTime startDate = firstDayOfThisMonth.AddMonths(-3);
DateTime endDate = firstDayOfThisMonth.AddDays(-1);
Console.WriteLine(startDate);
Console.WriteLine(endDate);
DateTime now = DateTime.Now;
DateTime firstDayOfThisMonth = new DateTime(now.Year, now.Month, 1);
DateTime startDate = firstDayOfThisMonth.AddMonths(-3);
DateTime endDate = firstDayOfThisMonth.AddDays(-1);
Console.WriteLine(startDate);
Console.WriteLine(endDate);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文