C#中对日期及时间的操作

            string s1 = "19700101";
            string s2 = dtpNow.Value.ToString("d"); //dtpNow是个dateTimePicker 控件
            DateTime DateTime1 = DateTime.ParseExact(s1, "yyyymmdd", System.Globalization.CultureInfo.CurrentCulture);
//将"19700101"转换成日期注意格式 "yyyymmdd"
            DateTime DateTime2 = DateTime.ParseExact(s2, "d", System.Globalization.CultureInfo.CurrentCulture);
            TimeSpan ts = DateTime2 - DateTime1; //两个日期间隔的时间(日期)
            lab3.Text = ts.Days.ToString(); //取天数

你可能感兴趣的:(String,C#)