Home  >  Article  >  Web Front-end  >  How to format time in jquery

How to format time in jquery

藏色散人
藏色散人Original
2021-01-04 09:32:239338browse

How to format time in jquery: First create a js code sample file; then format the Date date and time data through the timeStamp2String method in Jquery.

How to format time in jquery

The operating environment of this tutorial: Dell G3 computer, Windows 7 system, jquery1.10.0 version.

Recommended: "javascript basic tutorial""jquery tutorial"

Format Date date and time data in Jquery

$(function(){
    //当前时间格式化yyyy-MM-dd HH:mm:ss
    alert(timeStamp2String(new Date().getTime()));
    alert(timeStamp3String(new Date().getTime()));
    debugger;
});
//在Jquery里格式化Date日期时间数据
function timeStamp2String(time){
    var datetime = new Date();
    datetime.setTime(time);
    var year = datetime.getFullYear();
    var month = datetime.getMonth() + 1 < 10 ? "0" + (datetime.getMonth() + 1) : datetime.getMonth() + 1;
    var date = datetime.getDate() < 10 ? "0" + datetime.getDate() : datetime.getDate();
    var hour = datetime.getHours()< 10 ? "0" + datetime.getHours() : datetime.getHours();
    var minute = datetime.getMinutes()< 10 ? "0" + datetime.getMinutes() : datetime.getMinutes();
    var second = datetime.getSeconds()< 10 ? "0" + datetime.getSeconds() : datetime.getSeconds();
    return year + "-" + month + "-" + date+" "+hour+":"+minute+":"+second;
}

The formatted time here is yyyy-MM-dd HH:mm:ss. If you need to format it to other times such as yyyy-MM-dd, you can just remove the hours, minutes and seconds.

How to format time in jquery

For more programming-related knowledge, please visit: Programming Teaching! !

The above is the detailed content of How to format time in jquery. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn