js 显示友好的时间格式【刚刚、几秒前,几小时,几天前(3天内) 时间格式化】

/**
 * 毫秒转换友好的显示格式
 * 输出格式:21小时28分钟15秒
 * @param  {[type]} time [description]
 * @return {[type]}      [description]
 */
function timeToDate(time)
{
    // 获取当前时间戳
    var currentTime = parseInt(new Date().getTime()/1000);
    var diffTime     = currentTime-time;
    var second         = 0;
    var minute         = 0;
    var hour         = 0;
    if (null != diffTime && "" != diffTime) {
        if (diffTime > 60 && diffTime < 60 * 60) {
            diffTime = parseInt(diffTime / 60.0) + "分钟" + parseInt((parseFloat(diffTime / 60.0) - parseInt(diffTime / 60.0)) * 60) + "秒";
        }
        else if (diffTime >= 60 * 60 && diffTime < 60 * 60 * 24) {
            diffTime = parseInt(diffTime / 3600.0) + "小时" + parseInt((parseFloat(diffTime / 3600.0) -
                parseInt(diffTime / 3600.0)) * 60) + "分钟" +
                parseInt((parseFloat((parseFloat(diffTime / 3600.0) - parseInt(diffTime / 3600.0)) * 60) -
                parseInt((parseFloat(diffTime / 3600.0) - parseInt(diffTime / 3600.0)) * 60)) * 60) + "秒";
        }
        else {
            //超过1天
            var date = new Date(parseInt(time) * 1000);
            diffTime = date.getFullYear()+"/"+(date.getMonth()+1)+"/"+date.getDate();
            //diffTime = parseInt(diffTime) + "秒";
        }
    }
    return diffTime;
}
/**
 * 毫秒转换友好的显示格式
 * 输出格式:21小时前
 * @param  {[type]} time [description]
 * @return {[type]}      [description]
 */
function dateStr(date){
    //获取js 时间戳
    var time=new Date().getTime();
    //去掉 js 时间戳后三位,与php 时间戳保持一致
    time=parseInt((time-date*1000)/1000);

    //存储转换值
    var s;
    if(time<60*10){//十分钟内
        return ‘刚刚‘;
    }else if((time<60*60)&&(time>=60*10)){
        //超过十分钟少于1小时
        s = Math.floor(time/60);
        return  s+"分钟前";
    }else if((time<60*60*24)&&(time>=60*60)){
        //超过1小时少于24小时
        s = Math.floor(time/60/60);
        return  s+"小时前";
    }else if((time<60*60*24*3)&&(time>=60*60*24)){
        //超过1天少于3天内
        s = Math.floor(time/60/60/24);
        return s+"天前";
    }else{
        //超过3天
        var date= new Date(parseInt(date) * 1000);
        return date.getFullYear()+"/"+(date.getMonth()+1)+"/"+date.getDate();
    }
}
时间: 2024-08-15 19:29:00

js 显示友好的时间格式【刚刚、几秒前,几小时,几天前(3天内) 时间格式化】的相关文章

模仿微信朋友圈发布时间,将过去时间格式化成xx(秒/分/小时/天)前

模仿微信朋友圈发布时间,使用扩展方法将将过去时间展示成xx(秒/分/小时/天)前,以留言列表中的留言时间为例,先来看一下直接的时间展示效果(date.ToString("yyyy/MM/dd HH:mm:ss")) 感觉很一般,没有什么特别 下面 我们写一个拓展方法,将留言时间格式化成xx(秒/分/小时/天)前 decimal.Truncate(data)//取decimal整数位 public static class HtmlExpansion { //只格式化2天内的时间 pub

ios 在中国地区,24小时时间格式 系统设定下 获得12小时制时间的方法

如题,在中国地区,24小时时间格式 系统设定下,如果单单使用 NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; 之后,无论用hh 还是用K,都无法取得12小时制的时间,都是24小时的. 需要加上一条设定 formatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];

Java中时间格式处理,指定N天/小时等之后的时间

1)根据当前时间,获取具体的时刻的时间 N天前 M小时之前 可用 new Date().getTime() - 24 * 60 * 60 * 1000*N[N天之前]的方法来获取处理时间之后的具体的值,最终转化为想要的时间格式 import java.text.SimpleDateFormat; import java.util.Date; public class getTime { public static void main(String[] args) { SimpleDateForm

js 显示友好的时间格式【刚刚、几分钟前、几小时、几天前、几周前、几月前等等 时间格式化】(工具类)

/** * 毫秒转换友好的显示格式 * 输出格式:21小时前 * stringTime为:年-月-日 时:分:秒 * @param {[type]} time [description] * @return {[type]} [description] */function friendlyFormatTime(stringTime) { let minute = 1000 * 60; let hour = minute * 60; let day = hour * 24; let week =

linux基础--时间格式

在linux中,经常会使用各种时间格式,特别在shell脚本中会经常调用,默认的时间格式为 [[email protected] ~]# date Wed Dec 14 19:43:07 CST 2016 注:如果拼接日期格式中有空格,则需要使用"..."来表示 常见格式有: %Y    年份 %m    月份(01-12) %d    按月计的日期(例如:01) %H    小时(00-23) %l    时(1-12) %M    分(00-59) %S    秒(00-60) %

Python中time模块和datetime模块的常用操作以及几种常用时间格式间的转换

最常见以及常用的几种时间格式 1.时间戳(timestamp),时间戳表示的是从1970年1月1日00:00:00开始按秒计算的偏移量. 2.时间元组(struct_time),共有九个元素组. 3.格式化时间(format time ),可以格式化为固定或者自定义格式,增加可读性. #!/usr/bin/env python # -*- coding:utf-8 -*- import time #时间戳格式,默认获取当前时间 1500029143.7640195 timestamp = tim

【解决方案】时间格式处理方案

.net DateTime paramDateTime = DateTime.Now;//如果paramGetDate==null,则使用当前时间 if (dateTime > 0) { //将long类型时间戳的参数转换为DateTime类型 paramDateTime = new DateTime(1970, 1, 1).AddMilliseconds(dateTime); } TimeSpan ts = DateTime.Now - new DateTime(1970, 1, 1); ts

.NET Core 中使用 Humanizer 显示友好时间格式

今天在将一个 .net framework 项目迁移至 .net core 的过程中,在迁移到显示友好时间格式(比如“1分钟前”,“1小时前”)的代码时,找了找看有没有对应的开源库,结果找到了 Humanizer ,顺手体验了一下,感觉不错,在这篇随笔中记录一下. 由于显示的是中文友好时间格式,需要安装 nuget 包 Humanizer.Core.zh-CN . <ItemGroup> <PackageReference Include="Humanizer.Core.zh-

EasyUI的DataGrid日期列(datebox)正确显示json时间格式

问题描述: 前端使用EasyUI,后台使用Spring MVC, 数据库里面存储的时间格式为:2014-06-10,但是后台返回给前台页面的数据是json格式的,类似于:1402367297000的形式,日期列datebox是无法解析的.具体如下图: 自己也是EasyUI小白,网上查查资料,倒腾下总算搞出来了,这里做下记录. 一般情况下我们所需的日期格式都是:2014-02-02或者2014/09/09形式的,因此首先要考虑实现一个添加日期格式化的插件. jQuery日期格式化 在自己的js中添