Powered by MSDN

US - English
NEW! Silverlight 5 is available Learn More

Store UTC Display Local Date Time RSS

2 replies

Last post Oct 24, 2010 08:14 PM by DonRule

(0)
  • DonRule

    DonRule

    Member

    117 Points

    119 Posts

    Store UTC Display Local Date Time

    Oct 24, 2010 01:23 AM | LINK

    There are many threads on this issue but I would appreciate a pointer to working code. What I want to do is to keep my dates in a database in UTC but display them in the client as local time. I wrote an IValueConverter that will convert the local value back and forth between Local and UTC but I can't seem to get data validation to work properly for the textbox that the user will enter data into (there are many threads on this issue too). The IValueConverter worked fine when a datepicker was validating input but for birthdates I think that a simple textbox will be easier for data entry.

    If anyone knows of a simple sample that has appropriate Domain metadata that will both convert and validate that would be very useful.

    Thanks,
    Don Rule

    Validation RIA DateTimeime IValueConverter

  • xlinspire

    xlinspire

    Member

    244 Points

    32 Posts

    Re: Store UTC Display Local Date Time

    Oct 24, 2010 03:27 AM | LINK

    Hi,

    maybe you have already done this, but ...  
      
        public class DateTimeUtc2LocalValueConverter : IValueConverter
        {

            public DateTimeUtc2LocalValueConverter()
            {

            }

            public DateTimeUtc2LocalValueConverter(string dateFormat)
            {
                DateFormat = dateFormat;
            }

            public string DateFormat { getset; }

            #region IValueConverter Members

            /// <summary>
            /// Modifies the source data before passing it to the target for display in the UI.
            /// </summary>
            /// <param name="value">The source data being passed to the target.</param>
            /// <param name="targetType">The <see cref="T:System.Type"/> of data expected by the target dependency property.</param>
            /// <param name="parameter">An optional parameter to be used in the converter logic.</param>
            /// <param name="culture">The culture of the conversion.</param>
            /// <returns>
            /// The value to be passed to the target dependency property.
            /// </returns>
            public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
            {
                DateTime valueUtc;
                if (value == null)
                {
                    return null;
                }
                else if (value is string && !DateTime.TryParse((string)value, out valueUtc))
                {
                    return null;
                }
                else if (!(value is DateTime || value is DateTime?))
                {
                    return null;
                }

                DateTime valueLocal = ((DateTime)value).ToLocalTime();

                if (targetType == typeof(string))
                {
                    return DateFormat == null ? valueLocal.ToString() : valueLocal.ToString(DateFormat);
                }
                else if (targetType == typeof(DateTime) || targetType == typeof(DateTime?))
                {
                    return valueLocal;
                }
                return null;
            }

            /// <summary>
            /// Modifies the target data before passing it to the source object.  This method is called only in <see cref="F:System.Windows.Data.BindingMode.TwoWay"/> bindings.
            /// </summary>
            /// <param name="value">The target data being passed to the source.</param>
            /// <param name="targetType">The <see cref="T:System.Type"/> of data expected by the source object.</param>
            /// <param name="parameter">An optional parameter to be used in the converter logic.</param>
            /// <param name="culture">The culture of the conversion.</param>
            /// <returns>
            /// The value to be passed to the source object.
            /// </returns>
            public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
            {

                DateTime valueLocal;

                if (value == null)
                {
                    return null;
                }
                else if (value is string && !DateTime.TryParse((string)value, out valueLocal))
                {
                    return null;
                }
                else if (!(value is DateTime || value is DateTime?))
                {
                    return null;
                }

                DateTime valueUtc = ((DateTime)value).ToUniversalTime();

                if (targetType == typeof(string))
                {
                    return DateFormat == null ? valueUtc.ToString() : valueUtc.ToString(DateFormat);
                }
                else if (targetType == typeof(DateTime) || targetType == typeof(DateTime?))
                {
                    return valueUtc;
                }

                return null;
            }

            #endregion
        }
    and 
     <TextBox Text="{Binding Date, Converter={StaticResource DateTimeUtc2LocalValueConverter}, Mode=TwoWay}" />
     
    Might be a good idea to force the user to enter the date in some format (perhaps with a mask 'DD-MM-YYYY'), 
    it becomes easier to handle converting from the converter side.
     
    Regards,
    Bruno Rocha
     
     
    Mark as Answer" if this post resolved your query
    innovmedia Saberes&Sabores Rir Até Cair
  • DonRule

    DonRule

    Member

    117 Points

    119 Posts

    Re: Store UTC Display Local Date Time

    Oct 24, 2010 08:14 PM | LINK

    I have a similar converter but your error checking is more robust. I like the dateformat and it might be nice to read if from the converter parameter.

    The issue that I am struggling with is the validation of the date. When the date sent for conversion is incorrect Ideally I would like to throw an exception that would tell the control that it was an invalid date. There is a lot written about this but essentially it doesn't work that way just yet.

    I tried a regular expression evaluation in the metadata but saw some odd behavior. When the value in the textbox had a valid value it would be converted to a valid datetime string. When the value was empty it was converted to null. When you changed the value from a valid date to an invalid string it simply left the value alone. So my regular expression evaluator would work in cases when there was an invalid value in the textbox.

    Thanks very much,
    Don