如何正确显示日期时间本地值

时间:2018-09-30 03:44:53

标签: javascript html

所以我有这段代码,其中包含日期时间本地输入,单击按钮时,日期和时间显示在sa网页上。问题是格式太混乱了,我不知道如何将其转换为更具表现力的方式

HTML文件和javascript文件:

<clickable :action="doAjaxRequest">
  <button type="button" slot="action">Button</button>
</clickable>
<script type="text/javascript">
     
         function Record(){
		
		 document.getElementById("datetime").innerHTML = document.getElementById("dateTime").value;
		 }
        </script>

最终结果显示类似日期:2018-12-16T03:12

1 个答案:

答案 0 :(得分:2)

function myTime()
{
  var time= document.getElementById("party").value;
  console.log(time);
  var date = new Date(time);

  var time1 = (time.split("T")[1]);

  time1 = time1.toString ().match (/^([01]\d|2[0-3])(:)([0-5]\d)(:[0-5]\d)?$/) || [time];

  if (time1.length > 1) 
  { 
    time1 = time1.slice (1);
    time1[5] = +time1[0] < 12 ? 'AM' : 'PM'; 
    time1[0] = +time1[0] % 12 || 12; 
  }
  var month = date.getMonth() + 1;
  var day = date.getDate();
  month = month > 9 ? month : "0"+month;
  day = day > 9 ? day : "0"+day;

  console.log(month+"-"+day+"-"+date.getFullYear());
  console.log(time1.join(''));
}
<input id="party" type="datetime-local" name="partydate" value="2017-06-01T08:30">
<button onclick="myTime()">Submit</button>