0

I'm creating a scheduler and I'm getting the value of the dates from a database. When I display the dates in a combo box it adds " 12:00:00 a.m" to all the values at the end of the dates for example "1/06/2016 12:00:00 a.m." instead of just "1/06/2016" the 12 am is not shown with the values when displayed in the database or in a data grid and i tried to replace slots.replace but that didn't work

 DataTable day = new DataTable();
 foreach (DataRow db in timeslots.Tables["schedules"].Rows)
 {
    string slot = db["sdates"].ToString();
    if (!comboBox2.Items.Contains(slot))
    {
        comboBox2.Items.Add(db["sdate"].ToString());
    }
 }
Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
ToKeen
  • 3
  • 2

6 Answers6

1

Since C# doesn't have a Date datatype. All the db date values are converted into DateTime. You can always format your output to get only the date part.

In WPF/Data binding you can directly use the StringFormat property:

StringFormat={}{0:dd-MM-yyyy}

while in coding you can use String.Format like:

String.Format({0:MM-dd-yyyy}, x.Date);
Failed Scientist
  • 1,977
  • 3
  • 29
  • 48
1

Since the db["sdate"] is a DateTime object you can Specify the Format along with the .ToString() as like the following, which will give you the result in the expected format:

string DateOnlyString= db["sdate"].ToString("dd/MM/yyyy");
if (!comboBox2.Items.Contains(DateOnlyString))
{
   comboBox2.Items.Add(DateOnlyString);
}

Take a look into more formatting options here, Alternatively you can collect only Date From the database using proper CAST or CONVERT functions, So that additional formatting can be avoided and you can use the result directly

Community
  • 1
  • 1
sujith karivelil
  • 28,671
  • 6
  • 55
  • 88
  • when i use that i get the error 'No overlord method for 'Tostring' takes 1 argument – ToKeen Jun 01 '16 at 01:10
  • @ToKeen: which means that the `db["sdate"]` is already a string; So to make it work you need to convert it as DateTime then to String, Instead of this repeated works, You can use query to get the result – sujith karivelil Jun 01 '16 at 01:22
1

By default the DateTime struct on C# displays 12:00:00 AM if you don't set any time value. If you want just date part, use varname.Date (assume varname is your DateTime variable).

Or use String.Format to ensure it has correct format:

var date = String.Format({0:MM/dd/yyyy}, varname.Date);
Tetsuya Yamamoto
  • 24,297
  • 8
  • 39
  • 61
1

Try this:

DataTable day = new DataTable();
foreach (DataRow db in timeslots.Tables["schedules"].Rows)
{
    string slot = ((DateTime)db["sdates"]).ToShortDateString();
    if (!comboBox2.Items.Contains(slot))
    {
        comboBox2.Items.Add(slot);
    }
}

As others said, dates from db are converted as DateTime, and DateTime already contains a function to get as an string just the date part, ToShortDateString();

sujith karivelil
  • 28,671
  • 6
  • 55
  • 88
Gusman
  • 14,905
  • 2
  • 34
  • 50
0

I think the best option here is to simply try to convert it to DateTime and make sure it's valid. Then you can call the ToShortDateString method on it.

        DataTable day = new DataTable();

        foreach (DataRow db in timeslots.Tables["schedules"].Rows)
        {
            string slot = db["sdates"].ToString();
            DateTime dt = DateTime.MinValue;
            var isValidDateTime = DateTime.TryParse(slot, out dt);


            if (!comboBox2.Items.Contains(slot))
            {
                if(isValidDateTime && dt != DateTime.MinValue)
                {
                    comboBox2.Items.Add(dt.ToShortDateString());
                }

            }
        }
prospector
  • 3,389
  • 1
  • 23
  • 40
  • If db["sdates"] comes from db it's already a DateTime, so why you're converting it to string and then again to datetime? also, as you're using dt as an output parameter it doesn't needs to be initialized. – Gusman Jun 01 '16 at 01:12
  • @Gusman He had it converted to a string, not me I just added what he had and showed him how to do it. I changed the example though, but you are wrong that dt doesn't need to be initialized. – prospector Jun 01 '16 at 01:37
  • no, I'm not wrong, you can test it, any variable used as out parameter in a function doesn't needs to be initialized as the function must intialize obligately the out parameter. – Gusman Jun 01 '16 at 01:38
  • Maybe you should test it. Also you have to take into account that the slot can be null. – prospector Jun 01 '16 at 01:40
  • 1-if slot is datetime it cannot be null, 2-again, no, there's no need to initialize it, I'm bored of doing it, you can check it, the compiler sees you're using the var as output parameter and doesn't complains about it not being initialized. – Gusman Jun 01 '16 at 01:41
  • Yes I can be null from the Database, if you initialize a DateTime in C# then it can't be, it would always be DateTime.MinValue. – prospector Jun 01 '16 at 01:43
  • So you're stating what I said, if it's a datetime it cannot be null. – Gusman Jun 01 '16 at 01:44
  • But if it comes the the database it can be null if nulls are allowed and if it's not converted to DateTime object. – prospector Jun 01 '16 at 01:57
  • .net retrieves the data types from the table definition, not row by row, so if the field type is datetime it will be always datetime, and thus, a datetime cannot be null, what you stated first was right, if it's null then it will be DateTime.MinValue, so there's no need to check it. Also, your code is using db["sdates"] directly which will throw an exception as it will be a DateTime, not an string, so you must add a ToString at the end, what will also throw an exception if the value was null. – Gusman Jun 01 '16 at 02:01
0
  <ComboBox ItemsSource="{Binding StartTimes}">
            <ComboBox.ItemTemplate>
              <DataTemplate>
                <TextBlock Text="{Binding StringFormat='dd.MMMM-yyyy'}" />
              </DataTemplate>
            </ComboBox.ItemTemplate>
  </ComboBox>