JS time format appears hh: 12: 30, time format
1. Problem background
Recently, when I was working on a project, I encountered the following problem: when I used JS to format the date and time, the page displayed hh: 12: 30, and no data was displayed in the hour, when hh is changed to HH, sometimes HH is displayed in the hour
var dayTime = $("#endTime").val();var dtime = new Date(dayTime);dtime.setTime(dtime.getTime()+365*100);var date = new Date(dtime.getTime());$("#endDate").val(date.fromat("yyyy-MM-dd hh:mm:ss"));
2. Problem Analysis
(1) For the formatting time, the format is: yyyy-MM-dd hh: mm: ss. hh is generated when the display is 12 hours. If it is two o'clock P.M., 02 is displayed; six o'clock A.M. appears in the project, for example:
Hh: 00: 40 is displayed directly in the display box, probably because the format is not compatible.
(2) The format is yyyy-MM-dd HH: mm: ss. Sometimes, the format of HH: 12: 30 may occur.
3. Problem Methods
Concatenate date and time using the conventional method
var dayTime = $("#endTime").val();var dtime = new Date(dayTime);dtime.setTime(dtime.getTime()+365*100);var date = new Date(dtime.getTime());var year = date.getFullYear();var month = date.getMonth()+1;var day = date.getDate();var hour = date.getHours();var minute = date.getMinutes();var second = date.getSeconds();var date1 = year+"-"+(month<10?"0"+month:month)+"-"+(day<10?"0"+day:day)+" "+(hour<10?"0"+hour:hour)+":"+(minute<10?"0"+minute:minute)+":"+(second<10?"0"+second:second);$("#endDate").val(date1);
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.