Home php教程 PHP源码 Convert timestamp to time format, convert time format to xx hours ago

Convert timestamp to time format, convert time format to xx hours ago

Nov 08, 2016 am 11:31 AM


mysql时间戳传到前台,百度的那些都转换代码都不可用。此代码片段是从一个在线转换网站扣的。

/**
 * 将时间转换为 xx小时前
 * @param {Object} pTime
 */
function jsDateDiff(pTime) {
    var d_minutes, d_hours, d_days, d;
    var timeNow = parseInt(new Date().getTime() / 1000);
    pTime_new = new Date(pTime).getTime() / 1000;
    d = timeNow - pTime_new;
    d_days = parseInt(d / 86400);
    d_hours = parseInt(d / 3600);
    d_minutes = parseInt(d / 60);
    if (d_days > 0 && d_days < 4) {
        return d_days + "天前";
    } else if (d_days <= 0 && d_hours > 0) {
        return d_hours + "小时前";
    } else if (d_hours <= 0 && d_minutes > 0) {
        return d_minutes + "分钟前";
    } else {
        return pTime;
    }
}
Copy after login
function format(timestamp) {
    var time = new Date(timestamp);
    var year = time.getFullYear();
    var month = (time.getMonth() + 1) > 9 && (time.getMonth() + 1) || (&#39;0&#39; + (time.getMonth() + 1)) var date = time.getDate() > 9 && time.getDate() || (&#39;0&#39; + time.getDate()) var hour = time.getHours() > 9 && time.getHours() || (&#39;0&#39; + time.getHours()) var minute = time.getMinutes() > 9 && time.getMinutes() || (&#39;0&#39; + time.getMinutes()) var second = time.getSeconds() > 9 && time.getSeconds() || (&#39;0&#39; + time.getSeconds()) var YmdHis = year + &#39;-&#39; + month + &#39;-&#39; + date + &#39; &#39; + hour + &#39;:&#39; + minute + &#39;:&#39; + second;
    return YmdHis;
}
var timestamp = &#39;1466076718000&#39;;
timestamp = timestamp.replace(/^\s+|\s+$/, &#39;&#39;);
if (/^\d{10}$/.test(timestamp)) {
    timestamp *= 1000;
} else if (/^\d{13}$/.test(timestamp)) {
    timestamp = parseInt(timestamp);
} else {
    alert(&#39;时间戳格式不正确!&#39;);
    return;
}
var YmdHis = format(timestamp);
alert(YmdHis);
Copy after login


Statement of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1653
14
PHP Tutorial
1251
29
C# Tutorial
1224
24