Remove Time part out of Jquery UI DatePicker

断了今生、忘了曾经 提交于 2019-12-13 19:56:06

问题


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?


回答1:


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');


来源:https://stackoverflow.com/questions/21709171/remove-time-part-out-of-jquery-ui-datepicker

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!