Home JQuery格式化时间
Post
Cancel

JQuery格式化时间

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
$(function(){
    alert(getDateString(new Date("2022-04-19")));
});

function getDateString(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;
}
This post is licensed under CC BY 4.0 by the author.