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 ...
随机推荐
- Entity Framework的事务提交
一组业务整体处理的行为叫一个事务.这一组的业务都能成功处理,我们就可以把这个事务提交来保存你已做的行为结果.事物的Commit是执行了你的方法进行了数据库的提交,之前的sava都是放在缓存中并没有执行 ...
- fullcalendar日历控件集合知识
1.基本的语法: 首先,fullcalendar和JQUERY一样,以面向对象的方式来组织代码.当然,这里的面向对象不过指能够把整个fullcalendar理解为一个类,这个类里包含有非常多的属性.方 ...
- 生命游戏(两),有一种东西叫CCScrollView
订婚app要么game'肯定不会陌生:CCScrollView并且CCTableView. 假如我不知道是什么CCScrollView,再看看testcpp要么testlua样品棒. 先说说CCScr ...
- UI 纯代码实现计算器
// MHTAppDelegate.h // TestCa // Copyright (c) 2014年 Summer. All rights reserved. #import <UIK ...
- Singleton模式(Singleton创建类型)c#简单的例子
单(Singleton创建模式)c#简单的例子 当需要生成一个实例,可单发模式 样品可以在短短的球员中产生,玩家和测试.单线程例子,如以下: namespace singletonpattern { ...
- 由Lucnene 对于预治疗的文字,全角半角转换器(可执行)
这是我第二次读这本书,在自己的学习之间XML,javascript,的深入研究<JAVA 核心技术>. 在当中深入的学习了java的非常多机制. 回头再来看搜索引擎这本书的时候.就认为比第 ...
- linux shell中间$0,$?,$!和<<'END'
变量说明: $$ Shell自己PID(ProcessID) $! Shell背景上次执行Process的PID $? 最后执行的命令结束码(回报值) $- 使用Set命令设定的Flag一览 $* 全 ...
- 无需Visual Studio,5容易的 - 分为报告
总报告设计,例如RDLC.水晶报表等.,需要安装Visual Studio.由VS提供报表设计界面设计报告,由VS设计报告.NET非常方便开发者,.但对于非开发,安装4G一个VS.并且需要Licens ...
- 开源 java CMS - FreeCMS2.1公布
项目地址:http://www.freeteam.cn/ FreeCMS商业版V2.1更新功能 1.web页面信息採集:通过简单配置就可以抓取目标网页信息,支持增量式採集.keyword替换.定时採集 ...
- 【剑指offer】打印1到最大的n数字
转载请注明出处:http://blog.csdn.net/ns_code/article/details/25562105 剑指offer第12题.九度OJ測试通过. 题目描写叙述: 给定一个数字N, ...