zoukankan
html css js c++ java
返回友好时间显示,如*天前,本周四,昨天等
using
System;
using
System.Globalization;
namespace
CRM_Support.BLL
{
public
class
DateTimeHelper
{
返回友好时间显示
#region
返回友好时间显示
带时间
#region
带时间
public
static
string
GetManReadable(
string
datetime)
{
try
{
return
GetManReadable(Convert.ToDateTime(datetime));
}
catch
{
return
datetime;
}
}
public
static
string
GetManReadable(
object
datetime)
{
try
{
return
GetManReadable(Convert.ToDateTime(datetime));
}
catch
{
return
datetime.ToString();
}
}
public
static
string
GetManReadable(DateTime datetime)
{
string
time
=
datetime.ToShortTimeString();
return
GetShortManReadable(datetime)
+
"
"
+
time;
}
#endregion
不带时间
#region
不带时间
public
static
string
GetShortManReadable(
string
datetime)
{
if
(
string
.IsNullOrEmpty(datetime.Trim()))
return
string
.Empty;
try
{
return
GetShortManReadable(Convert.ToDateTime(datetime));
}
catch
{
return
datetime;
}
}
public
static
string
GetShortManReadable(
object
datetime)
{
if
(datetime
==
null
||
datetime.ToString().Trim()
==
string
.Empty)
return
string
.Empty;
try
{
return
GetShortManReadable(Convert.ToDateTime(datetime));
}
catch
{
return
datetime.ToString();
}
}
public
static
string
GetShortManReadable(DateTime datetime)
{
DateTime now
=
DateTime.Now;
if
(now.Year
==
datetime.Year)
//
以下的前提是两时间都为同一年
{
TimeSpan span
=
now.Date
-
datetime.Date;
int
days
=
span.Days;
switch
(days)
{
case
1
:
return
"
昨天
"
;
case
0
:
return
"
今天
"
;
case
-
1
:
return
"
明天
"
;
default
:
break
;
}
if
(days
>=
-
14
||
days
<=
14
)
{
GregorianCalendar gc
=
new
GregorianCalendar();
int
dateWeekofYear
=
gc.GetWeekOfYear(datetime, CalendarWeekRule.FirstDay, DayOfWeek.Monday);
int
nowWeekofYear
=
gc.GetWeekOfYear(now, CalendarWeekRule.FirstDay, DayOfWeek.Monday);
string
dateDayofWeek
=
gc.GetDayOfWeek(datetime).ToString();
int
weeks
=
nowWeekofYear
-
dateWeekofYear;
switch
(weeks)
{
case
1
:
return
string
.Format(
"
上周{0}
"
, WhichDay(dateDayofWeek));
case
0
:
return
string
.Format(
"
本周{0}
"
, WhichDay(dateDayofWeek));
case
-
1
:
return
string
.Format(
"
下周{0}
"
, WhichDay(dateDayofWeek));
default
:
break
;
}
}
if
(days
>=
-
62
||
days
<=
62
)
{
int
months
=
now.Month
-
datetime.Month;
int
dayofMonth
=
datetime.Day;
switch
(months)
{
case
1
:
return
string
.Format(
"
上月{0}号
"
, dayofMonth);
case
0
:
return
string
.Format(
"
本月{0}号
"
, dayofMonth);
case
-
1
:
return
string
.Format(
"
下月{0}号
"
, dayofMonth);
default
:
break
;
}
}
}
else
//
以下的前提是两时间不同年
{
}
return
datetime.ToShortDateString();
}
#endregion
public
static
string
WhichDay(
string
enWeek)
{
switch
(enWeek.Trim())
{
case
"
Sunday
"
:
return
"
日
"
;
case
"
Monday
"
:
return
"
一
"
;
case
"
Tuesday
"
:
return
"
二
"
;
case
"
Wednesday
"
:
return
"
三
"
;
case
"
Thursday
"
:
return
"
四
"
;
case
"
Friday
"
:
return
"
五
"
;
case
"
Saturday
"
:
return
"
六
"
;
default
:
return
enWeek;
}
}
#endregion
生日提醒
#region
生日提醒
public
static
string
GetBirthdayTip(DateTime birthday)
{
DateTime now
=
DateTime.Now;
//
TimeSpan span = DateTime.Now - birthday;
int
nowMonth
=
now.Month;
int
birtMonth
=
birthday.Month;
if
(nowMonth
==
12
&&
birtMonth
==
1
)
return
string
.Format(
"
下月{0}号
"
, birthday.Day);
if
(nowMonth
==
1
&&
birtMonth
==
12
)
return
string
.Format(
"
上月{0}号
"
, birthday.Day);
int
months
=
now.Month
-
birthday.Month;
//
int days = now.Day - birthday.Day;
if
(months
==
1
)
return
string
.Format(
"
上月{0}号
"
, birthday.Day);
else
if
(months
==
-
1
)
return
string
.Format(
"
下月{0}号
"
, birthday.Day);
else
if
(months
==
0
)
{
if
(now.Day
==
birthday.Day)
return
"
今天
"
;
return
string
.Format(
"
本月{0}号
"
, birthday.Day);
}
else
return
birthday.ToShortDateString();
}
public
static
string
GetBirthdayTip(
string
birthday)
{
try
{
return
GetBirthdayTip(Convert.ToDateTime(birthday));
}
catch
{
return
birthday;
}
}
#endregion
其他日期相关
#region
其他日期相关
/**/
///
<summary>
///
返回日期加短时间格式
///
</summary>
///
<param name="dt"></param>
///
<returns></returns>
public
static
string
GetDateShortTime(DateTime dt)
{
return
dt.ToString(
"
yyyy-MM-dd hh:mm
"
);
}
public
static
string
GetDateShortTime(
object
o1)
{
try
{
return
GetDateShortTime(Convert.ToDateTime(o1));
}
catch
{
return
o1.ToString();
}
}
/**/
///
<summary>
///
返回短日期
///
</summary>
///
<param name="dt"></param>
///
<returns></returns>
public
static
string
GetShortDate(DateTime dt)
{
return
dt.ToString(
"
yyyy-MM-dd
"
);
}
public
static
string
GetShortDate(
object
o1)
{
try
{
return
GetShortDate(Convert.ToDateTime(o1));
}
catch
{
return
o1.ToString();
}
}
/**/
///
<summary>
///
获取下个月是几月
///
</summary>
///
<param name="date"></param>
///
<returns></returns>
public
static
int
GetPreviousMonth(DateTime date)
{
return
date.AddMonths(
-
1
).Month;
}
/**/
///
<summary>
///
获取当月是几月
///
</summary>
///
<param name="date"></param>
///
<returns></returns>
public
static
int
GetThisMonth(DateTime date)
{
return
date.Month;
}
/**/
///
<summary>
///
获取下个月是几月
///
</summary>
///
<param name="date"></param>
///
<returns></returns>
public
static
int
GetNextMonth(DateTime date)
{
return
date.AddMonths(
1
).Month;
}
/**/
///
<summary>
///
获取前或后几个月是几月
///
</summary>
///
<param name="i"></param>
///
<param name="date"></param>
///
<param name="year"></param>
///
<returns></returns>
public
static
int
GetMonth(
int
i, DateTime date,
out
int
year)
{
DateTime time
=
date.AddMonths(i);
year
=
time.Year;
return
time.Month;
}
public
static
string
GetWeek()
{
return
string
.Empty;
}
#endregion
}
}
查看全文
相关阅读:
PowerShell Arrays
PowerShell Hashtable
PowerShell Variables
MVC3 类型“System.Web.Mvc.ModelClientValidationRule”同时存在
神奇的Timer
神奇的Timer之lock篇
利用lambda表达式正确关闭WCF连接
Zendstutio设置
一个服务器部署多个项目
Magento模块配置文件
原文地址:https://www.cnblogs.com/vagerent/p/1227975.html
最新文章
红龙书随书源码第一例D3D9 Init在VS2019下编译通过的方法
用于网络测速的python脚本speedtest (转载)
《Direct3D中的2D编程》随书源码在vs2019中编译通过
解决LINK : fatal error LNK1104: 无法打开文件“d3dx9.lib”的问题
C++的string类的c_str()方法和C的字符指针的关系具体见 C++ primer第5版第111页
Linux下如何将c语言编译时输出的大量信息全部保存至文件
minix3下编译C和C++程序
看完红龙书之后推荐看 Microsoft DirectX SDK (June 2010) 安装目录下面自带的官方sample例子代码
Debug和Release的区别是什么(非常详细)?(转载)
VirtualBox安装Minix3(转载)
热门文章
解决vs2019生成的文件很大的问题
vs2019报错说 E0167 "const wchar_t *" 类型的实参与 "LPCSTR" 类型的形参不兼容
error C4430: missing type specifier int assumed. Note: C++ does not support defaultint 解决方法
谈AOP要step by step
关于silverlight的xap包与dll分离的一些事儿
【让原控件见鬼去吧】之打造可编辑的DropdownList
经典网页设计404页面第二季
PowerShell Operators
OSCP Security Technology Pivoting
PowerShell Pipelines
Copyright © 2011-2022 走看看