none
handle datetime textbox

    Question

  • how can i type the ddmmyyyy HHss that the system will recongite to dd/mm/yyyy HH:ss

    automatically add the separator as '/' between day,month and yyyy and add ':' between HH and ss

    Friday, September 24, 2010 3:35 AM

Answers

  • hi,

    try

    if (e.KeyData != Keys.Back)
     {        
        if (textBox1.Text.Length == 2)
        {
           textBox1.AppendText("/");
        }
        if (textBox1.Text.Length == 5)
        {
           textBox1.AppendText("/");
        }
        if (textBox1.Text.Length == 10)
        {
           textBox1.AppendText(" ");
        }
        if (textBox1.Text.Length == 13)
        {
           textBox1.AppendText(":");
        }
        if (textBox1.Text.Length == 16)
        {
           textBox1.AppendText(":");
        }
     }
    


    Nagarjuna Dilip
    • Marked as answer by --_-- Monday, September 27, 2010 9:09 AM
    Monday, September 27, 2010 7:25 AM

All replies

  • hi,

    string strDate = DateTime.Now.ToString("dd/MM/yyyy hh:mm:ss"); //out put 24/09/2010 10:05:10

    string strDate = DateTime.Now.ToString("ddMMyyyy hhmmss"); //out put 24092010 100510

    jst have a look at this link...........

    http://www.csharp-examples.net/string-format-datetime/


     


    Nagarjuna Dilip
    Friday, September 24, 2010 4:34 AM
  • hi,

    string strDate = DateTime.Now.ToString("dd/MM/yyyy hh:mm:ss"); //out put 24/09/2010 10:05:10

    string strDate = DateTime.Now.ToString("ddMMyyyy hhmmss"); //out put 24092010 100510

    jst have a look at this link...........

    http://www.csharp-examples.net/string-format-datetime/


     


    Nagarjuna Dilip

    can i type first 2 digit then add a separator '/' then contineous to type thrid..forth digit for month in the textbox or any control can do this effect.
    Friday, September 24, 2010 4:39 AM
  • hi,

    i think this is not best practice to do........but, itz 1 sort of solution to solve this

    void textBox1_KeyUp(object sender, KeyEventArgs e)
        {
          if (textBox1.Text.Length == 2)
          {
            textBox1.AppendText("/");        
          }
          if (textBox1.Text.Length == 5)
          {
            textBox1.AppendText("/");
          }
          if (textBox1.Text.Length == 10)
          {
            textBox1.AppendText(" ");
          }
          if (textBox1.Text.Length == 13)
          {
            textBox1.AppendText(":");
          }
          if (textBox1.Text.Length == 16)
          {
            textBox1.AppendText(":");
          }
        }

    //out put 24/09/2010 10:05:10

     


    Nagarjuna Dilip
    Friday, September 24, 2010 5:28 AM
  • hi,

    i think this is not best practice to do........but, itz 1 sort of solution to solve this

     

    void textBox1_KeyUp(object sender, KeyEventArgs e)
    
      {
    
       if (textBox1.Text.Length == 2)
    
       {
    
        textBox1.AppendText("/");    
    
       }
    
       if (textBox1.Text.Length == 5)
    
       {
    
        textBox1.AppendText("/");
    
       }
    
       if (textBox1.Text.Length == 10)
    
       {
    
        textBox1.AppendText(" ");
    
       }
    
       if (textBox1.Text.Length == 13)
    
       {
    
        textBox1.AppendText(":");
    
       }
    
       if (textBox1.Text.Length == 16)
    
       {
    
        textBox1.AppendText(":");
    
       }
    
      }
    
    

     

    //out put 24/09/2010 10:05:10

     


    Nagarjuna Dilip


    when i type worng date and use backspace,it can't retype the date. how to handle?

    Monday, September 27, 2010 6:27 AM
  • hi,

    try

    if (e.KeyData != Keys.Back)
     {        
        if (textBox1.Text.Length == 2)
        {
           textBox1.AppendText("/");
        }
        if (textBox1.Text.Length == 5)
        {
           textBox1.AppendText("/");
        }
        if (textBox1.Text.Length == 10)
        {
           textBox1.AppendText(" ");
        }
        if (textBox1.Text.Length == 13)
        {
           textBox1.AppendText(":");
        }
        if (textBox1.Text.Length == 16)
        {
           textBox1.AppendText(":");
        }
     }
    


    Nagarjuna Dilip
    • Marked as answer by --_-- Monday, September 27, 2010 9:09 AM
    Monday, September 27, 2010 7:25 AM
  • Normally the CDate will do that confirm your culture

    Dim TheDate = CDate("YourDateTimeString)

     


    Success
    Cor
    Monday, September 27, 2010 7:53 AM