从Jquery UI DatePicker中删除Time部分

我知道这已被问过很多次了,相信我,我尝试了一切让它发挥作用。

我有一个网站,让您注册或登录第三方提供商(Facebook,Twitter,Google +,Microsoft)在注册表格中有一个字段,您应该把你的生日。

这是该字段的ViewModel(RegisterViewModel):

[Required] [DataType(DataType.Date, ErrorMessageResourceType = typeof(Resources), ErrorMessageResourceName = "IncorrectDateFormat")] [DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}", ApplyFormatInEditMode = true)] public DateTime BirthDate { get; set; } 

这是模型(UserViewModel):

  [Required] [DataType(DataType.Date)] public DateTime BirthDate { get; set; } 

我正在使用automapper将这两个映射到我的控制器中。

在视图中,我正在渲染这样的字段:

  
@Html.LabelFor(m => m.BirthDate, new { @class = "col-md-2 control-label" })
@Html.TextBoxFor(m => m.BirthDate, new { @class = "form-control input-xlarge clearfix", @placeholder = @Resources.BirthDate }) @Html.ValidationMessageFor(model => model.BirthDate)

我使用Html.TextBoxFor而不是EditorFor,因为Chrome自动呈现DatePicker,我使用的是Jquery datepicker。

这就是我在JS文件中设置DatePicker的方法:

 //All birthdate will be datepickers! $("#BirthDate").datepicker({ altFormat: 'MM/dd/yyyy'}); 

我有以下问题/观察:

案例1 – 注册本地帐户)工作完美! 在DatePicker文本中,我只以正确的格式看到Date。

案例2 – 登录第三方提供商)这是我的问题,我从这样的用户获得Facebook出生日期(Facebook返回像MM / dd / yyyy这样的日期,但显然是一个字符串,所以我将它转换为DateTime :

 var birthdateClaim = externalIdentity.Result.Claims.Single(c => c.Type == "urn:facebook:birthday").Value; var birthdate = DateTime.ParseExact(birthdateClaim, "MM/dd/yyyy", CultureInfo.InvariantCulture); var registerModel = new RegisterViewModel { BirthDate = birthdate, }; return registerModel; 

当我记录用户时,我将他发送到一个视图,在那里他可以看到他的信息(从Facebook中提取)并且他能够添加更多信息,但是日期选择器会随着时间的推移显示它! 像这样:19/10/1978 12:00:00 am(dd / MM / yyyy Time)

我错过了什么? 我不想在提供者发送生日的时候显示时间。 我该怎么办? 有任何想法吗?

即使没有提供时间, DateTime也会添加时间(如DateTIME建议的名称)。 您必须使用toString格式化日期。

 var birthdate = DateTime.ParseExact(birthdateClaim, "MM/dd/yyyy", CultureInfo.InvariantCulture).toString('MM/dd/yyyy');