0

I am trying to set a default date to my input box with type = date

<input name="date" class="dateToday form-control" type="date" required>

I tried this:

$('.dateToday').val(new Date().toISOString().slice(0, 10));

But this is setting the default value to tomorrows date instead of today.

Is there a way to set date more accurately using system time?

jedu
  • 1,211
  • 2
  • 25
  • 57
  • 1
    It is work for me bro !! – David Japan Dec 03 '19 at 04:40
  • 1
    What is your timezone ? – Shubhanu Sharma Dec 03 '19 at 04:41
  • Notice you are using toISOString() which returns UTC format so it may resolve to a date ahead depending where you are - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toISOString You can just create the string yourself using the getDate(), getDay(), getFullYear() methods from a date object instead and it will always be local to the browser running the script. – Alejandro Dec 03 '19 at 04:44
  • Can you set the default value from the backend? Setting it on the frontend is not advisable since user can change their device time and date. – Eddie Dec 03 '19 at 05:26

3 Answers3

0

This will give you the date of system

var systemDate = new Date().getDate();

And you can set it in this way

<input id="dateId" name="date" class="dateToday form-control" type="date" required>
$("#dateId").text(systemDate);
Wasim
  • 600
  • 2
  • 11
  • 32
0

please try this one get current date in html

html code

              <div class="col">
                    <label for="date">Date</label>
                    <input type="date" onload="getDate()" class="form-control" id="date" 
                      name="date">
                </div>

js code

function getDate(){
    var today = new Date();

document.getElementById("date").value = today.getFullYear() + '-' + ('0' + (today.getMonth() + 1)).slice(-2) + '-' + ('0' + today.getDate()).slice(-2);


}
PHP Geek
  • 3,949
  • 1
  • 16
  • 32
0

This will help you

var d = new Date();
var month = d.getMonth()+1;
var day = d.getDate();
var output = d.getFullYear() + '/' +
(month<10 ? '0' : '') + month + '/' +
(day<10 ? '0' : '') + day;
Mowgli
  • 129
  • 1
  • 3
  • 9