Date、DateTime值的格式化扩展函数
public static class DateTimeExtensions
{
public static DateTime ToUtc(this DateTime time)
{
if (time.Kind == DateTimeKind.Utc)
{
return time;
}
else if (time.Kind == DateTimeKind.Local)
{
return time.ToUniversalTime();
}
else
{
return time.ToLocalTime().ToUniversalTime();
}
}
private static readonly DateTime s_maxDateTime = new DateTime(4000, 1, 1, 1, 1, 1, 1).ToUtc();
public static DateTime MaxDateTime
{
get
{
return s_maxDateTime;
}
}
public static bool IsMaxDateTime(this DateTime time)
{
return time.Year == MaxDateTime.Year;
}
public static DateTime? ToUtc(this DateTime? time)
{
return time.HasValue ? (DateTime?)time.Value.ToUtc() : null;
}
public static string ToChineseDate(this DateTime time)
{
return time.ToString("yyyy-MM-dd");
}
public static string ToChineseDate(this DateTime? time)
{
if (!time.HasValue)
return string.Empty;
return time.Value.ToString("yyyy-MM-dd");
}
public static string ToChineseTime(this DateTime time)
{
return time.ToString("yyyy-MM-dd HH:mm");
}
/// <summary>
/// 转换成时间戳
/// </summary>
/// <param name="time"></param>
/// <returns></returns>
public static Int64 ToTimestamp(this DateTime time)
{
return (time.ToUniversalTime().Ticks - 621355968000000000) / 10000000;
}
public static string ToExpireString(this DateTime time)
{
var now = DateTime.Now;
if (now < time)
return string.Empty;
var offset = (now - time);
if (offset < new TimeSpan(0, 1, 0))
{
return "刚刚";
}
else if (offset < new TimeSpan(1, 0, 0))
{
return string.Format("{0}分钟", offset.Minutes);
}
else if (offset < new TimeSpan(1, 0, 0, 0))
{
return string.Format("{0}小时", offset.Hours);
}
else if (offset < new TimeSpan(7, 0, 0, 0))
{
return string.Format("{0}天", offset.Days);
}
else if (offset < new TimeSpan(30, 0, 0, 0))
{
return string.Format("{0}周", offset.Days / 7);
}
else if (offset < new TimeSpan(365, 0, 0, 0))
{
return string.Format("{0}个月", offset.Days / 30);
}
else
{
return time.ToChineseTime();
}
}
public static DateTime GetThisMonday(this DateTime value)
{
int dayofWeek = value.DayOfWeek == DayOfWeek.Sunday ? 7 : (int)value.DayOfWeek;
return value.Date.AddDays((int)DayOfWeek.Monday - dayofWeek);
}
public static DateTime GetTheMinTime(this DateTime time)
{
return DateTime.Parse(time.ToShortDateString() + " 00:00:00");
}
public static DateTime GetTheMaxTime(this DateTime time)
{
return DateTime.Parse(time.ToShortDateString() + " 23:59:59");
}
public static string HtmlEncode(this DateTime value, string format)
{
return value.ToString(format).HtmlEncode();
}
public static string HtmlAttrEncode(this DateTime value, string format)
{
return value.ToString(format).HtmlAttrEncode();
}
public static string UrlEncode(this DateTime value, string format)
{
return value.ToString(format).UrlEncode();
}
}
Date、DateTime值的格式化扩展函数的更多相关文章
- Mysql 数据库date, datetime类型设置0000-00-00默认值(default)报错问题
Mysql 数据库date, datetime类型设置0000-00-00默认值报错问题 现象:MySQL5.7版本之后,date, datetime类型设置默认值"0000-00-00&q ...
- 转 数据库中的 date datetime timestamp的区别
转 数据库中的 date datetime timestamp的区别 DATETIME, DATE和TIMESTAMP类型是相关的.本文描述他们的特征,他们是如何类似的而又不同的. DATETIME类 ...
- django datetime format 日期格式化
django datetime format 日期格式化 www.jx-lab.com python 中 date,datetime,time对象都支持strftime(format)方法,但有一些区 ...
- MySQL数据库中的Date,DateTime,int,TimeStamp和Time类型的对比
DATETIME 用在你需要同时包含日期和时间信息的值时.MySQL检索并且以'YYYY-MM-DD HH:MM:SS'格式显示DATETIME值,支持的范围是'1000-01-01 00:00:00 ...
- 简述MySQL数据库中的Date,DateTime,TimeStamp和Time类型
DATETIME类型 定义同时包含日期和时间信息的值时.MySQL检索并且以'YYYY-MM-DD HH:MM:SS'格式显示DATETIME值,支持的范围是'1000-01-01 00:00:00' ...
- Mysql 实战关于date,datetime,timestamp类型使用
最近在做一个项目 项目中 不同的小伙伴同时在不同的业务模块中用到了date,datetime,timestamp这三个类型 特别是datetime,timestamp这两个 如果不能理解到位 其实很 ...
- 设置Input标签Date默认值为当前时间
需求:想设置Imput标签Date默认值为当前时间,通过JavaScript实现. <html> ...... <body> <input type="date ...
- mybatis的判定时间字段问题 java.lang.IllegalArgumentException: invalid comparison: cn.hutool.core.date.DateTime and java.lang.String
今天听组员说: mybatis在3.30版本及以上判定时间时 <if test="date_time != null and date_time != '' "> da ...
- Java连载78-深入自动拆装箱、Date类和SimpleDateFormat格式化
一.深入自动拆装箱 1.直接举例: public class D78_AutomaticUnpackingAndPacking{ public static void main(String[] ar ...
随机推荐
- URAL 1725. Sold Out!(数学啊 )
题目链接:space=1&num=1725" target="_blank">http://acm.timus.ru/problem.aspx?space= ...
- Android的第二次增加SurfaceView基本使用
本文来源于http://blog.csdn.net/hellogv/ ,引用必须注明出处. 上次介绍MediaPlayer的时候略微介绍了SurfaceView,SurfaceView因为能够直接从内 ...
- ShareSDK for Android 2.3.8它已发表
ShareSDK for Android 2.3.8已经公布,本次更新内容包含: 1.一键分享加入"摇一摇截图分享"功能 3.优化一键分享截图分享功能 4.一键分享编辑页界面微调 ...
- hdu1964之插头DP求最优值
Pipes Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Subm ...
- 【C语言探索之旅】 第二部分第二课:进击的指针,C语言的王牌!
内容简介 1.课程大纲 2.第二部分第二课: 进击的指针,C语言的王牌 3.第二部分第三课预告: 数组 课程大纲 我们的课程分为四大部分,每一个部分结束后都会有练习题,并会公布答案.还会带大家用C语言 ...
- 【SEO 决胜网络索引】 课程大纲及第一部分第一课:网络营销战略中的索引
内容简介 1.课程大纲 2.第一部分第一课: 网络营销战略中的索引 3.第一部分第二课预告: 索引是什么 课程大纲 现在是网络为王的时代,人们越来越离不开互联网: SEO(Search Engine ...
- Chapter 1 Securing Your Server and Network(10):使用扩展保护避免授权中继攻击
原文:Chapter 1 Securing Your Server and Network(10):使用扩展保护避免授权中继攻击 原文出处:http://blog.csdn.net/dba_huang ...
- 给Activity设置Dialog属性,点击区域外消失;
1.在AndroidManifest.xml中给Activity设置样式: <activity android:name=".MyActivity" ...
- 3D数学--学习笔记(五岁以下儿童):总结一些概念(避免遗忘!)
下面是一些概念只是一个简单的解释,这里是它的一个简单的了解! 当人们谈论,我能理解有关. 1.正交投影: 投影.这意味着降维操作. 全部的点都被拉平至垂直的轴(2D)或平面(3D)上.这样的类型的投影 ...
- SQL 2008 SP2 找不到SQL Server Engine
原文:SQL 2008 SP2 找不到SQL Server Engine 最近我有个客户碰到一个很奇怪的问题.他安装SQL server 2008 SP2的时候, SP2的安装程序无法找到SQL se ...