Hi everyone!
My problem is when the clock is for example 02.04.45, i cannot show number 0 with the code below. it always shows 2, not 02 and 4, not 04... how can i fix this?...
|
MessageBox.Show(DateTime.Now.Hour.ToString());
MessageBox.Show(DateTime.Now.Minute.ToString()); _____________________________________________________ it didnt work if i write it with these:
if ((Convert.ToString(DateTime.Now.Minute.ToString("mm")) == Convert.ToString(dak_textBox.Text).ToString())
{ if ((Convert.ToString(DateTime.Now.Hour.ToString("hh") == Convert.ToString(saat_textBox.Text))){ System.Diagnostics. Process.Start("shutdown", "-f -s");} } i have to write a program which is switched off by the time the user want... |
yusufPosted Nov 21, 2007, 6:45 AM
i solved this problem by using just two txtbox'es. I'll write how i solved because that would be some people who are going to use it in the future. :
private void timer1_Tick(object sender, EventArgs e){
textBox1.Text =
DateTime.Now.ToLongTimeString(); if (textBox2.Text == textBox1.Text){
System.Diagnostics.
Process.Start("shutdown", "-f -s");}
}
yusufPosted Nov 21, 2007, 5:58 AM
Jan MontanoPosted Nov 20, 2007, 9:51 PM
if ((Convert.ToString(DateTime.Now.Minute.ToString("mm")) == Convert.ToString(dak_textBox.Text).ToString())
{
if ((Convert.ToString(DateTime.Now.Hour.ToString("hh") == Convert.ToString(saat_textBox.Text)))
{
System.Diagnostics.Process.Start("shutdown", "-f -s");
}
}
Please correct me if I'm wrong. I'm assuming dak_textBox.Text = 2-digit minutes and saat_textBox.Text = 2-digit hours. Now you're comparing it with the current hour and current minute and if it matches it will call System.Diagnostics.Process.Start("shutdown","-f-s");
DateTime.Now.Hour.ToString("hh") will be equal to "hh" always regardless of the current hour.
DateTime.Now.Hour will be equal to the numeric value of the current hour.
DateTime.Now.Hour.ToString() will be equal to the string value of the current hour.
DateTime.Now.Minute.ToString("mm") will be equal to "mm" always regardless of the current minute.
DateTime.Now.Minute will be equal to the numeric value of the current minute.
DateTime.Now.Minute.ToString() will be equal to the string value of the current minute.
The Convert.ToString() basically is called to convert a non-string value to a string. So if the value to be converted is already a string, you don't need to call this anymore.
DateTime.Now will be equal to the DateTime value of the current date & time.
DateTime.Now.ToString() will be equal to the string value of the current date & time.
ToString() method is overloaded and takes in certain parameters as format of the result you want it to be.
Let's assume it's November 21, 2007 9 hours 51 minutes 16 seconds in the evening.
Calling DateTime.Now.ToString() will result in "11/21/2007 9:51:16 PM"
If we call DateTime.Now.ToString("hh") instead, it will pick just the hour part of the current DateTime = "09"
If we call DateTime.Now.ToString("mm") instead, it will pick just the minute part of the current DateTime = "51"
This link about String formatting in c# provides different formats for the Datetime.
You could change your condition instead to:
if (DateTime.Now.ToString("mm") == dak_textBox.Text)
{
if (DateTime.Now.ToString("hh") == saat_textBox.Text)
{
System.Diagnostics.Process.Start("shutdown", "-f -s");
}
}
Hope this helps.
Cheers,
Jan
Jan MontanoPosted Nov 19, 2007, 9:26 PM
MessageBox.Show(DateTime.Now.Hour.ToString("00"));
MessageBox.Show(DateTime.Now.Minute.ToString("00"));
or better yet use:
MessageBox.Show(DateTime.Now.ToString("hh")); // for 2-digit hours
MessageBox.Show(DateTime.Now.ToString("mm")); // for 2-digit minutes