0

I have a Day- and Timepicker on my form that is supposed to only show me the time in this format:

01-May-13.

I've set the custom format in the properties to: dd/mm/yyyy but the value still shows the time when I load the data in a listbox (link).

This is the code I am using to display the data from my database:

  private void loadlist() // methode om de lijsten te laden (SELECT*)
    {
        listBox1.Items.Clear();  // Maakt listBox1 leeg
        listBox2.Items.Clear();  // Maakt listBox2 leeg
        listBox3.Items.Clear(); // Maakt listBox3 leeg
        listBox4.Items.Clear(); // Maakt listBox4 leeg
        cn.Open();                  // DB connectie openen
        cmd.CommandText = "SELECT * FROM tafel"; // Query
        dr = cmd.ExecuteReader();
        if (dr.HasRows)
        {
            while (dr.Read())
            {
                listBox1.Items.Add(dr[1].ToString());  // Voegt rows uit tbl in listbox
                listBox2.Items.Add(dr[2].ToString());  // en zet ze om in string
                listBox3.Items.Add(dr[3].ToString());
                listBox4.Items.Add(dr[4].ToString());
            }
        }
        cn.Close();
    }

enter image description here

My current format is: dd/MM/yyyy (and it shows: 10-Apr-2013 17:08 in the listBox)

I'm sorry if this is an easy question.. I couldn't find out how to do it.

David Asssad
  • 41
  • 1
  • 9
  • 2
    Please show how you're setting the format. Also note that "m" is minutes, not months - and "dd/MM/yyyy" isn't going to give you "01-May-13" anyway. Why have you got a listbox in the first place when you're also talking about a date/time picker? – Jon Skeet Apr 10 '13 at 15:06
  • Something like this should do it: `string dateOnly = DateTime.Now.ToString("dd-MM-yyyy");` Can you show us how you're setting the format? – tnw Apr 10 '13 at 15:07
  • shouldnt be dd-MMM-yy? – Prabhu Murthy Apr 10 '13 at 15:08
  • Hi, I am entering data into a database with the dateTimerpicker and then loading it into a listBox. I am using dd/MMM/yyyy now but it still shows me the time. – David Asssad Apr 10 '13 at 15:11
  • David, please **show us the code**. – tnw Apr 10 '13 at 15:11
  • What code exactly? I've updated the first post. – David Asssad Apr 10 '13 at 15:16
  • That still doesn't help. What are `dr[1], dr[2]` and so on? What are the type of values being returned from your query? – tnw Apr 10 '13 at 15:20
  • Sorry for not being clear. dr[1] and so on are rows in my table. If I just delete everything and start again.. how do I get the dateTimerpicker to only pass "01-Apr-2013" instead of "01-Apr-2013"? Thanks for your time. – David Asssad Apr 10 '13 at 15:22
  • OK, what you need to do, assuming dr[1] is returning a date string, is this: `DateTime.Parse(dr[1]).ToShortDateString());`. I've updated my answer with this as well. – tnw Apr 10 '13 at 15:26

2 Answers2

2

A couple different things to try.

string dateOnly = dateTimeVariable.ToString("dd-MM-yyyy");

or

dateTimeVariable.ToShortDateString();

Both should give you what you're looking for.

EDIT: From your code, try this now:

DateTime.Parse(dr[1]).ToShortDateString());
tnw
  • 13,521
  • 15
  • 70
  • 111
  • I tried adding it but it won't work. Pretty sure I'm replacing the code in the wrong place. Where do I enter the code to parse it? Thanks and sorry – David Asssad Apr 10 '13 at 15:35
  • @DavidAsssad, it's extremely difficult to help you without seeing all of your code and being able to debug it myself. Since I can't do that, you're going to have to take this information and figure it out for yourself. Any string in a Date or DateTime format that's returned from SQL can be parsed and outputted as only a date using the methods I've described here. There isn't much more I can do to help you. – tnw Apr 10 '13 at 15:38
  • I understand. Thanks for your time, I'm going to rewrite my code and see if I can get it working with your code. Thank you! – David Asssad Apr 10 '13 at 15:40
  • @DavidAsssad Would you mind accepting my answer if this solution works for you? – tnw Apr 10 '13 at 15:42
-1
Convert.ToDateTime(dr[1].ToString()).ToString("yyyy/MM/dd");

add this one

Gino4nx
  • 64
  • 1
  • 3
  • Thanks but it is not working for some reason (red line: No overload for method "ToString" takes 1 argument" – David Asssad Apr 10 '13 at 15:25
  • That's because Gino4nx is incorrectly assuming `dr[1]` returns a `DateTime` object from a SQL query. – tnw Apr 10 '13 at 15:26