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?

Adnan Niloy
  • 451
  • 11
  • 16
Eugene
  • 209
  • 2
  • 13
  • Евгений, есть же stackoverflow по русски, можете туда обратиться: https://ru.stackoverflow.com/ –  Apr 19 '17 at 08:53
  • разница? тут людей больше @anete.anetes – Eugene Apr 19 '17 at 08:55
  • окладно. вот решение на сервере: http://stackoverflow.com/questions/6121271/how-to-remove-time-portion-of-date-in-c-sharp-in-datetime-object-only –  Apr 19 '17 at 08:55

2 Answers2

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();
swdev95
  • 653
  • 1
  • 4
  • 21
  • 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 Apr 19 '17 at 09:17
  • @Eugene Yes, like this. – swdev95 Apr 19 '17 at 09:50
  • @Eugene Yes. You need to check what date type is required in your database. – swdev95 Apr 19 '17 at 09:53
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);
t2t
  • 721
  • 9
  • 15
  • 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 Apr 19 '17 at 09:16
  • In my example it would be Birthday: birthday – t2t Apr 19 '17 at 09:47
  • Do you still get the error when you remove the birthday from the data payload? – t2t Apr 19 '17 at 09:54
  • If I write like before. No – Eugene Apr 19 '17 at 09:55
  • Do you know in which string format your backend accept the date? – t2t Apr 19 '17 at 10:02