Ciphra Ciphra - 1 day ago 3
C# Question

Can I store my DateTime in european notation?

I'm trying to find a way to store my

DateTime
-object in the European notation. Right now, when I bind a textbox to the
DateTime
-property of my object, it displays as the MM/dd/yyyy HH:mm AM(or PM). The reason I want to save it in this notation, is that I can't alter the notation via
ToString("dd/MM/yyyy HH:MM")
, because the same property is also used to update other
DateTime
-properties. It would require a lot of rewriting (everytime I retrieve the string to set another object's DateTime, I'd have to
TryParse
it back to a
DateTime
-format), so I'm trying to find out if there's a way to save it in a desired notation.

An example that obviously doesn't work, but brings the thought across,

public DateTime<dd/MM/yyyy HH:mm> ModifiedAt { get; set; }


I tried to see if I could force it to keep track in a different format like this,

public DateTime ModifiedAt
{
get { return this.modifiedAt; }
set
{
var s = value.ToString("dd/MM/yyyy HH:mm");
DateTime.TryParse(s, out this.modifiedAt);
this.RaisePropertyChanged();
}
}


but to no avail. As I'm starting to doubt this is possible, I'm curious if anyone has come across this problem before. Maybe there is a way to alter the displaystring in the declaration of the binding? As I'm using MVVM, I've set my viewmodel as datacontext and my binding looks like this,

<TextBox Text="{Binding SelectedObject.ModifiedAt, Mode=OneWay}"/>

Answer

format your DateTime Property in xaml.

<TextBox Text="{Binding MyDateTimeProperty, StringFormat='dd/MM/yyyy HH:mm'}"/>

As explained here

Comments