Javascript日期输出

时间:2016-05-26 18:07:47

标签: javascript

是否可以以dd/mm/yyyy格式输出日期?我需要这种简短的格式,以使其与我的数据库兼容。

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

2 个答案:

答案 0 :(得分:0)

你可以为格式转换做一些字符串操作,我假设你不想使用jQuery:

&#13;
&#13;
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;
&#13;
<input type="text" id="theDate" name="taksittarih1" class="form-control">
&#13;
&#13;
&#13;

答案 1 :(得分:0)

您可以考虑使用JavaScript Date Format library

var now = new Date();

now.format("dd/mm/yyyy");