• C#代码中如何比较两个日期的大小?


    C#中比较两个日期大小的方法

           /// <summary>
            /// 比较两个日期大小
            /// </summary>
            /// <param name="dateStr1">日期1</param>
            /// <param name="dateStr2">日期2</param>
            /// <param name="msg">返回信息</param>
            public void CompanyDate(string dateStr1, string dateStr2,ref string msg) {
                //将日期字符串转换为日期对象
                DateTime t1 = Convert.ToDateTime(dateStr1);
                DateTime t2 = Convert.ToDateTime(dateStr2);
                //通过DateTIme.Compare()进行比较()
                int compNum = DateTime.Compare(t1, t2);
    
                //t1> t2
                if (compNum > 0) {
                    msg = "t1:(" + dateStr1 + ")大于" + "t2(" + dateStr2 + ")";              
                }
                //t1= t2
                if (compNum == 0) {
                    msg = "t1:(" + dateStr1 + ")等于" + "t2(" + dateStr2 + ")";
                }
                //t1< t2
                if (compNum < 0) {
                    msg = "t1:(" + dateStr1 + ")小于" + "t2(" + dateStr2 + ")";
                }
            }

    调用的代码

            public ActionResult Index()
            {
                //t1< t2
                string msg1 = string.Empty;
                string date1 = "2018-07-25";
                string date2 = "2018-07-27";
                CompanyDate(date1,date2,ref msg1);
                
                //t1> t2
                string msg2 = string.Empty;
                string date3 = "2018-07-27";
                string date4 = "2018-07-25";
                CompanyDate(date3, date4, ref msg2);
    
                return View();
            }

    整体demo源码

     public class TestController : Controller
        {
            // GET: Test
            public ActionResult Index()
            {
                //t1< t2
                string msg1 = string.Empty;
                string date1 = "2018-07-25";
                string date2 = "2018-07-27";
                CompanyDate(date1,date2,ref msg1);
                
                //t1> t2
                string msg2 = string.Empty;
                string date3 = "2018-07-27";
                string date4 = "2018-07-25";
                CompanyDate(date3, date4, ref msg2);
    
                return View();
            }
    
            /// <summary>
            /// 比较两个日期大小
            /// </summary>
            /// <param name="dateStr1">日期1</param>
            /// <param name="dateStr2">日期2</param>
            /// <param name="msg">返回信息</param>
            public void CompanyDate(string dateStr1, string dateStr2,ref string msg) {
                //将日期字符串转换为日期对象
                DateTime t1 = Convert.ToDateTime(dateStr1);
                DateTime t2 = Convert.ToDateTime(dateStr2);
                //通过DateTIme.Compare()进行比较()
                int compNum = DateTime.Compare(t1, t2);
    
                //t1> t2
                if (compNum > 0) {
                    msg = "t1:(" + dateStr1 + ")大于" + "t2(" + dateStr2 + ")";              
                }
                //t1= t2
                if (compNum == 0) {
                    msg = "t1:(" + dateStr1 + ")等于" + "t2(" + dateStr2 + ")";
                }
                //t1< t2
                if (compNum < 0) {
                    msg = "t1:(" + dateStr1 + ")小于" + "t2(" + dateStr2 + ")";
                }
            }
        }

    具体效果图:

    1)图1

    2)图2

  • 相关阅读:
    [洛谷 U68862] 奶牛滑迷宫 题解
    STL的妙用(二)——洛谷 P2073 送花
    平衡树 x 01-trie √
    最小生成树(大纲,待补全)
    单源最短路算法
    黑科技:如何提高整数域内高斯消元的精度和速度——高斯消元与辗转相除法的结合
    Scratch的入门笔记
    Ubuntu18.04安装Tensorflow
    Ubuntu18.04安装英伟达显卡驱动
    macOS下appstore提示未能完成该操作的解决办法
  • 原文地址:https://www.cnblogs.com/xielong/p/9383058.html
Copyright © 2020-2023  润新知