如何获得该月的最后一天 [英] How to get the last day of the month

查看:89
本文介绍了如何获得该月的最后一天的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请知道如何获得本月的最后一天。



i我正在写一个代码,需要我提供一个到期日期作为参数,现在即将到期的日期是该月的最后一个日期。



i今天可以通过这样做来获得

 DateTime now = DateTime.Today; 





但是如何获得本月的最后一个日期呢。 />


谢谢



我尝试过:



DateTime now = DateTime.Today



就像我之前解释的那样......



i只需要一种方法来获得本月的最后一天。



谢谢。

解决方案

关于Google如何: Google forc#Last Day of Month [ ^ ]



你会轻松地发现它必须是这样的:

今天的DateTime = DateTime.Today; 
int lastDay = DateTime.DaysInMonth(today.Year,today.Month);


< blockquote>如果提供日期,您可以通过以下代码获得当月的最后一天。



 public static DateTime GetLastDayOfMonth(this DateTime dateTime)
{
返回new DateTime(dateTime.Year,dateTime。 Month,DateTime.DaysInMonth(dateTime.Year,dateTime.Month));
}


  //  获取最后一天 
今日DateTime = DateTime.Now;
int lastDay = DateTime.DaysInMonth(today.Year,today.Month); // 31


// 获取上次日期
DateTime firstDate = new DateTime (today.Year,today.Month, 1 );
DateTime lastDate = firstDate.AddMonths( 1 )。AddDays(-1); // {07/31/2016 00:00:00}


please i will like to know how to get the last day of the month.

i am writing a code which needs me to provide an expiring date as a parameter, now the expiring date as to be the last date of the month.

i can get today date by doing this

DateTime now = DateTime.Today;



but how do i get the last date of the month with this.

thanks

What I have tried:

DateTime now = DateTime.Today

like i explained earlier...

i just need a way to get the last day of the month.

thank you.

解决方案

How is about Google: Google for "c# Last Day of Month"[^]

You will easely find it has to be something like this:

DateTime today = DateTime.Today;
int lastDay = DateTime.DaysInMonth(today.Year, today.Month);


If date is provided you can get last day in the month by below code.

public static DateTime GetLastDayOfMonth(this DateTime dateTime)
{
    return new DateTime(dateTime.Year, dateTime.Month, DateTime.DaysInMonth(dateTime.Year, dateTime.Month));
}


// To get Last Day
       DateTime today = DateTime.Now;
       int lastDay = DateTime.DaysInMonth(today.Year, today.Month); //31


       // To Get Last Date
       DateTime firstDate = new DateTime(today.Year, today.Month, 1);
       DateTime lastDate = firstDate.AddMonths(1).AddDays(-1); // {07/31/2016 00:00:00}


这篇关于如何获得该月的最后一天的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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