简体   繁体   English

获取DateTime的Date部分(ASP.NET MVC)

[英]Get Date part of DateTime(ASP.NET MVC)

I have this property in Model 我在模型中有此属性

[Display(Name = "День рождения")]
    [DataType(DataType.Date)]
    public System.DateTime Birthday { get; set; }

And write to database value like this via AJAX 然后通过AJAX写入数据库值

<script>
   $(document).ready(function () {
    $('#save').click(function () {
        save();
    });
});
    function save() {
        $.ajax({
            type: 'Post',
            dataType: 'Json',
            data: {
                FIO: $('#FIO').val(),
                Email: $('#Email').val(),
                Salary: $('#Salary').val(),
                Telephone: $('#Telephone').val(),
                English: $('#english').val(),
                City: $('#City').val(),
                Birthday: $('#Birthday').val(),
                id: $('#Interview_Id').val(),


            },
            url: '@Url.Action("WelcomeWriter", "Interwier")',
            success: function (da) {
                if (da.Result === "Success") {

                    window.location.href = da.RedirectUrl;

                } else {

                    alert('Error' + da.Message);
                }
            },
            error: function (da) {
                alert('Error');
            }
        });
    }

But my problem is this - it is writing Date and Time , I only need Date. 但是我的问题是-写日期和时间,我只需要日期。

How I can fix this? 我该如何解决?

You can use this: 您可以使用此:

var date = new Date($('#Birthday').val());

This will give you access to all the Date functions. 这将使您可以访问所有日期功能。 For example: 例如:

var day = date.getDate(); 
var month = date.getMonth();  
var year = date.getFullYear() 

So for full date: 因此,对于完整日期:

var fullDate=day+"/"+month+"/"+year;

Or: 要么:

var fullDate=d.toLocaleDateString();

Format your date in your save() method before you do your ajax call. 在执行ajax调用之前,请在save()方法中格式化日期。 Something like this: 像这样:

var d = new Date($('#Birthday').val());
var birthday = d.getFullYear() + "-" + ('0' + (d.getMonth() + 1)).slice(-2)
                + "-" + ('0' + d.getDate()).slice(-2);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM