Parse data with javascript

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>

Answers


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"

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)});

Need Your Help

Response.Write() in WebService

asp.net json web-services asmx

I want to return JSON data back to the client, in my web service method. One way is to create SoapExtension and use it as attribute on my web method, etc. Another way is to simply add [ScriptService]

Screen Videoing on the iPod Touch?

ios ipod-touch

I want to record a Screen Video of the iPod Touch 4G. Unfortunately, The iPod Touch doesn't have apps like SnapzPro or screenflow. I did find a iShowU but its not the screen recording one.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.