使用给定的DateTime对象获取一个月的第一个和最后一天 [英] Getting the first and last day of a month, using a given DateTime object

查看:707
本文介绍了使用给定的DateTime对象获取一个月的第一个和最后一天的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获得给定日期所在的月份的第一天和最后一天。日期来自UI字段中的值。

I want to get the first day and last day of the month where a given date lies in. The date comes from a value in a UI field.

如果我正在使用时间选择器我可以说

If I'm using a time picker I could say

var maxDay = dtpAttendance.MaxDate.Day;

但是我试图从DateTime对象中获取它。所以如果我有这个...

But I'm trying to get it from a DateTime object. So if I have this...

DateTime dt = DateTime.today;

如何从 dt获得月的第一天和最后一天code>?

How to get first day and last day of the month from dt?

推荐答案

DateTime 结构只存储一个值,而不是值的范围。 MinValue MaxValue 是静态字段,其中包含 DateTime 结构。这些字段是静态的,与 DateTime 的特定实例无关。它们与 DateTime 类型本身有关。

DateTime structure stores only one value, not range of values. MinValue and MaxValue are static fields, which hold range of possible values for instances of DateTime structure. These fields are static and does not relate to particular instance of DateTime. They relate to DateTime type itself.

建议阅读: static(C#参考)

更新:获取月份范围: / p>

UPDATE: Getting month range:

DateTime date = ...
var firstDayOfMonth = new DateTime(date.Year, date.Month, 1);
var lastDayOfMonth = firstDayOfMonth.AddMonths(1).AddDays(-1);

这篇关于使用给定的DateTime对象获取一个月的第一个和最后一天的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆