kenan salim kenan salim - 6 months ago 24
Javascript Question

Javascript Date Output

Is it possible to output dates in

dd/mm/yyyy
format? I need it in this short format to make it compatible with my database.



var date = new Date();

// add 8 months
date.setMonth(date.getMonth() + 1);

document.getElementById('theDate').value = date;

// check if it is a saturday
if(date.getDay() == 6)
{
// if it is then add two days
date.setDate(date.getDate() + 2);
}

if(date.getDay() == 0)
{
// if it is then add one days
date.setDate(date.getDate() + 1);
}

document.getElementById('theDate').value = date;


<input type="text" id="theDate" name="taksittarih1" class="form-control">



Answer

You can do some string manipulation for the format conversion, I am assuming you don't want to use jQuery:

var date = new Date();

// add 8 months
date.setMonth(date.getMonth() + 1);

document.getElementById('theDate').value = date;

// check if it is a saturday
if(date.getDay() == 6)
{
  // if it is then add two days
  date.setDate(date.getDate() + 2);
}

if(date.getDay() == 0)
{
  // if it is then add two days
  date.setDate(date.getDate() + 1);
}
var dateString = ("0" + date.getUTCDate()).slice(-2) + "/" + ("0" + (date.getUTCMonth()+1)).slice(-2) +"/"+ date.getUTCFullYear();
document.getElementById('theDate').value = dateString;
<input type="text" id="theDate" name="taksittarih1" class="form-control">