0

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

<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?

3
  • Евгений, есть же stackoverflow по русски, можете туда обратиться: ru.stackoverflow.com Commented Apr 19, 2017 at 8:53
  • разница? тут людей больше @anete.anetes Commented Apr 19, 2017 at 8:55
  • окладно. вот решение на сервере: stackoverflow.com/questions/6121271/… Commented Apr 19, 2017 at 8:55

2 Answers 2

1

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();
Sign up to request clarification or add additional context in comments.

3 Comments

so in ajax call I need to write like this data: { FIO: $('#FIO').val(), Email: $('#Email').val(), Salary: $('#Salary').val(), Telephone: $('#Telephone').val(), English: $('#english').val(), City: $('#City').val(), Birthday: fulldate, id: $('#Interview_Id').val(), }?
@Eugene Yes, like this.
@Eugene Yes. You need to check what date type is required in your database.
0

Format your date in your save() method before you do your ajax call. Something like this:

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

5 Comments

so in ajax call I need to write like this data: { FIO: $('#FIO').val(), Email: $('#Email').val(), Salary: $('#Salary').val(), Telephone: $('#Telephone').val(), English: $('#english').val(), City: $('#City').val(), Birthday: fulldate, id: $('#Interview_Id').val(), }?
In my example it would be Birthday: birthday
Do you still get the error when you remove the birthday from the data payload?
If I write like before. No
Do you know in which string format your backend accept the date?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.