简体   繁体   中英

Set Date Time to Jquery date time picker

I have date in Javascript

Sun Feb 15 2015 08:02:00 GMT+0200 (EET)

how I can to set in format ' dd/mm/yyyy hh:mm:ss ' into datetime picker?

If I set like this:

dateStart

Sun Feb 15 2015 08:02:00 GMT+0200 (EET)

$('#dateTimeStart').datetimepicker('setDate', dateStart);

Error parsing the date/time string: Missing number at position 10 date/time string = 02-15-2015 08:02:00 timeFormat = HH:mm dateFormat = mm-dd-yyyy

$('#dateTimeStart').datetimepicker({
        dateFormat: 'yy-dd-mm'
        timeFormat: "hh:mm:ss"
    });

You have to format date that you have. You can do it with help of function that you can grab here .

Usage:

<script>
  document.write($.format.date("Sun Feb 15 2015 08:02:00", "dd/mm/yyyy hh:mm:ss"));
</script>

It's small and really nice solution that can help you to resolve your issue.

You're looking for a format like

jQuery('#dateTimeStart').datetimepicker({
    format:'d/m/Y H:i:s'
});

According to the DateTimePicker documentation at http://xdsoft.net/jqplugins/datetimepicker/ , the 'format' string is based on the PHP date format strings, which you can read up on at http://php.net/manual/en/function.date.php . Note in particular they use just one letter to represent multiple numbers (eg 'd' instead of 'dd')

use below javascript code to change formate

 var date = new Date('Sun Feb 15 2015 08:02:00 GMT+0200');

 var day    = date.getDate();
 var month  = date.getMonth();
 var year  = date.getFullYear();
 var hours = date.getHours();
 var minutes = "0" + date.getMinutes();
 var seconds = "0" + date.getSeconds();

 var fulldate = day+'/'+(month+1)+'/'+year+' '+hours + ':' + minutes.substr(minutes.length-2) + ':' + seconds.substr(seconds.length-2);

see working copy of fiddle

you can create function which return date in format

function convertdate(param){
    var date = new Date(param);
    var day    = date.getDate();
    var month  = date.getMonth();
    var year  = date.getFullYear();
    var hours = date.getHours();
    var minutes = "0" + date.getMinutes();
    var seconds = "0" + date.getSeconds();

    return fulldate = day+'/'+(month+1)+'/'+year+' '+hours + ':' + minutes.substr(minutes.length-2) + ':' + seconds.substr(seconds.length-2);
}
alert(convertdate('Sun Feb 15 2015 08:02:00 GMT+0200'));

I change format of date from server in this : ' 02/15/2015 08:02:00 AM ' and then I parse this string and create new date:

var dateString ='02/15/2015 08:02:00 AM';
var dateStart = new Date(Date.parse(dateString, "mm/dd/yyyy hh:mm:ss"));
$('#dateTimeStart').datetimepicker('setDate', dateStart);

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