UK date format issues

I have a restaurant based web project that I am currently working on and am having problems with Datetime.

I have WebMethodone that adds table reservations to a SQL Server database.

[WebMethod]
public void AddTable(string first, string last, string email, long telephone, int people, string special, DateTime bookingDate)
{
    using (BookingLinqDataContext bl = new BookingLinqDataContext())
    {
        bl.AddTable(first, last, email, telephone, people, bookingDate, special);
    }
}

This is the parsed data from an Ajax call:

$("#AddTableBut").click(function () {
    var firstName = $("#FirstName").val();
    var lastName = $("#LastName").val();
    var email = $("#Email").val();
    var telephone = $("#Telephone").val();
    var numberPeople = $("#NumberPeople").val();
    var date = $("#DateComing").val();
    var special = $("#SpecialReq").val();

    var Book = {
        'first': firstName,
        'last': lastName,
        'email': email,
        'telephone': telephone,
        'people': numberPeople,
        'special': special,
        'bookingDate': date
    }

    $.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        url: "/PeldonRoseService.asmx/AddTable",
        dataType: "json",
        data: JSON.stringify(Book),
        success: function () {
            $("[id$=booking]").hide('blind', { direction: 'up' }, 2000);
            $("#BookNowBut").delay(2100);
            $("#BookNowBut").show('slide', { direction: 'right' }, 500);
        }
    });
});

When I check this, I add the date to the corresponding text box in the UK date format dd-MM-yyyy. However, I get this error from the Chrome Developer Toolbar:

Message: 04/30/2014 is not a valid value for DateTime.

However, if I use the American date format MM-dd-yyyy, everything works fine.

Could the problem be my browser culture information?

+4
2

.parse .

JQuery :

$("#AddTableBut").click(function () {
var firstName = $("#FirstName").val();
var lastName = $("#LastName").val();
var email = $("#Email").val();
var telephone = $("#Telephone").val();
var numberPeople = $("#NumberPeople").val();
var date = $("#DateComing").val();
var special = $("#SpecialReq").val();

var Book = {
    'first': firstName,
    'last': lastName,
    'email': email,
    'telephone': telephone,
    'people': numberPeople,
    'special': special,
    'bookingDate': date
}

$.ajax({
    type: "POST",
    contentType: "application/json; charset=utf-8",
    url: "/PeldonRoseService.asmx/AddTable",
    dataType: "json",
    data: JSON.stringify(Book),
    success: function () {
        $("[id$=booking]").hide('blind', { direction: 'up' },     2000);
        $("#BookNowBut").delay(2100);
        $("#BookNowBut").show('slide', { direction: 'right' }, 500);
    }
});

});

-:

[WebMethod]
public void AddTable(string first, string last, string email, long telephone, int people, string special, string bookingDate)
{
Date time dt = DateTime.parse(bookingDate);
using (BookingLinqDataContext bl = new BookingLinqDataContext())
{
    bl.AddTable(first, last, email, telephone, people, dt, special);
}

}

, .parse, , . linq.

0

- . :

  • , - .
  • bookingDate string DateTime.
  • DateTime.Parse DateTime.ParseExact DateTime.
  • , . "1/4/2014" 4 1 . Parse ParseExact . , , . , .

  • JavaScript- , . Date moment ( moment.js). , .
  • ISO-8601, "2014-04-13" (, , ).
  • .
  • DateTime .

moment.js, :

var date = moment($("#DateComing").val(),"l").format("YYYY-MM-DD");

moment.js, - :

var dt = new Date($("#DateComing").val());

function zeroPad(n){ return n < 10 ? '0' + n : n; }

var date = dt.getFullYear() + '-' + zeroPad(dt.getMonth()+1) +
                              '-' + zeroPad(dt.getDate());

( , toISOString toJSON .)

+3

Source: https://habr.com/ru/post/1536330/


All Articles