如何在输入表单字段中插入当前日期作为值

时间:2018-08-05 00:11:53

标签: javascript php html

我能够使用所需格式的java获取当前日期,并且实际上希望它在输入字段中显示为文本值。 我不想使用type =“ date”进行选择。 任何提示,或如何将当前日期作为值插入输入字段。我不介意php是否可以助威

 <input type="text" value='<span id="date"></span>'>

Java脚本

  var months = ["January", "February", "March", "April", "May", "June",    
  "July", "August", "September", "October", "November", "December"];
  var n = new Date();
  var y = n.getFullYear();
  var m = n.getMonth();
  var d = n.getDate();
  document.getElementById("datea").innerHTML = d + " " + months[m] + " " +    
   y;

2 个答案:

答案 0 :(得分:3)

您犯了一些错误。否则很好

O(n log n)
var months = ["January", "February", "March", "April", "May", "June",
  "July", "August", "September", "October", "November", "December"
];
var n = new Date();
var y = n.getFullYear();
var m = n.getMonth();
var d = n.getDate();
document.getElementById("date").value = d + " " + months[m] + " " + y;

答案 1 :(得分:0)

如果您使用PHP生成页面,则可以执行以下操作:

<input type="text" value="<?php echo (new DateTime())->format('j F Y'); ?>"></input>

输出:

<input type="text" value="4 August 2018"></input>