创新互联从2013年创立,是专业互联网技术服务公司,拥有项目成都做网站、网站制作网站策划,项目实施与项目整合能力。我们以让每一个梦想脱颖而出为使命,1280元金凤做网站,已为上家服务,为金凤各地企业和个人服务,联系电话:18980820575>using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Beehive.CloudReader.Utility
{
public static class DateTimeExtensions
{
/// ///
/// /// /// public static string ToRelativeDateTimeT(this DateTime dateTime)
{
var time = DateTime.Now - dateTime;
//如果不在同一年 则直接显示年月日 if ((int.Parse(dateTime.ToString("yyyy")) - int.Parse(DateTime.Now.ToString("yyyy"))) != 0) // if (time.TotalDays > 365) return dateTime.ToString("yyyy-MM-dd");
if (time.TotalDays > 2)
return dateTime.ToString("M月d日 HH:mm");
else
{
var day = DateTime.Now.Date - dateTime.Date;
if (day.Days == 2)
return string.Format("前天{0:HH:mm}", dateTime);
if (day.Days == 1)
return string.Format("昨天{0:HH:mm}", dateTime);
if (time.TotalMinutes > 60)
return string.Format("今天{0:HH:mm}", dateTime);
if (time.TotalSeconds > 60)
return string.Format("{0}分钟前", time.Minutes);
if (time.TotalSeconds < 60 && time.TotalDays >= 0)
return "刚刚";
else
{
if (time.Days == 0 && dateTime.Date == DateTime.Now.Date)
return string.Format("今天{0:HH:mm}", dateTime);
if (time.TotalDays >= -1)
return string.Format("明天{0:HH:mm}", dateTime);
if (time.TotalDays < -1 && time.TotalDays > -2)
return string.Format("后天{0:HH:mm}", dateTime);
return dateTime.ToString("M月d日 HH:mm");
}
}
}
public static string ToRelativeDateTimeDay(this DateTime dateTime)
{
var time = DateTime.Now - dateTime;
//如果不在同一年 则直接显示年月日 if ((int.Parse(dateTime.ToString("yyyy")) - int.Parse(DateTime.Now.ToString("yyyy"))) != 0) // if (time.TotalDays > 365) return dateTime.ToString("yyyy-MM-dd");
if (time.TotalDays > 2)
return dateTime.ToString("M月d日");
else
{
var day = DateTime.Now.Date - dateTime.Date;
if (day.Days == 2)
return string.Format("前天", dateTime);
if (day.Days == 1)
return string.Format("昨天", dateTime);
if (time.TotalMinutes > 60)
return string.Format("{0:HH:mm}", dateTime);
if (time.TotalSeconds > 60)
return string.Format("{0}分钟前", time.Minutes);
if (time.TotalSeconds < 60 && time.TotalDays >= 0)
return "刚刚";
else
{
if (time.Days == 0 && dateTime.Date == DateTime.Now.Date)
return string.Format("{0:HH:mm}", dateTime);
if (time.TotalDays >= -1)
return string.Format("明天", dateTime);
if (time.TotalDays < -1 && time.TotalDays > -2)
return string.Format("后天", dateTime);
return dateTime.ToString("M月d日");
}
}
}
/// ///
/// /// /// public static long ToTimeStamp(this DateTime dateTime)
{
var stamp = dateTime.Ticks - new DateTime(1970, 1, 1).Ticks;
return stamp / 10000000 - 8 * 60 * 60;
}
public static string ToWelcome(this DateTime dateTime)
{
var hour = DateTime.Now.Hour;
var msg = "";
if (hour >= 6 && hour < 8)
{
msg= "早上好";
}
else if (hour >= 8 && hour < 11)
{
msg= "上午好";
}
else if (hour >= 11 && hour < 13)
{
msg= "中午好";
}
else if (hour >= 13 && hour < 18)
{
msg= "下午好";
}
else
{
msg= "晚上好";
}
return msg;
}
}
}
当前文章:时间转换:DateTimeExtensions-创新互联
分享网址:
http://cdweb.net/article/hdphh.html