• C# 常用日期取得


    列举一下常用的日期取得方法

        static class DateTimeDemo
        {
            public static DateTime FirstDayOfMonth(this DateTime value)
            {
                return new DateTime(value.Year, value.Month, 1);
            }
            public static DateTime LastDayOfMonth(this DateTime value)
            {
                return value.FirstDayOfMonth().AddMonths(1).AddDays(-1);
            }
            public static DateTime LastSecondOfDay(this DateTime value)
            {
                return value.Date.AddDays(1).AddSeconds(-1);
            }
    
            public static DateTime FirstDayOfLastMonth(this DateTime value)
            {
                return value.FirstDayOfMonth().AddMonths(-1);
            }
            public static int DaysInMonth(this DateTime value)
            {
                return DateTime.DaysInMonth(value.Year, value.Month);
            }
    
            public static DateTime DayOfYear(int month, int day)
            {
                return new DateTime(DateTime.Now.Year, month, day);
            }
    
            public static DateTime DayOfNextMonth(this DateTime value, int month)
            {
                return value.AddMonths(month).AddDays(-1);
            }
    
            public static DateTime StartOfDay(this DateTime theDate)
            {
                return theDate.Date;
            }
    
            public static DateTime EndOfDay(this DateTime theDate)
            {
                return theDate.Date.AddDays(1).AddTicks(-1);
            }
    
            public static DateTime QuarterStart(this DateTime dt)
            {
                return dt.AddMonths(0 - (dt.Month - 1) % 3).AddDays(1 - dt.Day);
            }
    
            public static DateTime QuarterEnd(this DateTime dt)
            {
                return dt.QuarterStart().AddMonths(3).AddDays(-1);
            }
        }

    测试结果:

  • 相关阅读:
    四十八.监控概述 、 Zabbix基础 、 Zabbix监控服务
    123D
    bzoj3879
    bzoj1699
    LA6878
    uoj#149
    687C
    codeforces round #424 div2
    803E
    713C
  • 原文地址:https://www.cnblogs.com/teyigou/p/8556232.html
Copyright © 2020-2023  润新知