C#--日期大小比较

static void Main(string[] args)
        {
            string date1 = "2019-07-26";
            string date2 = "2019-07-27";
            CompanyDate(date1, date2);
            
            string date3 = "2018-07-27";
            string date4 = "2018-07-25";
            CompanyDate(date3, date4);
        }
        public static void CompanyDate(string value1, string value2)
        {
            //将日期字符串转换为日期对象
            DateTime t1 = Convert.ToDateTime(value1);
            DateTime t2 = Convert.ToDateTime(value2);
            //通过DateTIme.Compare()进行比较()
            int compNum = DateTime.Compare(t1, t2);

            if (compNum > 0)
            {
                //t1 > t2
            }
            if (compNum == 0)
            {
                //t1 = t2
            }
            if (compNum < 0)
            {
                //t1 < t2
            }
        }

 

posted @ 2019-08-08 17:10  初晨~  阅读(2032)  评论(0编辑  收藏  举报