简体   繁体   中英

Desktop app's datetime format compatibility with Win10

I have a WPF application and I'm using the MVVM framework. In my view model, I have a date time setter that checks if the value of the variable has changed and if yes, sets the new value to the variable.

My problem is that the value of the date time if incorrect every time it enters the setter.

For example: Datetime value is January 11, 2019

On initial set value is equal to January 11, 2019, but on the succeeding run it became November 1, 2019 -- and on the next run back to January 11, 2019 and so on and forth.

Thus causes an StackMemoryException due to infinite looping in the setter function.

I've tried adding an if statement to check id the old and new value is the same but the problem is the value of the setter always switcher from Jan to Nov.

I've tried parsing the date to a same date format.

public DateTime? ActionDate
{
    get { return actionDate; }
    set
    {
       if (value.HasValue && !actionDate.HasValue)
       {
            actionDate = value;
            OnPropertyChanged("ActionDate");
       }
       else if (value.HasValue && actionDate.HasValue && actionDate.Value.Date.ToString("MM/dd/yyyy") != value.Value.Date.ToString("MM/dd/yyyy"))
        {
            actionDate = value;
            OnPropertyChanged("ActionDate");
         }
      }
}

I also tried running the app in a machine using Windows 7 OS and it works okay. Any suggestions will be helpful.

Thanks

Edit: I've already resolved this problem. The issue was the Datepicker tag in my xaml. I bind the model value to both the SelectedDate and Text attribute of the Date Picker that's why the setter was called twice and incorrect date is set. I just remove the Text binding from the Datepicker in xaml file.

Thanks

I think your problem comes from your string format.

https://docs.microsoft.com/en-us/dotnet/standard/base-types/standard-date-and-time-format-strings

  //For the date January 11, 2019, 
  actionDate.Value.Date.ToString("MM/dd/yyyy")
  this will return November 1, 2019. 

  dd => day
  MM => month
  yyyy => year 
  HH => hour (24 hours)
  hh => hour (12 hours)
  mm => minute
  ss => second
  here is an exemple for January 11, 2019

  //you can use 

  actionDate.Value.Date.ToString("dd/MM/yyyy");
  //or
  string.Format("{0:dd-MM-yyyy}", actionDate.Value.Date); // => 11-01-2019

I think the display format in which you are displaying needs to be corrected. As for setting the value if value is different then earlier set value. you can use this logic: -

public DateTime? ActionDate
    {
    get { return actionDate; }
    set
    {
    if (value.HasValue && value.CompareTo(actionDate)!=0)
        actionDate = value;
   OnPropertyChanged("ActionDate");
    } 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM