zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    禅知Pro 1.6 前台任意文件读取 | 代码审计
    wpa破解学习实践
    Natural Merge Sort(自然归并排序)
    [转]the service mysql57 failed the most recent status[/br]mysql57 was not found解决办法
    《Metasploit魔鬼训练营》第七章学习笔记
    Adobe阅读器漏洞(adobe_cooltype_sing)学习研究
    MS10_087漏洞学习研究
    第三方插件渗透攻击之KingView
    《Metasploit魔鬼训练营》虚拟环境搭建中网络配置的一些问题
    KingView 6.53漏洞学习研究
  • 原文地址:https://www.cnblogs.com/xielong/p/9383058.html
Copyright © 2011-2022 走看看