从Jquery UI DatePicker中删除时间部分 [英] Remove Time part out of Jquery UI DatePicker
问题描述
我有一个网站,让您注册或使用第三方提供商(Facebook,Twitter,Google+,Microsoft)登录在注册表格中有一个字段应该把你的生日。
这是该字段的ViewModel(RegisterViewModel):
code> [必需]
[DataType(DataType.Date,ErrorMessageResourceType = typeof(Resources),ErrorMessageResourceName =incorrectDateFormat)]
[DisplayFormat(DataFormatString ={0:MM / dd / yyyy},ApplyFormatInEditMode = true)]
public DateTime BirthDate {get;组; }
这是模型(UserViewModel):
[必需]
[DataType(DataType.Date)]
public DateTime BirthDate {get;组;
我正在使用automapper在我的控制器中映射这两个。
在视图中,我正在渲染该字段:
< div class =form-组>
@ Html.LabelFor(m => m.BirthDate,new {@class =col-md-2 control-label})
< div class =col- md-10>
< span class =input-icon>
@ Html.TextBoxFor(m => m.BirthDate,new {@class =form-control input-xlarge clearfix,@placeholder = @ Resources.BirthDate})
@ Html.ValidationMessageFor model => model.BirthDate)
< i class =icon-calendar blue>< / i>
< / span>
< / div>
< / div>
我使用Html.TextBox而不是EditorFor,因为Chrome会自动呈现DatePicker,而且我正在使用Jquery datepicker
这是我在JS文件中设置DatePicker的方式:
//所有出生日期将是datepickers!
$(#BirthDate)。datepicker({altFormat:'MM / dd / yyyy'});
我有以下问题/意见:
案例1 - 注册本地帐户)
工程完美!在DatePicker文本中,我只看到日期格式正确。
案例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中提取),然后他可以添加更多的信息datepicker显示它与时间!像这样:19/10/1978 12:00:00 a.m.(dd / MM / yyyy时间)
我缺少什么?当提供者发送出生日期时,我不会显示时间。我该怎么办?任何想法?
DateTime
添加时间即使没有时间提供(作为DateTIME建议的名称)。您必须使用 toString
格式化日期。
var birthdate = DateTime .ParseExact(birthdateClaim,MM / dd / yyyy,CultureInfo.InvariantCulture).toString('MM / dd / yyyy');
I know this has been asked many times, and trust me I tried everything to make it work.
I have a site that let's you register or sign in with a third party provider (Facebook, Twitter, Google+, Microsoft) In the registration form there is a field where you should put your birthday.
This is the ViewModel (RegisterViewModel) for that field:
[Required]
[DataType(DataType.Date, ErrorMessageResourceType = typeof(Resources), ErrorMessageResourceName = "IncorrectDateFormat")]
[DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}", ApplyFormatInEditMode = true)]
public DateTime BirthDate { get; set; }
This is the model (UserViewModel):
[Required]
[DataType(DataType.Date)]
public DateTime BirthDate { get; set; }
I am using automapper to map those two in my controller.
In the view I am rendering that field like this:
<div class="form-group">
@Html.LabelFor(m => m.BirthDate, new { @class = "col-md-2 control-label" })
<div class="col-md-10">
<span class="input-icon">
@Html.TextBoxFor(m => m.BirthDate, new { @class = "form-control input-xlarge clearfix", @placeholder = @Resources.BirthDate })
@Html.ValidationMessageFor(model => model.BirthDate)
<i class="icon-calendar blue"></i>
</span>
</div>
</div>
I am using Html.TextBoxFor instead of EditorFor since Chrome automatically renders a DatePicker and I am using Jquery datepicker.
This is how I set up the DatePicker in my JS file:
//All birthdate will be datepickers!
$("#BirthDate").datepicker({ altFormat: 'MM/dd/yyyy'});
I have the following problems / observations:
Case 1 - Register Local Account) Works perfect! On the DatePicker text I only see Date in the correct format.
Case 2 - Sign In Third Party Provider) Here is my problem, I'm getting facebook Date of Birth from the user like this (Facebook returns a date like MM/dd/yyyy but as a string obviously, so I convert it to 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;
When I log the user, I send him to a view where he can see his information (extracted from Facebook) and he is able to add more information, however the datepicker shows it with time!!! Like this: 19/10/1978 12:00:00 a.m. (dd/MM/yyyy Time)
What am I missing??? I don't whant to show the time whenever the provider sends the birthdate. What should I do? Any ideas?
DateTime
adds the time even if no time is provided (as the name DateTIME suggests). You have to format the date with toString
.
var birthdate = DateTime.ParseExact(birthdateClaim, "MM/dd/yyyy", CultureInfo.InvariantCulture).toString('MM/dd/yyyy');
这篇关于从Jquery UI DatePicker中删除时间部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!