首页 > 网站 > WEB开发 > 正文

javascript时间格式转换(今天,昨天,前天)

2024-04-27 14:08:19
字体:
来源:转载
供稿:网友

javascript时间格式转换(今天,昨天,前天)

function transDate() {        var $time =document.getElementById("share-time");        var date = $time.innerHTML.trim();        var tt = new Date(parseInt(date));        var days = parseInt((new Date().getTime() - date) / 86400000);        var today = new Date().getDate();        var year = tt.getFullYear();        var mouth = tt.getMonth() + 1;        var day = tt.getDate();        var time = tt.getHours() < 10 ? "0" + tt.getHours() : tt.getHours();        var min = tt.getMinutes() < 10 ? "0" + tt.getMinutes() : tt.getMinutes();        var result, offset;       offset = Math.abs(today - day);    if (days < 4&&offset<4) {       if (offset === 0) {
                result = "今天" + time + ":" + min;            } else if (offset === 1) {                result = "昨天" + time + ":" + min;            } else if (offset === 2) {                result = "前天" + time + ":" + min;            }        } else {            result = year + "-" + mouth + "-" + day + " " + time + ":" + min;        }        $time.innerHTML = result;    }    transDate();


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表