• 日期


    日期和字符串一样,都不能改变自身的值
    1            System.DateTime d = System.DateTime.Now;
    2            System.Console.WriteLine(d.AddDays(1));
    3            System.Console.WriteLine(d.AddHours(1));
    4            System.Console.WriteLine(d.AddMilliseconds(1));
    5            System.Console.WriteLine(d.AddMinutes(1));
    6            System.Console.WriteLine(d.AddMonths(1));
    7            System.Console.WriteLine(d.AddYears(1));
    8            System.Console.WriteLine(d);//d没有改变值
    9            d = d.AddYears(1);//需要重新赋值

    一些关于日期的算法要牢记

    1            System.Console.WriteLine(d.AddDays(-(d.Day-1)));//本月第一天
    2            System.Console.WriteLine(d.AddDays(-d.Day));//上月最后一天
    3            System.Console.WriteLine(d.AddDays(-(int)d.DayOfWeek));//本周第一天

    ok,我们来看一个例子:

     1            System.DateTime dt = new System.DateTime(1949101);
     2            System.Console.WriteLine(dt.ToLongDateString());
     3            System.DateTime d = System.DateTime.Now;
     4            int toDay = d.Day;
     5            int thisMonth = d.Month;
     6            System.Console.WriteLine("当前日期:{0}", d.Date);
     7            d = d.AddDays(-(toDay - 1));
     8            System.Console.WriteLine("该月第一天:{0}", d.Date);
     9
    10            do
    11            {
    12                string toDayFlag = "";
    13                if (d.Day == toDay)
    14                {
    15                    toDayFlag = "*";
    16                }

    17                System.Console.WriteLine("{0}:{1}{2}", d.Day, d.DayOfWeek, toDayFlag);
    18                d = d.AddDays(1);
    19            }

    20            while (d.Month == thisMonth);

    上面例子输出本月的所有日期和星期,并在当日标注*

    那么开始巩固知识:

    设一企业合同约定每月5号发工资,请输出该企业今年实际每月发工资的日期(若5号是周六或周日,则工资提前到周五发)

     1            System.DateTime dt = System.DateTime.Now;
     2            dt = dt.AddDays(-(dt.DayOfYear - 1));
     3            System.Console.WriteLine("该年第一天:{0}", dt.Date);
     4            dt = dt.AddDays(4);
     5            int iDay = 0;
     6            System.Console.WriteLine("定位到第一个月的5号:{0}", dt.Date);
     7            do
     8            {
     9                if (dt.Month != 5 && dt.Month != 10)
    10                {
    11                    if (dt.DayOfWeek == System.DayOfWeek.Saturday)
    12                    { iDay = -1; }
    13                    if (dt.DayOfWeek == System.DayOfWeek.Sunday)
    14                    { iDay = -2; }
    15                }

    16                else
    17                { iDay = -5; }
    18                System.Console.WriteLine("{0}月的发薪水日期是{1}",
    19                                dt.Month, dt.AddDays(iDay).Date);
    20                dt = dt.AddMonths(1);
    21                iDay = 0;
    22            }

    23            while (dt.Year == System.DateTime.Now.Year);


    设每年9.1开学,若9.1不是星期一,则要求开学日期改为9月的第一个星期一,请输出从1980-2004年的每年开学日期

     1            //方案1
     2            System.DateTime dt = new System.DateTime(194991);
     3            System.DateTime tmpdt;
     4            do
     5            {
     6                if (dt.DayOfWeek == System.DayOfWeek.Monday)
     7                { tmpdt = dt; }
     8                else
     9                {
    10                    if (dt.DayOfWeek == System.DayOfWeek.Sunday)
    11                    { tmpdt = dt.AddDays(1); }
    12                    else
    13                    { tmpdt = dt.AddDays(7 - (int)dt.DayOfWeek + 1); }
    14                }

    15                System.Console.WriteLine("{0}年的9.1是{1}", dt.Year, dt.DayOfWeek);
    16                System.Console.WriteLine("所以实际开学日期是:{0}", tmpdt.Date);
    17                System.Console.WriteLine("------------------------");
    18                dt = dt.AddYears(1);
    19            }

    20            while (dt.Year <= System.DateTime.Now.Year);
    21
    22
    23            //方案2
    24            System.DateTime dt = new System.DateTime(194991);
    25            System.DateTime tmpdt;
    26            do
    27            {
    28                if ((int)dt.DayOfWeek < 2)
    29                { tmpdt = dt.AddDays(1 - (int)dt.DayOfWeek); }
    30                else
    31                {
    32                    tmpdt = dt.AddDays(7 - (int)dt.DayOfWeek + 1);
    33                }

    34                System.Console.WriteLine("{0}年的9.1是{1}", dt.Year, (int)dt.DayOfWeek);
    35                System.Console.WriteLine("所以实际开学日期是:{0}", tmpdt.Date);
    36                System.Console.WriteLine("------------------------");
    37                dt = dt.AddYears(1);
    38            }

    39            while (dt.Year <= System.DateTime.Now.Year);

    注意方案2的第29行,使用了01互换的方法,简化了周日和周一的计算


    计算给定日期的年的每个月的周数

     1            System.DateTime tmpdt = new DateTime(year, 11);
     2            int tmpMonth = tmpdt.Month;//记录当前月
     3            int[] weeks = new int[12];//每个元素存放每月的星期数量
     4            do
     5            {
     6                do
     7                {//如果是周六就加1
     8                    if (tmpdt.DayOfWeek == System.DayOfWeek.Saturday)
     9                    {
    10                        weeks[tmpMonth - 1= weeks[tmpMonth - 1+ 1
    11                    }

    12                    tmpdt = tmpdt.AddDays(1);//下一天
    13                }

    14                while (tmpdt.Month == tmpMonth);//月循环
    15                if (tmpdt.Day == 1 && tmpdt.DayOfWeek != System.DayOfWeek.Sunday)
    16                {//如果每月的第一天不是周日,则上月再加一周
    17                    weeks[tmpMonth - 1= weeks[tmpMonth - 1+ 1;
    18                }

    19                tmpMonth = tmpdt.Month;
    20            }

    21            while (tmpdt.Year == year);//年循环
    22            for (int i = 0; i <= weeks.Length - 1; i++)
    23            {
    24                System.Console.WriteLine("{0}年 {1}月有{2}周", year, i + 1, weeks[i]);
    25            }
  • 相关阅读:
    查看Oracle连接数 限制某个用户的连接数
    (原创网上办法经过改良)系统重装后,如何快速的回复oracle 10g(测试环境:windows server 2003 sp1+Oracle 10g)
    Response.Redirect报"正在中止进程"异常的处理
    快速使网页变灰色
    .NET创建Windows服务[转]
    [收集]javascript中得到当前窗口的高和宽
    [转帖]javascript版 UrlEncode和UrlDecode函数
    js异步读取xml(支持ff和xpath)
    辞职考研后记
    BCB6 E2494 Unrecognized __declspec modifier
  • 原文地址:https://www.cnblogs.com/shyleoking/p/635500.html
Copyright © 2020-2023  润新知