JS time format appears hh: 12: 30, time format

Source: Internet
Author: User
Tags date1

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.