关于时间的问题! 还有一个问题。如何在一个label上显示动态时间?? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 很普通的数学变换了啊如果你让你的自定义时间能和系统时间同步的话,你只需要让系统时间减去一个时间差来得到你的自定义时间,而这个时间差就等于你的某个特定自定义时间的时候的系统时间与特定时间之差这样就可以和系统时间同步了。DateTime CustomedTime(){return DateTime.Now-TimeDeltal;}你每次要获得这个自定义时间就调用CustomedTime函数。这个TimeDelta是TimeSpan类型,可以通过某一特定的系统时间减去当时的自定义时间来得到 你可以添加一个Timer控件,然后修改Interval(默认的100足矣),然后再Tick事件里:Label1.Text=String.Format("现在的时间是:{0}",DateTime.Now.ToString()); 例如你的自定义时间永远比当前系统时间多1天2小时3分钟4秒5微妙,那么定义时间差TimeSpan span=new TimeSpan(1,2,3,4,5);在Timer中:Label1.Text=String.Format("现在的自定义时间是:{0}",(DateTime.Now+span).ToString()); 调整嘛,调整那个span就可以了 你从文本框来获取数据,然后通过获取的数据来设置相应的时间差(那个span)就可以了啊 客户端的动态时间只能在客户端用脚本实现,自定义时间的例子:YourDate.ToString("dddd - d - MMMM"); Regex regex=new Regex("^([0-9]+):([0-9]+):([0-9]+):([0-9]+):([0-9])+$",RegexOptions.SingleLine);Match m=regex.Match(textBox1.Text);if(!m.Success){ MessageBox.Show("请按照这个格式输入时间差 天:小时:分钟:秒:微妙 !"); return;}int Days,Hours,Minutes,Seconds,Milliseconds;Days=int.Parse(m.Group[1].Value);Hours=int.Parse(m.Group[2].Value);Minutes=int.Parse(m.Group[3].Value);Seconds=int.Parse(m.Group[4].Value);Milliseconds=int.Parse(m.Group[5].Value);span=new TimeSpan(Days,Hours,Minutes,Seconds,Milliseconds);未测试 在这里看到你的帖子,立刻做了一个,看是简单,也费了一点功夫。我试着用DateTime类对象,可是也不行,只好自己做个模拟的时间。如果能为你所用,我很高兴!如果需要的话,给我你的邮箱,我给你把原程序发过去!具体步骤如下(WinForm中): 在窗体中拖入两个timer时间控件timer1,timer2,三个文本框,一个Button按钮!删除所有控件的text属性值。具体代码如下: 1.先在类中声明变量private int year,month,day,hour,minute,second; 2.方法代码: private void timer1_Tick(object sender, System.EventArgs e) { this.label1.Text=DateTime.Now.ToString(); } private void button1_Click(object sender, System.EventArgs e) { year=DateTime.Now.Year; month=DateTime.Now.Month; day=DateTime.Now.Day; hour=Convert.ToInt32(this.textBox1.Text); minute=Convert.ToInt32(this.textBox2.Text); second=Convert.ToInt32(this.textBox3.Text);// DateTime dt=new DateTime(year,month,day,hour,minute,second); this.label2.Text=year+"-"+month+"-"+day+" "+hour+":"+minute+":"+second; this.timer2.Enabled=true; this.timer2.Interval=1000; } private void timer2_Tick(object sender, System.EventArgs e) { second++; if(second>=60) { second=second-60; minute++; if(minute>=60) { minute=minute-60; hour++; if(hour>=24) { hour=hour-24; } } } this.label2.Text=year+"-"+month+"-"+day+" "+hour+":"+minute+":"+second; } private void Form1_Load(object sender, System.EventArgs e) { this.timer1.Interval=1000; this.timer1.Enabled=true; }试验过,还不错!时间显示方式自己润色吧!我要休息一会了!呵呵! 太感谢了,请把原程序发给我吧。我的有箱是[email protected] private void button1_Click(object sender, System.EventArgs e) {// // //用正则表达式来判断// if(dt.Text != "")// {///^\s*((0?|1)\d|2[0-3])\s*:\s*(0?|[1-5])\d\s*(:\s*(0?|[1-5])\d(\.\d{3})?)?\s*$/;// string strPatternPhone=@"^\s*((0?|1)\d|2[0-3])\s*:\s*(0?|[1-5])\d\s*(:\s*(0?|[1-5])\d(\.\d{3})?)?\s*$";// Regex reg=new Regex(strPatternPhone);// Match mh=reg.Match(dt.Text);// if(!mh.Success)// {// MessageBox.Show("时间格式不正确","info");// dt.Focus();// return;// }// } if(this.textBox5.Text != "") {///^\s*((0?|1)\d|2[0-3])\s*:\s*(0?|[1-5])\d\s*(:\s*(0?|[1-5])\d(\.\d{3})?)?\s*$/; string strPatternPhone=@"^\s*((0?|1)\d|2[0-3])\s*:\s*(0?|[1-5])\d\s*(:\s*(0?|[1-5])\d(\.\d{3})?)?\s*$"; Regex reg=new Regex(strPatternPhone); Match mh=reg.Match(this.textBox5.Text+":"+this.textBox6.Text+":"+this.textBox2.Text); if(!mh.Success) { MessageBox.Show("时间格式不正确","info"); dt.Focus(); return; } } } 未将对象引用设置到对象的实例 几个关于C#的问题 用互斥体实现一个程序的单开 C#文件上传对大侠们的小问题。 用正则表达式得到两个字符串之间的内容 ArrayList怎么快速去掉重复项? 捕获/设置窗体以外的鼠标事件 C#与命名管道 关于类中成员以及方法的排列问题 C# 调用DELPHI 结构体指针数组出现的乱码 如何同步datagrid数据显示 vs.net安装项目
如果你让你的自定义时间能和系统时间同步的话,你只需要让系统时间减去一个时间差来得到你的自定义时间,而这个时间差就等于你的某个特定自定义时间的时候的系统时间与特定时间之差
这样就可以和系统时间同步了。DateTime CustomedTime()
{
return DateTime.Now-TimeDeltal;
}
你每次要获得这个自定义时间就调用CustomedTime函数。这个TimeDelta是TimeSpan类型,可以通过某一特定的系统时间减去当时的自定义时间来得到
Label1.Text=String.Format("现在的时间是:{0}",DateTime.Now.ToString());
TimeSpan span=new TimeSpan(1,2,3,4,5);在Timer中:
Label1.Text=String.Format("现在的自定义时间是:{0}",(DateTime.Now+span).ToString());
自定义时间的例子:
YourDate.ToString("dddd - d - MMMM");
Match m=regex.Match(textBox1.Text);
if(!m.Success)
{
MessageBox.Show("请按照这个格式输入时间差 天:小时:分钟:秒:微妙 !");
return;
}int Days,Hours,Minutes,Seconds,Milliseconds;
Days=int.Parse(m.Group[1].Value);
Hours=int.Parse(m.Group[2].Value);
Minutes=int.Parse(m.Group[3].Value);
Seconds=int.Parse(m.Group[4].Value);
Milliseconds=int.Parse(m.Group[5].Value);
span=new TimeSpan(Days,Hours,Minutes,Seconds,Milliseconds);未测试
在窗体中拖入两个timer时间控件timer1,timer2,三个文本框,一个Button按钮!删除所有控件的text属性值。具体代码如下:
1.先在类中声明变量private int year,month,day,hour,minute,second;
2.方法代码:
private void timer1_Tick(object sender, System.EventArgs e)
{
this.label1.Text=DateTime.Now.ToString();
} private void button1_Click(object sender, System.EventArgs e)
{
year=DateTime.Now.Year;
month=DateTime.Now.Month;
day=DateTime.Now.Day;
hour=Convert.ToInt32(this.textBox1.Text);
minute=Convert.ToInt32(this.textBox2.Text);
second=Convert.ToInt32(this.textBox3.Text);
// DateTime dt=new DateTime(year,month,day,hour,minute,second);
this.label2.Text=year+"-"+month+"-"+day+" "+hour+":"+minute+":"+second;
this.timer2.Enabled=true;
this.timer2.Interval=1000;
} private void timer2_Tick(object sender, System.EventArgs e)
{
second++;
if(second>=60)
{
second=second-60;
minute++;
if(minute>=60)
{
minute=minute-60;
hour++;
if(hour>=24)
{
hour=hour-24;
}
}
}
this.label2.Text=year+"-"+month+"-"+day+" "+hour+":"+minute+":"+second;
} private void Form1_Load(object sender, System.EventArgs e)
{
this.timer1.Interval=1000;
this.timer1.Enabled=true;
}试验过,还不错!时间显示方式自己润色吧!我要休息一会了!呵呵!
我的有箱是[email protected]
{
//
// //用正则表达式来判断
// if(dt.Text != "")
// {///^\s*((0?|1)\d|2[0-3])\s*:\s*(0?|[1-5])\d\s*(:\s*(0?|[1-5])\d(\.\d{3})?)?\s*$/;
// string strPatternPhone=@"^\s*((0?|1)\d|2[0-3])\s*:\s*(0?|[1-5])\d\s*(:\s*(0?|[1-5])\d(\.\d{3})?)?\s*$";
// Regex reg=new Regex(strPatternPhone);
// Match mh=reg.Match(dt.Text);
// if(!mh.Success)
// {
// MessageBox.Show("时间格式不正确","info");
// dt.Focus();
// return;
// }
// }
if(this.textBox5.Text != "")
{///^\s*((0?|1)\d|2[0-3])\s*:\s*(0?|[1-5])\d\s*(:\s*(0?|[1-5])\d(\.\d{3})?)?\s*$/;
string strPatternPhone=@"^\s*((0?|1)\d|2[0-3])\s*:\s*(0?|[1-5])\d\s*(:\s*(0?|[1-5])\d(\.\d{3})?)?\s*$";
Regex reg=new Regex(strPatternPhone);
Match mh=reg.Match(this.textBox5.Text+":"+this.textBox6.Text+":"+this.textBox2.Text);
if(!mh.Success)
{
MessageBox.Show("时间格式不正确","info");
dt.Focus();
return;
}
}
}