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.
public DateTimeUtc2LocalValueConverter(string dateFormat) { DateFormat = dateFormat; }
publicstring DateFormat { get; set; }
#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> publicobject Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { DateTime valueUtc; if (value == null) { returnnull; } elseif (value isstring && !DateTime.TryParse((string)value, out valueUtc)) { returnnull; } elseif (!(value isDateTime || value isDateTime?)) { returnnull; }
///<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> publicobject ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
DateTime valueLocal;
if (value == null) { returnnull; } elseif (value isstring && !DateTime.TryParse((string)value, out valueLocal)) { returnnull; } elseif (!(value isDateTime || value isDateTime?)) { returnnull; }
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.
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
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 ...innovmedia Saberes&Sabores Rir Até Cair
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