首页 > 代码库 > 工作笔记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
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。