Vibrant discussion about CSLA .NET and using the framework to build great business applications.

Forum has moved

New location: CSLA .NET forum

CSLA .NET Resources:
  • CSLA .NET forum
  • CSLA .NET home page
  • SmartDate Question: Appears to be truncating time portion.

    Answered (Not Verified) This post has 0 verified answers | 6 Replies | 3 Followers

    Not Ranked
    2 Posts
    skeeler posted on Fri, May 3 2013 12:44 PM

    I have a ReadOnlyList and a BusinessBase class that contains Event information something like this.



            public static readonly PropertyInfo<SmartDate> StartProperty = RegisterProperty<SmartDate>(p => p.Start, "Start");

            public string Start


                get { return GetPropertyConvert<SmartDate, String>(StartProperty); }

                set { SetPropertyConvert<SmartDate, String>(StartProperty, value); }


            public static readonly PropertyInfo<SmartDate> EndProperty = RegisterProperty<SmartDate>(p => p.End, "End");

            public string End


                get { return GetPropertyConvert<SmartDate, String>(EndProperty); }

                set { SetPropertyConvert<SmartDate, String>(EndProperty, value); }


    When the entity is Created with a value that contains both a date and time via the BusinessBase object the time is stored correctly in the database.
    When the application retrieves the objects & values via SafeDataReader and into my ReadOnlyList into the Mvc4 application it seems to have truncated the time portion of the date.
    What am I doing wrong? Any suggestions?



    All Replies

    Top 25 Contributor
    498 Posts

    Hi skeeler,

    I also noticed SmartDate just shows the date. After all it's SmartDate and not SmartDateTime

    For simple auditing purposes - i.e. CreatedOn / ChangedOn - I use DateTime and set the same value on both properties when the object is created. As these properties can never be null I don't need any "smartness" here.

    Tiago Freitas Leal, CslaGenFork (Open Source CSLA code generator)

    Not Ranked
    2 Posts

    They are going to be a nullable datetime. I guess I'll have to just do the ladder.

    Thanks for the help.

    Top 10 Contributor
    2,279 Posts


    SmartDate supports format strings and the default format is"d".
    Se also Standard Date and Time Format strings

    Try  this code:

          var sd = new SmartDate();
          Debug.Print("default format: {0}", sd.FormatString);
          sd.FormatString = "g";
          Debug.Print("new format: {0}", sd.FormatString);
          sd.Text = DateTime.Now.ToString("g");

    Gives this result on my culture:

    default format: d
    new format: g
    04.05.2013 12:08
    04.05.2013 12:08

    when the datetime format is changed to "g" = General date/time pattern (short time).

    Jonny Bekkum, Norway CslaContrib Coordinator

    Top 200 Contributor
    45 Posts

    Hi Jonny. I have this special case where I need to select Date + Time as a property for user input.

    On my BusinessBase constructed class, I declare the property like this:

    private static PropertyInfo<SmartDate> StartDateTimeProperty = RegisterProperty<SmartDate>(c => c.StartDateTime, "Start Date & Time"new SmartDate(DateTime.Now));
    public string StartDateTime
    		var tempSD = GetProperty(StartDateTimeProperty);
    		tempSD.FormatString = "G";
    		string retValue = tempSD.Text;
    		return retValue;
    		//return GetPropertyConvert<SmartDate, string>(StartDateTimeProperty); 
    	set { SetPropertyConvert<SmartDatestring>(StartDateTimeProperty, value); }

    This is working fine but I think there may be a better way consisting of somehow being able to assign [StartDateTimeProperty.FormatString] without deriving a new [struct] from SmartDate. It would be more elegant. Is there any way of doing this?



    Top 10 Contributor
    2,279 Posts
    Suggested by JonnyBee


    You can specify the custom format in RegisterProperty:

    private static PropertyInfo<SmartDate> StartDateTimeProperty = 
    RegisterProperty(c => c.StartDateTime, "Start Date & Time"
     SmartDate(DateTime.Now) {FormatString = "G"});

    Jonny Bekkum, Norway CslaContrib Coordinator

    Top 200 Contributor
    45 Posts
    Troncho replied on Wed, Jun 10 2015 10:35 AM

    Nice! I knew there had to be a better way Smile

    Once again, thanks Jonny!

    Page 1 of 1 (7 items) | RSS

    Copyright (c) 2006-2014 Marimer LLC. All rights reserved.
    Email admin@lhotka.net for support.
    Powered by Community Server (Non-Commercial Edition), by Telligent Systems