码迷,mamicode.com
首页 > 其他好文 > 详细

工作笔记5

时间:2017-05-08 19:20:00      阅读:234      评论:0      收藏:0      [点我收藏+]

标签:timestamp   turn   convert   int   time   mon   function   工作   笔记   

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 = 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 = 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

标签:timestamp   turn   convert   int   time   mon   function   工作   笔记   

原文地址:http://www.cnblogs.com/len0031/p/6826802.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!