如何找出月末的日子 [英] how to find out month end day

查看:84
本文介绍了如何找出月末的日子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何查找特定日期的月末



示例



1/1 / 2010月底结束日= 2010年1月31日



15/5/2013月结束日= 2013年5月31日







是任何方法???

how to find out month end from a specific date

example

1/1/2010 month end day = 31/1/2010

15/5/2013 month end day = 31/5/2013



is any method ???

推荐答案

最后一天你得到这样的月份,返回31:

The last day of the month you get like this, which returns 31:
DateTime.DaysInMonth(2010, 01);



你也可以使用:


You can also get it by using:

var lastDayOfMonth = DateTime.DaysInMonth(date.Year, date.Month);


非常简单的方法

只需获取下个月第一天的日期(总是第一天)

并从datetime对象中扣除1天。



这样你也不用担心闰年,这总是有用的。





希望这会对你有所帮助。
very simple approach
just get the date for first day of next month (which is always 1st)
and deduct 1 day from datetime object.

this way you need not to worry for leap years also, this will always work.


hope this helps you.


如果日期以字符串形式给出,则必须将其转换为允许获取月份和年份数字的数字格式。然后你可以使用这些公式:

If the date is given as string, it must be converted to a numeric format which allows getting the month and year number. Then you can use these formulas:
bool IsLeapYear(int nYear)
{ 
    return (nYear % 4) == 0 && ((nYear % 100) != 0 || (nYear % 400) == 0); 
}

// Get number of days per month. NOTE: nMonth is 1 based!
int GetDaysOfMonth(int nYear, int nMonth)
{ 
    return 31 - ((nMonth == 2) ? (3 - IsLeapYear(nYear)) : (((nMonth - 1) % 7 ) % 2)); 
}


这篇关于如何找出月末的日子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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