asp.net 時間迴圈比較 CompareTo用法
阿新 • • 發佈:2019-02-06
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace CodeTest.test8 { public partial class WebForm6 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { string inputTime = "2014-1-1"; if (!string.IsNullOrEmpty(inputTime)) { DateTime thisDateNextDay = Convert.ToDateTime(inputTime).AddDays(7);//每天迴圈7天資料等待被插入,防止一次性抓谷歌資料異常 DateTime thisEndTime = Convert.ToDateTime("2014-5-17");//結束時間 if (thisDateNextDay.CompareTo(thisEndTime) <= 0) {//限制要插入的資料 for (DateTime time = Convert.ToDateTime(inputTime); time < thisEndTime; ) { if (time.CompareTo(thisDateNextDay) < 0) { //Response.Write(time.ToShortDateString() + " " + time.AddDays(1).ToShortDateString() + "</br>"); Response.Write(time.ToShortDateString() +"</br>"); } else { break; } time = time.AddDays(1); } } } } } }