Reputation: 99
As in title. I write a easy timer , which set time and date . Time works well , but date not. It shows a year and a day , but not month . Why ?
My code:
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)
{
}
}
}
Result of this code :
time eg : 11:34:23
date eg : 20--2013
result must change to :
time eg : 11:34:23
date eg : 20/01/2013
Could you help me how to solve this problem ?
Upvotes: 1
Views: 209
Reputation: 98750
Your DateTime
formatting is wrong. Try with dd/MM/yy
format like this;
label2.Text = DateTime.Now.ToString("dd/MM/yy");
Check out Custom Date and Time Format Strings
for more date and time format informations.
Here is a DEMO
.
Upvotes: 1
Reputation: 7737
You should use dd/MM/yy
not dd/tt/yy
.
See Custom Date and Time Format Strings for more information.
I can also see you have the following line:
int s = DateTime.Now.Minute;
I assume you meant:
int s = DateTime.Now.Second;
EDIT:
In fact, I can't see any reason why you are saving any portions of the date/time in local variables as they aren't being used.
Upvotes: 2