C# 定时器don';我一个月都没来
如标题所示。我写了一个简单的计时器,可以设置时间和日期。时间很有效,但日期不行。 它显示的是一年零一天,但不是一个月。为什么? 我的代码:C# 定时器don';我一个月都没来,c#,winforms,timer,C#,Winforms,Timer,如标题所示。我写了一个简单的计时器,可以设置时间和日期。时间很有效,但日期不行。 它显示的是一年零一天,但不是一个月。为什么? 我的代码: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.T
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication_timer
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void timer1_Tick(object sender, EventArgs e)
{
int h = DateTime.Now.Hour;
int m = DateTime.Now.Minute;
int s = DateTime.Now.Minute;
int d = DateTime.Now.Day;
int y = DateTime.Now.Year;
int t = DateTime.Now.Month;
label2.Text = DateTime.Now.ToString("dd/tt/yy");
label1.Text = DateTime.Now.ToString("hh:mm:ss");
}
private void button1_Click(object sender, EventArgs e)
{
timer1.Start();
}
private void label2_Click(object sender, EventArgs e)
{
}
}
}
此代码的结果:
time eg : 11:34:23
date eg : 20--2013
结果必须更改为:
time eg : 11:34:23
date eg : 20/01/2013
你能帮我解决这个问题吗?你应该使用
dd/MM/yy
而不是dd/tt/yy
有关更多信息,请参阅
我还可以看到您有以下行:
int s=DateTime.Now.Minute代码>
我想你的意思是:
int s=DateTime.Now.Second代码>
编辑:
事实上,我看不出为什么要将日期/时间的任何部分保存在局部变量中,因为它们没有被使用。您的DateTime
格式错误。试试这样的dd/MM/yy
格式
label2.Text = DateTime.Now.ToString("dd/MM/yyyy");
label2.Text = DateTime.Now.ToString("dd/MM/yy");
查看更多日期和时间格式信息
这是一本书