js仿照聊天记录的时间显示 今天 昨天

本文介绍如何使用JavaScript仿照聊天记录的时间显示,重点在于区分今天和昨天的时间格式,示例代码提供了基本思路。对于更复杂的情况,如一周内的星期几显示,可通过扩展时间变量和相应判断来实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

    var time = '2017年06月02日 10:43:24';
    // 时间统一函数
    function getTimeText(argument) {
        var timeS = argument;
        var todayT = ''; //
        var yestodayT = '';
        var timeCha = getTimeS(timeS);
        timeS = timeS.slice(-8);
        todayT = new Date().getHours()*60*60*1000 + new Date().getMinutes()*60*1000 + new Date().getSeconds()*1000;
        yestodayT = todayT + 24*60*60*1000;
        if(timeCha > yestodayT) {
            return argument.slice(0,11);
        }
        if(timeCha > todayT && timeCha < yestodayT) {
            return timeS.slice(0,2)>12?'昨天 下午'+(timeS.slice(0,2)==12 ? 12 : timeS.slice(0,2) - 12)+timeS.slice(2,5):'昨天 上午'+timeS.slice(0,5);
        }
        if(timeCha < todayT) {
            return timeS.slice(0,2)>=12?'下午'+(timeS.slice(0,2)==12 ? 12 : t
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值