在点网框架工作2
如何更好地使用和易于使用我有很多次
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string timenow = DateTime.Now.ToString("T");
Label1.Text = DateTime.Now.ToString("T");
Label3.Text = DateTime.Now.ToString("D");
Label2.Text = getTimeZone(timenow);
}
//hh:mm:ss
protected string getTimeZone(string time)
{
string[] e = time.Split(':');
int h = Convert.ToInt16(e[0]);
int m = Convert.ToInt16(e[1]);
int s = Convert.ToInt16(e[2]);
if ((h == 15) && (m >= 1) && (m <= 30) && (s <= 59))
{
return "15.01 - 15.30";
}
if ((h == 15) && (m >= 31) && (m <= 45) && (s <= 59))
{
return "15.31 - 15.45";
}
if ((h == 15) && (m >= 46) && (m == 0) && (s <= 59))
{
return "15.31 - 15.45";
}
return time;
}
}
答案 0 :(得分:9)
DateTime now = DateTime.Now;
DateTime start = new DateTime(now.Year, now.Month, now.Day, 15, 31, 0);
DateTime end = new DateTime(now.Year, now.Month, now.Day, 15, 45, 0);
if (now >= start && now <= end)
Console.WriteLine("We have a go!");
答案 1 :(得分:2)
这样的事情:
protected void Page_Load()
{
DateTime timenow = DateTime.Now;
Label2.Text = getTimeZone(timenow);
}
//hh:mm:ss
protected string getTimeZone(DateTime time)
{
if (time.Hour == 15 && time.Minute >= 1 && time.Minute <= 30)
return "15.01 - 15.30";
if (time.Hour == 15 && time.Minute >= 31 && time.Minute <= 45)
return "15.31 - 15.45";
return "";
}
答案 2 :(得分:1)
为什么直接使用字符串而不是DateTime,可以使用DateTime.TimeofDay
答案 3 :(得分:1)
如果所有时间都采用相同的模式,这将有效:
protected void Page_Load()
{
DateTime timenow = DateTime.Now;
Label1.Text = timenow.ToString("T");
Label3.Text = timenow.ToString("D");
Label2.Text = getTimeZone(timenow);
}
//hh:mm:ss
protected string getTimeZone(DateTime time)
{
if (time.Minute >= 1 && time.Minute <= 30)
return String.Format("{0:00}.01 - {0:00}.30", time.Hour);
if (time.Minute >= 31 && time.Minute <= 45)
return String.Format("{0:00}.31 - {0:00}.45", time.Hour);
if (time.Minute >= 46)
return String.Format("{0:00}.46 - {1:00}.00", time.Hour, (time.Hour < 23) ? time.Hour+1 : 0);
if (time.Minute == 00)
return String.Format("{0:00}.46 - {1:00}.00", (time.Hour > 0) ? time.Hour-1 : 23, time.Hour);
return "";
}
答案 4 :(得分:0)
好吧,我将getTimeZone从页面类中取出并将它放在一个util类中以便开始......
答案 5 :(得分:0)
您可以将字符串解析为DateTime对象并进行比较。 您还可以使用string.CompareTo()直接比较字符串。
答案 6 :(得分:0)
protected void Page_Load(object sender, EventArgs e)
{
DateTime now = DateTime.Now;
Label1.Text = now.ToString("T");
Label3.Text = now.ToString("D");
if (now.Hour == 15 && now.Minute >= 1 && now.Minute <= 30)
Label2.Text = "15.01 - 15.30";
else if (now.Hour == 15 && now.Minute >= 31 && now.Minute <= 45)
Label2.Text = "15.31 - 15.45";
else
Label2.Text = now.ToString("T");
}