locked
Nullabe DateTime

    Question

  • I have a class in a C# component that is returned to javascript.  The class has a property 

    public DateTime? DueOn { get; set; }

    This throws a build error:

    Error 1 Method 'TaskViewModel.DueOn.get()' returns 'System.Nullable<System.DateTime>', which is not a valid Windows Runtime type.  Methods exposed to Windows Runtime must only return Windows Runtime types. C:\projects\Engine\Repository.cs 432 34 Engine

    Is there a work around this issue?  This property is a date and can be null, so I am not sure how I would expose it to javascript...  as an object?


    www.emadibrahim.com

    Wednesday, May 16, 2012 9:41 PM

Answers

  • Hi,

      >> I think this thread answers my question

    The issue you mentioned is a bit different. Yes, we cannot use DateTime for public members in a custom WinRT component, because it is not a WinRT type. But in addition, nullable type is also a .NET concept. It is not a WinRT concept. There’s no nullable type in JavaScript or C++. When building a WinRT component, we can use WinRT types for public members. Please refer to http://msdn.microsoft.com/en-us/library/windows/apps/br230301(v=vs.110).aspx for more details.

      >> Is there a work around this issue?  This property is a date and can be null, so I am not sure how I would expose it to javascript...  as an object?

    I would like to suggest you to use a WinRT compatible type. For example, you can return the Ticks property of the DateTime, which is of type long. A long type in .NET can be automatically converted to a WinRT type.

    Best Regards,

    Ming Xu.


    Please mark the replies as answers if they help or unmark if not.
    If you have any feedback about my replies, please contact msdnmg@microsoft.com.
    Microsoft One Code Framework

    Thursday, May 17, 2012 9:03 AM
    Moderator
  • I ended up creating my own custom date, so I can easily pass it back and forth between winrt and javascript...

        public sealed class MyDateTime
        {
            public int Year { get; set; }
            public int Month { get; set; }
            public int Day { get; set; }
            public int Hour { get; set; }
            public int Minute { get; set; }
    
            public string ToDisplayDate()
            {
                var dt = ToDateTime();
                if (dt == null) return string.Empty;
                var value = dt.Value;
    
                if (value.Year == DateTime.Now.Year && value.Month == DateTime.Now.Month && value.Day == DateTime.Now.Day)
                {
                    return "Today";
                }
                var tomorrow = DateTime.Now.AddDays(1);
                if (value.Year == tomorrow.Year && value.Month == tomorrow.Month && value.Day == tomorrow.Day)
                {
                    return "Tomorrow";
                }
    
                return ToDateString();
    
            }
            public string ToDateString()
            {
                if (ToDateTime() == null) return string.Empty;
                return string.Format("{0}/{1}/{2}", Month, Day, Year);
            }
    
            internal DateTime? ToDateTime()
            {
                try
                {
                    return new DateTime(Year, Month, Day, Hour, Minute, 0);
                }
                catch (Exception)
                {
                    return null;
                }
            }
    
            public override string ToString()
            {
                try
                {
                    return ToDateTime().ToString();
                }
                catch (Exception)
                {
                    return string.Empty;
                }
            }
        }


    www.emadibrahim.com

    Friday, May 18, 2012 9:15 PM

All replies

  • Wednesday, May 16, 2012 9:47 PM
  • Hi,

      >> I think this thread answers my question

    The issue you mentioned is a bit different. Yes, we cannot use DateTime for public members in a custom WinRT component, because it is not a WinRT type. But in addition, nullable type is also a .NET concept. It is not a WinRT concept. There’s no nullable type in JavaScript or C++. When building a WinRT component, we can use WinRT types for public members. Please refer to http://msdn.microsoft.com/en-us/library/windows/apps/br230301(v=vs.110).aspx for more details.

      >> Is there a work around this issue?  This property is a date and can be null, so I am not sure how I would expose it to javascript...  as an object?

    I would like to suggest you to use a WinRT compatible type. For example, you can return the Ticks property of the DateTime, which is of type long. A long type in .NET can be automatically converted to a WinRT type.

    Best Regards,

    Ming Xu.


    Please mark the replies as answers if they help or unmark if not.
    If you have any feedback about my replies, please contact msdnmg@microsoft.com.
    Microsoft One Code Framework

    Thursday, May 17, 2012 9:03 AM
    Moderator
  • I ended up creating my own custom date, so I can easily pass it back and forth between winrt and javascript...

        public sealed class MyDateTime
        {
            public int Year { get; set; }
            public int Month { get; set; }
            public int Day { get; set; }
            public int Hour { get; set; }
            public int Minute { get; set; }
    
            public string ToDisplayDate()
            {
                var dt = ToDateTime();
                if (dt == null) return string.Empty;
                var value = dt.Value;
    
                if (value.Year == DateTime.Now.Year && value.Month == DateTime.Now.Month && value.Day == DateTime.Now.Day)
                {
                    return "Today";
                }
                var tomorrow = DateTime.Now.AddDays(1);
                if (value.Year == tomorrow.Year && value.Month == tomorrow.Month && value.Day == tomorrow.Day)
                {
                    return "Tomorrow";
                }
    
                return ToDateString();
    
            }
            public string ToDateString()
            {
                if (ToDateTime() == null) return string.Empty;
                return string.Format("{0}/{1}/{2}", Month, Day, Year);
            }
    
            internal DateTime? ToDateTime()
            {
                try
                {
                    return new DateTime(Year, Month, Day, Hour, Minute, 0);
                }
                catch (Exception)
                {
                    return null;
                }
            }
    
            public override string ToString()
            {
                try
                {
                    return ToDateTime().ToString();
                }
                catch (Exception)
                {
                    return string.Empty;
                }
            }
        }


    www.emadibrahim.com

    Friday, May 18, 2012 9:15 PM