locked
Why does DateTime object not have a "Date and Time" property? RRS feed

  • Question

  • User-1188570427 posted

    I am cusious why the DateTime object does not have a "DateAndTime" property with it.

    It has .Date and .TimeOfDay, but not one that is combined.

    This can impact when you are binding to a property and you make a list like the below:

                List<DateTime> list = new List<DateTime>();
    
                list.Add(new DateTime(2015, 1, 4, 5, 0, 0));
                list.Add(new DateTime(2019, 1, 4, 8, 0, 0));
    
                var time = list.FirstOrDefault().TimeOfDay;
                var Date = list.FirstOrDefault().Date;
                var dateAndTime = list.FirstOrDefault().ToString();
    

    So to get the FULL Date and Time I have to to a .ToString(), but I can't do that specifically in when binding because it is requesting a property. 

    To get around this, I have to make a new Class with a DateTime property within it and bind do that property. 

    IE:

        public class MyDateTime
        {
            public DateTime MyProperty { get; set; }
        }

    Thursday, January 3, 2019 11:09 AM

Answers

  • User475983607 posted

    tvb2727

    Okay, so I can't use just List<DateTIme> since it doesn't have a full .DateTime property on it? It has Date and TimeOfDay

    It is certainly possible bind a list of dates to a drop down for example because a List<T> implements ICollection.  

    It is much easier to help if you post sample code that illustrates the problem.  I assume the problem is simply formatting the DateTime.  The bound controls have the ability to format type.  Just read the docs...

    https://docs.microsoft.com/en-us/dotnet/framework/winforms/how-to-create-a-bound-control-and-format-the-displayed-data

    Keep in mind your original post is trying to bind a single DateTime type.

        public class MyDateTime
        {
            public DateTime MyProperty { get; set; }
        }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, January 3, 2019 1:21 PM
  • User-1716253493 posted

    DateAndTime is not property. It's module containing procedure and properties such as  

    DateTime dt = DateAndTime.DateAdd("m",1,DateAndTime.Now);

    dt has Date and Time together already

    You can get Date and Time part, but dt has both.

    var time = list.FirstOrDefault().TimeOfDay;
    var Date = list.FirstOrDefault().Date;
    var dateAndTime = list.FirstOrDefault();

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, January 3, 2019 1:29 PM
  • User-1188570427 posted

    DateAndTime is not property. It's module containing procedure and properties such as  

    DateTime dt = DateAndTime.DateAdd("m",1,DateAndTime.Now);

    dt has Date and Time together already

    You can get Date and Time part, but dt has both.

    var time = list.FirstOrDefault().TimeOfDay;
    var Date = list.FirstOrDefault().Date;
    var dateAndTime = list.FirstOrDefault();

    Thanks.

    That will work.

    If you ever do it in WPF here is an answer as well: 

    https://social.msdn.microsoft.com/Forums/vstudio/en-US/504c13f6-0d42-4c0d-856c-e3b5f0bb9bf8/why-does-datetime-object-not-have-a-quotdate-and-timequot-property?forum=wpf

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, January 3, 2019 2:21 PM

All replies

  • User475983607 posted

    So to get the FULL Date and Time I have to to a .ToString()

    Correct and by design.  You are confusing a DateTime type with a string.

    but I can't do that specifically in when binding because it is requesting a property.

    If you are referring to a data bound control then you are correct that's how bound controls works.  The data source is a type and the bound field resolve to a property name.

    Thursday, January 3, 2019 12:43 PM
  • User-1188570427 posted

    tvb2727

    So to get the FULL Date and Time I have to to a .ToString()

    Correct and by design.  You are confusing a DateTime type with a string.

    tvb2727

    but I can't do that specifically in when binding because it is requesting a property.

    If you are referring to a data bound control then you are correct that's how bound controls works.  The data source is a type and the bound field resolve to a property name.

    Okay, so I can't use just List<DateTIme> since it doesn't have a full .DateTime property on it? It has Date and TimeOfDay

    Thursday, January 3, 2019 1:13 PM
  • User475983607 posted

    tvb2727

    Okay, so I can't use just List<DateTIme> since it doesn't have a full .DateTime property on it? It has Date and TimeOfDay

    It is certainly possible bind a list of dates to a drop down for example because a List<T> implements ICollection.  

    It is much easier to help if you post sample code that illustrates the problem.  I assume the problem is simply formatting the DateTime.  The bound controls have the ability to format type.  Just read the docs...

    https://docs.microsoft.com/en-us/dotnet/framework/winforms/how-to-create-a-bound-control-and-format-the-displayed-data

    Keep in mind your original post is trying to bind a single DateTime type.

        public class MyDateTime
        {
            public DateTime MyProperty { get; set; }
        }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, January 3, 2019 1:21 PM
  • User-1716253493 posted

    DateAndTime is not property. It's module containing procedure and properties such as  

    DateTime dt = DateAndTime.DateAdd("m",1,DateAndTime.Now);

    dt has Date and Time together already

    You can get Date and Time part, but dt has both.

    var time = list.FirstOrDefault().TimeOfDay;
    var Date = list.FirstOrDefault().Date;
    var dateAndTime = list.FirstOrDefault();

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, January 3, 2019 1:29 PM
  • User753101303 posted

    Hi,

    Which binding approach do you use ? You can use annotations to tell how a particular property should be formatted. Keep in mind you have to make the difference between the "native" value and how it is shown.

    You even have for decimal values. As developer we see which value is 1.234 but in some countries it should be shown as 1,234 (and in countries where , is the thousand separator it could be read as 1234).

    In short the problem is that as developers we are used to writing code that works everywhere. Still each country can have its own way and shows differently the SAME value.

    Edit: seems definitively some confusion between the actual value and how this value is shown. IMO you should have a closer look at formatting capabilities for the binding approach you are using.

    Thursday, January 3, 2019 2:19 PM
  • User-1188570427 posted

    DateAndTime is not property. It's module containing procedure and properties such as  

    DateTime dt = DateAndTime.DateAdd("m",1,DateAndTime.Now);

    dt has Date and Time together already

    You can get Date and Time part, but dt has both.

    var time = list.FirstOrDefault().TimeOfDay;
    var Date = list.FirstOrDefault().Date;
    var dateAndTime = list.FirstOrDefault();

    Thanks.

    That will work.

    If you ever do it in WPF here is an answer as well: 

    https://social.msdn.microsoft.com/Forums/vstudio/en-US/504c13f6-0d42-4c0d-856c-e3b5f0bb9bf8/why-does-datetime-object-not-have-a-quotdate-and-timequot-property?forum=wpf

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, January 3, 2019 2:21 PM