2

I'm using Datetimepicker of XDAN I want to set current date as default when page loaded.

I used new Date() and getUTCFullYear functions to get it.

But our current time is utc+8 So theres difference in time, how can I solve this problem. I have tried d.getUTCDate()+1 but difference between utc and utc+8 is not 1 day

This is my code:

jQuery(function(){
var d = new Date(),
date = (d.getUTCFullYear())+'-'+(d.getUTCMonth()+1)+'-'+(d.getUTCDate());

 jQuery('#from-datepicker').datetimepicker({
     format:'Y-m-d 00:00:00',
     defaultTime:'00:00',
     formatTime: 'H:00',
     timepicker: false,
     mask: false,
     value: date,
  onShow:function( ct ){
   this.setOptions({
    maxDate:jQuery('#to-datepicker').val()?jQuery('#to-datepicker').val():false
   })
  },
 });
 jQuery('#to-datepicker').datetimepicker({
     format:'Y-m-d 23:59:59',
     defaultTime:'23:59',
     formatTime: 'H:59',
     timepicker: false,
     mask: false,
     value: date,
  onShow:function( ct ){
   this.setOptions({
    minDate:jQuery('#from-datepicker').val()?jQuery('#from-datepicker').val():false
   })
  },

 });
});


<input type="text" id="from-datepicker" name="from" placeholder="yyyy-mm-dd hh:mm:ss">

<input type="text" id="to-datepicker" name="to" placeholder="yyyy-mm-dd hh:mm:ss">
ABD
  • 113
  • 3
  • 12
  • 1
    Refer [Get current date](http://stackoverflow.com/questions/8398897/how-to-get-current-date-in-jquery) – Sorangwala Abbasali Jan 21 '17 at 04:51
  • 2
    Each `getUTC` method for `Date`s has an equivalent for local time – [`getUTCFullYear()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getUTCFullYear) and [`getFullYear()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getFullYear), etc. – Jonathan Lonowski Jan 21 '17 at 04:57
  • 1
    Thanks a lot guys, Its working now! I really appreciate your help! @SorangwalaAbbasali – ABD Jan 21 '17 at 05:03

1 Answers1

1

This is working, Thanks guys for your support!

    var d = new Date();
    var month = d.getMonth()+1;
    var day = d.getDate();
    var output = d.getFullYear() + '-' +
        ((''+month).length<2 ? '0' : '') + month + '-' +
        ((''+day).length<2 ? '0' : '') + day;
ABD
  • 113
  • 3
  • 12