datetimepicker getDate to return Date / Time in UT

2019-02-22 23:46发布

I happen to read this datetimepicker addon and found it to be of great use. The issue that I am facing using this tool is that I am unable to get data / time format in UTC or other formats (my intention is to at least get date / time in UTC format:

$('#starttime').datetimepicker({
    ampm: true,
    showHour: true,
    showMinute: true,
    showSecond: false,
    showMillisec: false,
    timeFormat: 'hh:mm TT',
    hourGrid: 4,
    stepHour: 1,
    minDate: minDate,
    maxDate: maxDate,
    stepMinute: 10
 });                             

The below script prints me data / time in below format:

var startDt = $('#starttime').datetimepicker('getDate');

Tue May 01 2012 00:00:00 GMT+0530 (India Standard Time)

How can I change this format to any other like

DD-MM-YYYY or DD/MM/YYYY or in UTC format?

2条回答
女痞
2楼-- · 2019-02-23 00:23

These are some of the date formats: http://jqueryui.com/demos/datepicker/#date-formats

$('#starttime').datetimepicker( "option", "dateFormat", "dd/mm/yy");
查看更多
家丑人穷心不美
3楼-- · 2019-02-23 00:34

Demo: http://jsfiddle.net/Dgnjn/2/

or

dd-mm-yy here http://jsfiddle.net/Dgnjn/5/

Now using datetimepicker

The important thing to note in the sample below is:

dateAsObject = $.datepicker.formatDate('mm-dd-yy', new Date(dateAsObject))

code

$('#datetimepicker').datetimepicker({
    //altField: '#alternate',
    dateFormat: 'yy-mm-dd',
    timeFormat: 'hh:mm:ss',
    stepHour: 2,
    stepMinute: 10,
    onSelect: function(dateText, inst) { 
      var dateAsString = dateText; //the first parameter of this function
      var dateAsObject = $(this).datepicker( 'getDate' ); //the getDate method
     dateAsObject = $.datepicker.formatDate('mm-dd-yy', new Date(dateAsObject))
        $('#alternate').val(dateAsObject);
       // alert(dateAsObject + '==> ' + $('#alternate').val());
   }

});
查看更多
登录 后发表回答