简体   繁体   中英

Convert Date Format to String Javascript

I have a php file to get data from ajax. Then I made the ajax to put the result into html using jquery. My problem is, there's a data from my db that have date format. When I want to put it to html, it doesn't works.

Here's my code:

function getKonsumen()
    {
        var selname = $("select[name=konsumen]").val();
            $.ajax({ url: "getData.php",
                data: {"selname":selname},
                type: 'post',
                dataType: "json",
                success: function(output) {
                  console.log(output);
                    $("#namak").val(output[2]);
                    $("#ktpk").val(output[3]);
                    $("#emailk").val(output[4]);
                    $("#hpk").val(output[5]);
                    $("#alamatk").val(output[6]);
                    $("#kotak").val(output[7]);
                    var newd = new Date(output[8]);
                    $("#bdayk").val(newd.toString());
                }

            });
    }

my sample of output[8] is like '2015-08-01' or '2014-12-25' all my form has been filled by this function, except the bdayk. Any solution?

You need to use the following syntax -

new Date(year, month, day [, hour, minute, second, millisecond ]);

So in your case it should be

var newd = new Date(2015,07,1);

And do note that months in Javascript start from 0 rather than 1.

My solution:

Convert my date data from sql to string using php, then I throw to javascript. it's all done.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM