ABD
ABD

Reputation: 133

get current date in js

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">

Upvotes: 2

Views: 5002

Answers (1)

ABD
ABD

Reputation: 133

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;

Upvotes: 1

Related Questions