C#开发笔记之15

Wesley13
• 阅读 313

本文由 比特飞 原创发布,欢迎大家踊跃转载。

转载请注明本文地址:https://www.byteflying.com/archives/3412

C#开发笔记概述

概述

本文介绍使用C#获取某月最大天数的4种方法,欢迎大家批评指正。

解释

public static class MonthHelper {

    public static int GetMaxDays(DateTime date) {
        //使用Calendar类获取某月最大天数
        var days = Thread.CurrentThread.CurrentUICulture.Calendar.GetDaysInMonth(date.Year, date.Month);
        return days;
    }

    public static int GetMaxDays2(DateTime date) {
        //使用下个月第1天的日期减去1天来计算某月最大天数
        var days = date.AddDays(1 - date.Day).AddMonths(1).AddDays(-1).Day;
        return days;
    }

    public static int GetMaxDays3(DateTime date) {
        //使用DaysInMonth静态方法获取某月最大天数
        var days = DateTime.DaysInMonth(date.Year, date.Month);
        return days;
    }

    public static int GetMaxDays4(DateTime date) {
        //使用下个月第1天和当月第1天的时间差来计算某月最大天数
        var d1 = new DateTime(date.Year, date.Month, 1);

        var nextMonth = date.AddMonths(1);
        var d2 = new DateTime(nextMonth.Year, nextMonth.Month, 1);

        var days = (d2 - d1).Days;
        return days;
    }

}

本文由 比特飞 原创发布,欢迎大家踊跃转载。

转载请注明本文地址:https://www.byteflying.com/archives/3412

点赞
收藏
评论区
推荐文章
blmius blmius
2年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
Wesley13 Wesley13
2年前
PPDB:今晚老齐直播
【今晚老齐直播】今晚(本周三晚)20:0021:00小白开始“用”飞桨(https://www.oschina.net/action/visit/ad?id1185)由PPDE(飞桨(https://www.oschina.net/action/visit/ad?id1185)开发者专家计划)成员老齐,为深度学习小白指点迷津。
Stella981 Stella981
2年前
Opencv中Mat矩阵相乘——点乘、dot、mul运算详解
Opencv中Mat矩阵相乘——点乘、dot、mul运算详解2016年09月02日00:00:36 \牧野(https://www.oschina.net/action/GoToLink?urlhttps%3A%2F%2Fme.csdn.net%2Fdcrmg) 阅读数:59593
Wesley13 Wesley13
2年前
HTTPS学习笔记
笔记详细地址:http://note.youdao.com/yws/public/redirect/share?id4882fca3838541908c75c92c92d28b74&typefalse(https://www.oschina.net/action/GoToLink?urlhttp%3A%2F%2Fnote.youdao.com%2F
Stella981 Stella981
2年前
Flash Player的终章——赠予它的挽歌
本文由葡萄城技术团队原创并首发转载请注明出处:葡萄城官网(https://www.oschina.net/action/GoToLink?urlhttps%3A%2F%2Fwww.grapecity.com.cn%2F),葡萄城为开发者提供专业的开发工具、解决方案和服务,赋能开发者。12月28日消息,微软已经确认Windows10
Stella981 Stella981
2年前
Golang注册Eureka的工具包goeureka发布
1.简介提供Go微服务客户端注册到Eureka中心。点击:github地址(https://www.oschina.net/action/GoToLink?urlhttps%3A%2F%2Fgithub.com%2FSimonWang00%2Fgoeureka),欢迎各位多多star!(已通过测试验证,用于正式生产部署)2.原理
Stella981 Stella981
2年前
Google地球出现“无法连接到登录服务器(错误代码:c00a0194)”解决方法
Google地球出现“无法连接到登录服务器(错误代码:c00a0194)”解决方法参考文章:(1)Google地球出现“无法连接到登录服务器(错误代码:c00a0194)”解决方法(https://www.oschina.net/action/GoToLink?urlhttps%3A%2F%2Fwww.codeprj.com%2Fblo
Wesley13 Wesley13
2年前
MySQL部分从库上面因为大量的临时表tmp_table造成慢查询
背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_
Stella981 Stella981
2年前
C++笔记002:VS2010报错:LINK fatal error LNK1123 转换到 COFF 期间失败文件无效或损坏
 原创笔记,转载请注明出处!点击【关注】,关注也是一种美德~错误描述:1已启动生成:项目:FirstCode,配置:DebugWin321生成启动时间为2018/2/521:00:30。1InitializeBuildStatus:1 正在
Python进阶者 Python进阶者
3个月前
Excel中这日期老是出来00:00:00,怎么用Pandas把这个去除
大家好,我是皮皮。一、前言前几天在Python白银交流群【上海新年人】问了一个Pandas数据筛选的问题。问题如下:这日期老是出来00:00:00,怎么把这个去除。二、实现过程后来【论草莓如何成为冻干莓】给了一个思路和代码如下:pd.toexcel之前把这