Js 日期 多少分钟前,多少秒前

;(function(window){
    /**
     * [dateDiff 算时间差]
     * @param  {[type=Number]} hisTime [历史时间戳,必传]
     * @param  {[type=Number]} nowTime [当前时间戳,不传将获取当前时间戳]
     * @return {[string]}         [string]
     */
    var dateDiff = function(hisTime,nowTime){
        if(!arguments.length) return ‘‘;
        var arg = arguments,
            now =arg[1]?arg[1]:new Date().getTime(),
            diffValue = now - arg[0],
            result=‘‘,

            minute = 1000 * 60,
            hour = minute * 60,
            day = hour * 24,
            halfamonth = day * 15,
            month = day * 30,
            year = month * 12,

            _year = diffValue/year,
            _month =diffValue/month,
            _week =diffValue/(7*day),
            _day =diffValue/day,
            _hour =diffValue/hour,
            _min =diffValue/minute;

            if(_year>=1) result=parseInt(_year) + "年前";
            else if(_month>=1) result=parseInt(_month) + "个月前";
            else if(_week>=1) result=parseInt(_week) + "周前";
            else if(_day>=1) result=parseInt(_day) +"天前";
            else if(_hour>=1) result=parseInt(_hour) +"个小时前";
            else if(_min>=1) result=parseInt(_min) +"分钟前";
            else result="刚刚";
            return result;
    }
    window.dateDiff = dateDiff
})(window);
dateDiff(new Date(‘1987-04-03‘).getTime()) //一定要转换成时间戳

  

时间: 2024-08-25 10:26:48

Js 日期 多少分钟前,多少秒前的相关文章

long类型的时间转为n秒前n分钟前n小时前或者日期

1 package com.tai.use; 2 3 import java.text.DateFormat; 4 import java.text.SimpleDateFormat; 5 import java.util.Calendar; 6 import java.util.Date; 7 8 public class GuiYI 9 { 10 /** 11 * GuiYI 诡异的需求 12 * 转为 n秒前 n分钟前 n小时前 日期 13 * @param time 14 * @para

js把字符串格式的时间转换成几秒前、几分钟前、几小时前、几天前等格式

最近在做项目的时候,需要把后台返回的时间转换成几秒前.几分钟前.几小时前.几天前等的格式:后台返回的时间格式为:2015-07-30 09:36:10,需要根据当前的时间与返回的时间进行对比,最后显示成几秒前.几分钟前.几小时前.几天前的形式. 1.由于返回的时间是字符串格式,所以要先转换成时间戳 //字符串转换为时间戳 function getDateTimeStamp (dateStr) { return Date.parse(dateStr.replace(/-/gi,"/"))

php 显示某一个时间相当于当前时间在多少秒前,多少分钟前,多少小时前

/* *function:显示某一个时间相当于当前时间在多少秒前,多少分钟前,多少小时前 *timeInt:unix time时间戳 *format:时间显示格式 */ public function timeFormat($timeInt,$format='Y-m-d H:i:s'){ if(empty($timeInt)||!is_numeric($timeInt)||!$timeInt){ return ''; } $d=time()-$timeInt; if($d<0){ return

使用C#把发表的时间改为几年前,几个月,几天前,几小时前,几分钟前,或几秒前

我们在评论中往往会看到多少天前,多少小时前. 实现原理:现在时间-过去时间 得到的时间差来做比较 下面我定义了一个Helper类,大家直接引用即可,参数就是时间差,注意时间差类型是TimeSpan类型,而不是DateTime类型哦~ TimeHelper.cs using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace 时间测试 { /// <summary> /

java几秒前,几分钟前,几小时前,几天前,几月前,几年前的实现

原文:java几秒前,几分钟前,几小时前,几天前,几月前,几年前的实现 源代码下载地址:http://www.zuidaima.com/share/1562038902000640.htm 以前有牛人分享的代码: java实现几分钟前,几小时前,几天前的代码 有点复杂,所以在stackoverflow上找了一个简单的,改写了下符合需求,给大家分享下. package com.date; import java.text.ParseException; import java.text.Simpl

C# 获取时间差(几天前,几小时前,几分钟前,几秒前)

1 #region 获取时间差string GetTime(BsonString getTime) 2 /// <summary> 3 /// 获取时间差 4 /// </summary> 5 /// <param name="getTime">数据库时间</param> 6 /// <returns>时间差</returns> 7 private string GetTime(BsonString getTime

asp.net 格式化显示时间为几个月,几天前,几小时前,几分钟前,或几秒前

public static string DateFormatToString(DateTime dt) { TimeSpan span = (DateTime.Now - dt).Duration(); if (span.TotalDays > 60) { return dt.ToString("yyyy-MM-dd"); } else if (span.TotalDays > 30) { return "1个月前"; } else if (span.

几秒前,几分钟前,几小时前,几天前,几月前,几年前的java实现

package com.zuidaima; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class RelativeDateFormat { private static final long ONE_MINUTE = 60000L; private static final long ONE_HOUR = 3600000L; private s

《Vue系列》timeago.js将时间戳转换成“几天前”“几分钟前”等格式

<Vue系列>timeago.js将时间戳转换成"几天前""几分钟前"等格式 原文地址:https://www.cnblogs.com/zxlb/p/12318275.html