请教:获得某月最后一天的函数 任意一个月,我想得到这个月的最后一天这个数值,c#有这个函数吗? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 某个月最后一天不就是这一个月的天数?System.DataTime.DaysInMonth() 方法 using System;using System.Collections;public class MyClass{ public static void Main() { // 如 2005-11 月 String yearMonth = "2005-11"; DateTime dt = DateTime.Parse(yearMonth + "-01"); dt = dt.AddMonths(1); DateTime lastDay = dt.AddDays(-1); Console.WriteLine("最后一天是:{0}",lastDay.Day); Console.ReadLine(); } } DateTime dt = System.DateTime.Now; //获得当前日期 dt = dt.AddDays(1 - dt.Day); //得到当月的第一天日期dt = (dt.AddMonths(1)).AddDays(-1); //得到当月的最后一天日期 /// <summary>/// 获得某月最后一天的数值/// Returns:最后一天的数值/// </summary> /// <param name = "p_dTime"> 需要提取的时间 </param>/// <returns> 最后一天的数值 </returns>public int GetDay(DateTime p_dTime){ int intResult = 0; switch (p_dTime.Month.ToString()) { case "1": case "3": case "5": case "7": case "8": case "10": case "12": intResult = 31; break; case "4": case "6": case "9": case "11": intResult = 30; break; case "2": if ((Convert.ToInt16(p_dTime.Year) % 4 == 0) && (Convert.ToInt16(p_dTime.Year) % 100 != 0) || (Convert.ToInt16(p_dTime.Year) % 400 == 0)) intResult = 29; else intResult = 28; break; } return intResult;} int mydays =System.DateTime.DaysInMonth(2005,11) using System;using System.Collections;public class MyClass{ public static void Main() { // 如 2005-11 月 String yearMonth = "2005-11"; DateTime dt = DateTime.Parse(yearMonth + "-01"); dt = dt.AddMonths(1); DateTime lastDay = dt.AddDays(-1); Console.WriteLine("最后一天是:{0}",lastDay.Day); Console.ReadLine(); } } System.DateTime today = System.DateTime.Now;System.DateTime NextMonth= today.AddMonths(1);System.DateTime NextMonthFirstDay=System.DateTime(NextMonth.Yesr,NextMonth.Month,1)System.DateTime lastday = NextMonthFirstDay.AddDays(-1); DataTime.DaysInMonth() 方法已经有的方法为什么要自己写? System.DateTime.DaysInMonth(2005,8)//获取2005年8月的总天数,即最后一天 SELECT dateadd(ms,-3,DATEADD(mm, DATEDIFF(m,0,getdate())+1, 0)) 本月最后一天SELECT dateadd(ms,-3,DATEADD(mm, DATEDIFF(m,0,getdate())+1, 0)) DECLARE @dt datetimeSET @dt=GETDATE()SELECT DATEADD(Day,-1,CONVERT(char(8),DATEADD(Month,1,@dt),120)+'1') ==>最后一天这个数值难道不是这个月的总天数么?.NET里面有内置的方法不用为什么要自己写??? DataTime.DaysInMonth() 这应该可以吧?!*_* Date.DaysInMonth(Year(Now), Month(Now)) VSTO读取EXCEL里面的CONST C# 如何读取excel中单个单元格的数据 FtpWebRequest如何创建多级目录 picturebox绘图的问题! 请问在程序中如何调用某个Button事件 水晶报表问题 Tablelayoutpanel控件的诡异问题 类似自动完成的功能 c#做的web网页,360网页的css为啥无法刷新 有人问我一个timestamp问题,谁知道这个时间戳是什么格式? xpsp2装VS.NET2003碰到的问题 stringbuilder的使用问题
System.DataTime.DaysInMonth() 方法
using System.Collections;public class MyClass
{
public static void Main()
{
// 如 2005-11 月
String yearMonth = "2005-11";
DateTime dt = DateTime.Parse(yearMonth + "-01");
dt = dt.AddMonths(1);
DateTime lastDay = dt.AddDays(-1);
Console.WriteLine("最后一天是:{0}",lastDay.Day);
Console.ReadLine();
}
}
dt = (dt.AddMonths(1)).AddDays(-1); //得到当月的最后一天日期
/// 获得某月最后一天的数值
/// Returns:最后一天的数值
/// </summary>
/// <param name = "p_dTime"> 需要提取的时间 </param>
/// <returns> 最后一天的数值 </returns>
public int GetDay(DateTime p_dTime)
{
int intResult = 0;
switch (p_dTime.Month.ToString())
{
case "1":
case "3":
case "5":
case "7":
case "8":
case "10":
case "12":
intResult = 31;
break;
case "4":
case "6":
case "9":
case "11":
intResult = 30;
break;
case "2":
if ((Convert.ToInt16(p_dTime.Year) % 4 == 0) &&
(Convert.ToInt16(p_dTime.Year) % 100 != 0) ||
(Convert.ToInt16(p_dTime.Year) % 400 == 0))
intResult = 29;
else
intResult = 28;
break;
}
return intResult;
}
using System.Collections;public class MyClass
{
public static void Main()
{
// 如 2005-11 月
String yearMonth = "2005-11";
DateTime dt = DateTime.Parse(yearMonth + "-01");
dt = dt.AddMonths(1);
DateTime lastDay = dt.AddDays(-1);
Console.WriteLine("最后一天是:{0}",lastDay.Day);
Console.ReadLine();
}
}
System.DateTime NextMonth= today.AddMonths(1);
System.DateTime NextMonthFirstDay=System.DateTime(NextMonth.Yesr,NextMonth.Month,1)
System.DateTime lastday = NextMonthFirstDay.AddDays(-1);
SELECT dateadd(ms,-3,DATEADD(mm, DATEDIFF(m,0,getdate())+1, 0))
SET @dt=GETDATE()
SELECT DATEADD(Day,-1,CONVERT(char(8),DATEADD(Month,1,@dt),120)+'1')
难道不是这个月的总天数么?.NET里面有内置的方法不用为什么要自己写???
这应该可以吧?!
*_*