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

后端 未结 3 1302
挽巷
挽巷 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 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}"

提交回复
热议问题