Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery date picker set date format as 'MM-DD-YYYY' from a string containing datetime

I am trying to set the jquery datepicker date format but it is not working, I have read few posts and answer already but none of them worked for me. Below is the code I am using please check and tell me where I am doing wrong. I am getting the datetime from Database as 2012-03-06 00:00:00 UTC

<script>
        $(document).ready(function() {
            $(".datepicker").datepicker({
                dateFormat:'MM-DD-YYYY'
            }).val();
        });
    </script>

Also I tried

<script>
        $(document).ready(function() {
            var dateTypeVar = $('.datepicker').datepicker('getDate');
            $.datepicker.formatDate('dd-mm-yy', dateTypeVar);
        });
    </script>
like image 963
Paul Phoenix Avatar asked Aug 21 '13 14:08

Paul Phoenix


People also ask

How do I change date format in date picker?

Do one of the following: For a text box control or a date picker control, ensure that the Data type list displays the appropriate data type, and then click Format. For an expression box control, ensure that the Format as list displays the appropriate data type, and then click Format.

How do I change the date format from YYYY-MM-DD in jQuery?

Re: convert Date from YYYY-MM-DD to MM/DD/YYYY in jQuery/JavaScript. var tempDate = new Date("2021-09-21"); var formattedDate = [tempDate. getMonth() + 1, tempDate.

How can change date format in jQuery ui Datepicker?

inside the jQuery script code just paste the code. $( ". selector" ). datepicker({ dateFormat: 'yy-mm-dd' });


2 Answers

This 2012-03-06 00:00:00 UTC is not a valid JavaScript date, so the datepicker can't accept the value assigned.

Date object: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date

setDate method: http://api.jqueryui.com/datepicker/#method-setDate

Get the date in a compliant format, and then set the datepicker in this way.

Code:

$(document).ready(function () {
    var dbDate = "2012-03-06";
    var date2 = new Date(dbDate);

    $(".datepicker").datepicker({
        dateFormat: 'mm-dd-yy'
    }).datepicker('setDate', date2)

});

Demo: http://jsfiddle.net/IrvinDominin/7ck7D/

like image 115
Irvin Dominin Avatar answered Oct 16 '22 19:10

Irvin Dominin


$(document).ready(function () {

    var date2 = new Date().getDate()-9;

    $(".datepicker").datepicker({
        dateFormat: 'dd-mm-yy'
    }).datepicker('setDate', date2)

});

http://jsfiddle.net/IrvinDominin/7ck7D/

like image 27
Gregory Dias Avatar answered Oct 16 '22 17:10

Gregory Dias