首页 > 代码库 > 工作笔记5

工作笔记5

1、js 时间转化为几天前、几小时前、几分钟前

function getDateDiff(dateTimeStamp){
    var minute = 1000 * 60;
    var hour = minute * 60;
    var day = hour * 24;
    var halfamonth = day * 15;
    var month = day * 30;
    var now = new Date().getTime();
    var diffValue = http://www.mamicode.com/now - dateTimeStamp;
    if(diffValue < 0){return;}
    var monthC =diffValue/month;
    var weekC =diffValue/(7*day);
    var dayC =diffValue/day;
    var hourC =diffValue/hour;
    var minC =diffValue/minute;
    if(monthC>=1){
        result="" + parseInt(monthC) + "月前";
    }
    else if(weekC>=1){
        result="" + parseInt(weekC) + "周前";
    }
    else if(dayC>=1){
        result=""+ parseInt(dayC) +"天前";
    }
    else if(hourC>=1){
        result=""+ parseInt(hourC) +"小时前";
    }
    else if(minC>=1){
        result=""+ parseInt(minC) +"分钟前";
    }else
    result="刚刚";
    return result;
}

   C# 时间转化为几天前、几小时前、几分钟前

    private string GetDateDiff(int dateTimeStamp) {
        string result = string.Empty;
        var minute = 60;
        var hour = minute * 60;
        var day = hour * 24;
        var halfamonth = day * 15;
        var month = day * 30;
        var now = GetTimeStamp();
        var diffValue = http://www.mamicode.com/now - dateTimeStamp;
        if (diffValue < 0) { return ""; }
        var monthC = diffValue / month;
        var weekC = diffValue / (7 * day);
        var dayC = diffValue / day;
        var hourC = diffValue / hour;
        var minC = diffValue / minute;
        if (monthC >= 1) {
            result = "" + Convert.ToInt32(monthC) + "月前";
        } else if (weekC >= 1) {
            result = "" + Convert.ToInt32(weekC) + "周前";
        } else if (dayC >= 1) {
            result = "" + Convert.ToInt32(dayC) + "天前";
        } else if (hourC >= 1) {
            result = "" + Convert.ToInt32(hourC) + "小时前";
        } else if (minC >= 1) {
            result = "" + Convert.ToInt32(minC) + "分钟前";
        } else {
            result = "刚刚";
        }
        return result;
    }

    public Int64 GetTimeStamp() {
        TimeSpan ts = DateTime.Now - new DateTime(1970, 1, 1, 0, 0, 0, 0);
     //去掉毫秒数
var time = ts.TotalMilliseconds.ToString().Substring(0, 10); return Convert.ToInt64(time); }

 

工作笔记5