0

Hi guys I'm a newbie of JavaScript and I tried a lot before posting here.

I have two input tags where i'm using Datapicker from jquery UI to pick some data range. After I select these values with jQuery method .val(), but the problem is that Datepicker use this type of data form: 12/30/2013, but I need to parse this data in this way: 2013-10-21T00:00:00 because I must use this date range to call a getJSON method to call a webserver. I wrote also this function to parse manually. Bellow is my code, what's wrong? In general I want to find a way to translate the data from input tags in this format: yyyy-MM-dd'T'HH:mm:ss because our web server accept through rest this format.

<input id="from" type="text" class="datepicker" />
<input id="to" type="text" class="datepicker" />
<input id="submit" value="Send" type="submit" />
<script>      
$(function () {
        $(".datepicker").datepicker();
    });

    $("#submit").click(function () {

        var from = $('#from').val();
        var to = $('#to').val();

        function parse(i) {
            var t = i.toString().split("/");
            var y = t[2];
            var m = t[0];
            var d = t[1];
            var n_d = y.concat("-") + m.concat("-") + d.concat("T00:00:00");
            return n_d;
        }


    $.getJSON("xxxx/?from=" + parse(from) + "&to=" +parse(to));

    });

</script>
2

2 Answers 2

1

Try Date.parse and Date.prototype.toISOString:

var date = Date.parse('12/30/2013');//1388347200000
date = new Date(date);//"Mon Dec 30 2013 00:00:00 GMT+0400 (MSK)"
date.toISOString;//"2013-12-29T20:00:00.000Z"
Sign up to request clarification or add additional context in comments.

2 Comments

Note that Date.parse is known to have issues, and even on MDN the compatabilty table is just a bunch of red questionmarks, as you never really know what happens when you use Date.parse, it could work in some browsers, it could not work in some browser, noone really seem to know? toISOString is also only supported in IE9 and up, so generally it's consider good practice to build the string yourself.
Yes, but there are many polyfills. You can use one of it.
0

You can get the actual date objects by using $(element).datepicker('getDate'), and then just parse those to the wanted format and send away :

Number.prototype.padLeft = function(base,chr){
    var  len = (String(base || 10).length - String(this).length)+1;
    return len > 0? new Array(len).join(chr || '0')+this : this;
}

function parseDate(d) {
    return [d.getDate().padLeft(), (d.getMonth()+1).padLeft(), d.getFullYear()].join('/')+'T'+
           [d.getHours().padLeft(), d.getMinutes().padLeft(), d.getSeconds().padLeft()].join(':');
}

$(function () {
    $(".datepicker").datepicker();

    $("#submit").click(function () {
        var from = $('#from').datepicker('getDate');
        var to   = $('#to').datepicker('getDate');

        $.getJSON("xxxx/?from=" + parseDate(from) + "&to=" + parseDate(to));
    });
});

FIDDLE

I'm guessing you would have to url encode those dates as well, and it probably would be better to just do

$.getJSON("xxxx/", {from : parseDate(from), to : parseDate(to)});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.