Why does DisplayFormat with DataFormatString changes “/” (slash) to “-” (dash)?

后端 未结 3 1287
挽巷
挽巷 2020-12-31 04:21

I\'m using the following code

// Model
[DisplayFormat(
    ApplyFormatInEditMode = true, 
    DataFormatString = \"{0:dd/MM/yyyy}\")]
public DateTime StartDa         


        
相关标签:
3条回答
  • 2020-12-31 04:54

    Can you change it to DataFormatString = "{0:d}"

    That should give you the short date pattern of mm/dd/year

    0 讨论(0)
  • 2020-12-31 05:00

    Change the Short date format of the server' Regional settings to use slashes e.g. yyyy/MM/dd.
    This solved the issue for me.

    0 讨论(0)
  • 2020-12-31 05:15

    Use DataFormatString = @"{0:dd\/MM\/yyyy}" instead. Since the / identifies a character that should be replaced by the default date separator for the current culture, you need to escape it in order for it to be used as a literal in the format.

    This way you have a fixed format instead of one that dynamically uses the date separator of the current culture.

    An alternative to escape the / character can be: DataFormatString = "{0:dd'/'MM'/'yyyy}"

    0 讨论(0)
提交回复
热议问题