我有一个应用程序,它显示特定区域代码的时区时间,该区号的时间以及将根据从上午8点到晚上8点的时间显示的消息。我遇到的问题是让应用程序显示基于区号而不是PC上当前时间显示的时间消息。我知道我的代码不正确,因为它使用了
DateTime.Now
而不是我的数字时钟标签的时间,而是由特定区域代码的时区选择。
这是我的代码,问题出在我解析DateTime的if语句中。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace TimeZone
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void areaCodeDataBaseTableBindingNavigatorSaveItem_Click(object sender, EventArgs e)
{
this.Validate();
this.areaCodeDataBaseTableBindingSource.EndEdit();
this.tableAdapterManager.UpdateAll(this.areaCodeDataBaseDataSet);
}
private void Form1_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the 'areaCodeDataBaseDataSet.AreaCodeDataBaseTable' table.
//You can move, or remove it, as needed.
this.areaCodeDataBaseTableTableAdapter.Fill(this.areaCodeDataBaseDataSet.AreaCodeDataBaseTable);
}
private void DigitalClock_Load(object sender, EventArgs e)
{
Timer t = new Timer();
t.Tick += new EventHandler(t_Tick);
t.Interval = 1000;
t.Enabled = true;
}
public void t_Tick(Object Sender, EventArgs e)
{
DateTime start = DateTime.Parse("08:00 am");
DateTime end = DateTime.Parse("08:00 pm").AddHours(10);
if (time_ZoneLabel1.Text == "Eastern")
{
DigitalClock.Text = DateTime.UtcNow.AddHours(19).ToLongTimeString();
DigitalClock2.Text = "";
}
if (time_ZoneLabel1.Text == "Central")
{
DigitalClock.Text = DateTime.UtcNow.AddHours(18).ToLongTimeString();
DigitalClock2.Text = "";
}
if (time_ZoneLabel1.Text == "Mountain")
{
DigitalClock.Text = DateTime.UtcNow.AddHours(17).ToLongTimeString();
DigitalClock2.Text = "";
}
if (time_ZoneLabel1.Text == "Pacific")
{
DigitalClock.Text = DateTime.UtcNow.AddHours(16).ToLongTimeString();
DigitalClock2.Text = "";
}
if (time_ZoneLabel1.Text == "Atlantic")
{
DigitalClock.Text = DateTime.UtcNow.AddHours(20).ToLongTimeString();
DigitalClock2.Text = "";
}
if (time_ZoneLabel1.Text == "Hawaii-Aleutian")
{
DigitalClock.Text = DateTime.UtcNow.AddHours(14).ToLongTimeString();
DigitalClock2.Text = "";
}
if (time_ZoneLabel1.Text == "Eastern/Central")
{
DigitalClock.Text = DateTime.UtcNow.AddHours(19).ToLongTimeString();
DigitalClock2.Text = DateTime.UtcNow.AddHours(18).ToLongTimeString();
}
if (time_ZoneLabel1.Text == "Central/Mountain")
{
DigitalClock.Text = DateTime.UtcNow.AddHours(18).ToLongTimeString();
DigitalClock2.Text = DateTime.UtcNow.AddHours(17).ToLongTimeString();
}
if (time_ZoneLabel1.Text == "Mountain/Pacific")
{
DigitalClock.Text = DateTime.UtcNow.AddHours(17).ToLongTimeString();
DigitalClock2.Text = DateTime.UtcNow.AddHours(16).ToLongTimeString();
}
if (time_ZoneLabel1.Text == "UTC+10")
{
DigitalClock.Text = DateTime.UtcNow.AddHours(10).ToLongTimeString();
DigitalClock2.Text = "";
}
if (time_ZoneLabel1.Text == "UTC-10")
{
DigitalClock.Text = DateTime.UtcNow.AddHours(20).ToLongTimeString();
DigitalClock2.Text = "";
}
if (time_ZoneLabel1.Text == "")
{
DigitalClock.Text = "";
DigitalClock2.Text = "";
}
if (DateTime.Parse(DigitalClock.Text) > start && (DateTime.Parse(DigitalClock.Text) < end))
{
warningLabel.ForeColor = Color.Green;
warningLabel.Text = "OK To Call";
}
else
{
warningLabel.ForeColor = Color.Red;
warningLabel.Text = "Do Not Call";
}
if (DigitalClock.Text == "")
{
warningLabel.Text = "";
}
}
private void exitButton_Click(object sender, EventArgs e)
{
Application.Exit();
}
}
} code here
答案 0 :(得分:0)
您正在使用不同的方法在不同的地方进行转换,这有点令人困惑。考虑重新编写代码,以便使用时区来确定偏移量,如下所示:
int HoursOffset = 0;
switch (time_ZoneLabel1)
{
case "Eastern" :
HoursOffset = 19;
break;
case "Central" :
HoursOffset = 18;
break;
// etc, for all your timezones
}
现在,在那个地方抽出时间:
DateTime currentLocalTime = DateTime.UtcNow.AddHours(HoursOffset);
然后您可以设置数字时钟文本:
DigitalClock.Text = currentLocalTime.ToLongTimeString();
现在,要确定您是否可以致电,您可以将Hour
的{{1}}值与开始和结束时间进行比较:
currentLocalTime
或者,您可以考虑使用DateTimeOffset来减少使用时区时遇到的一些混淆。