如何将日期格式12/12/2019转换为12-12-2019

时间:2019-12-09 17:09:06

标签: javascript

我在使用laravel的打击代码,我想将日期格式设置为12/12/2019至12-12-2019

代码

<script>
function DDMMYYYY(value, event) {
  let newValue = value.replace(/[^0-9]/g, '').replace(/(\..*)\./g, '$1');

  const dayOrMonth = (index) => index % 2 === 1 && index < 4;

  // on delete key.  
  if (!event.data) {
    return value;
  }

  return newValue.split('').map((v, i) => dayOrMonth(i) ? v + '/' : v).join('');;
}
</script>

输入

       <div class="col-md-4">
                                                   <input  type="tel" maxlength="10" placeholder="DD/MM/YYYY"
oninput="this.value = DDMMYYYY(this.value, event)" required class="form-control  " name="certificate_issue_date" id="certificate_issue_date">
                                    </div>

1 个答案:

答案 0 :(得分:2)

为什么不将最后一个/语句中的-替换为return

function DDMMYYYY(value, event) {
  let newValue = value.replace(/[^0-9]/g, '').replace(/(\..*)\./g, '$1');

  const dayOrMonth = (index) => index % 2 === 1 && index < 4;

  // on delete key.  
  if (!event.data) {
    return value;
  }

  return newValue.split('').map((v, i) => dayOrMonth(i) ? v + '-' : v).join('');;
}
<div class="col-md-4">
  <input type="tel" maxlength="10" placeholder="DD/MM/YYYY" oninput="this.value = DDMMYYYY(this.value, event)" required class="form-control  " name="certificate_issue_date" id="certificate_issue_date">
</div>