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值的格式化扩展函数的更多相关文章

  1. Mysql 数据库date, datetime类型设置0000-00-00默认值(default)报错问题

    Mysql 数据库date, datetime类型设置0000-00-00默认值报错问题 现象:MySQL5.7版本之后,date, datetime类型设置默认值"0000-00-00&q ...

  2. 转 数据库中的 date datetime timestamp的区别

    转 数据库中的 date datetime timestamp的区别 DATETIME, DATE和TIMESTAMP类型是相关的.本文描述他们的特征,他们是如何类似的而又不同的. DATETIME类 ...

  3. django datetime format 日期格式化

    django datetime format 日期格式化 www.jx-lab.com python 中 date,datetime,time对象都支持strftime(format)方法,但有一些区 ...

  4. MySQL数据库中的Date,DateTime,int,TimeStamp和Time类型的对比

    DATETIME 用在你需要同时包含日期和时间信息的值时.MySQL检索并且以'YYYY-MM-DD HH:MM:SS'格式显示DATETIME值,支持的范围是'1000-01-01 00:00:00 ...

  5. 简述MySQL数据库中的Date,DateTime,TimeStamp和Time类型

    DATETIME类型 定义同时包含日期和时间信息的值时.MySQL检索并且以'YYYY-MM-DD HH:MM:SS'格式显示DATETIME值,支持的范围是'1000-01-01 00:00:00' ...

  6. Mysql 实战关于date,datetime,timestamp类型使用

    最近在做一个项目 项目中 不同的小伙伴同时在不同的业务模块中用到了date,datetime,timestamp这三个类型 特别是datetime,timestamp这两个 如果不能理解到位  其实很 ...

  7. 设置Input标签Date默认值为当前时间

    需求:想设置Imput标签Date默认值为当前时间,通过JavaScript实现. <html> ...... <body> <input type="date ...

  8. 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 ...

  9. Java连载78-深入自动拆装箱、Date类和SimpleDateFormat格式化

    一.深入自动拆装箱 1.直接举例: public class D78_AutomaticUnpackingAndPacking{ public static void main(String[] ar ...

随机推荐

  1. Java线程Dump分析工具--jstack(转)

    jstack用于打印出给定的java进程ID或core file或远程调试服务的Java堆栈信息,如果是在64位机器上,需要指定选项"-J-d64",Windows的jstack使 ...

  2. 2014在百度之星资格赛的第二个问题Disk Schedule

    事实上,我认为它可以用来费用流问题.但光建地图上加班. ..不科学啊.. . 因副作用太大,否则,必然在.最后,想啊想,或者使用dp对.... 别想了一维dp... .我不知道我是怎么想.无论如何,这 ...

  3. Facebook新框架React Native,一套搞定App开发[转]

    Facebook新框架React Native,一套搞定App开发 本文来自微信公众号“给产品经理讲技术”(pm_teacher),欢迎关注. 做为一名产品经理,你是否遇到过这样的窘境,“帮我把字体调 ...

  4. MSSQL连接字符串,你真的清楚吗?

    原文:MSSQL连接字符串,你真的清楚吗? 几年前当我第一次面试时,考官发现我是个新手于是他让我写个连接字符串,虽然当时就知道X种连接字符串的写法,但是当时却没能写对一个,工作多年后我仍然不能写一个正 ...

  5. 微信小程序开发者工具集合包

    开发论坛 http://www.henkuai.com/forum-56-1.html 工具包下载  https://yunpan.cn/ckXFpuzAeVi2s  访问密码 b4cc 开发文档 h ...

  6. 编程算法 - 最小的k个数 红黑树 代码(C++)

    最小的k个数 红黑树 代码(C++) 本文地址: http://blog.csdn.net/caroline_wendy 题目: 输入n个整数, 找出当中的最小k个数. 使用红黑树(multiset) ...

  7. Hibernate HQL详细说明

    1.  Hibernate HQL详细说明 1.1.  hql一个简短的引论 Hibernate它配备了一种非常强大的查询语言.这种语言看起来非常像SQL.但是不要 要对相位的语法结构似,HQL是很有 ...

  8. html/css获得第一章

    1.基本教程来学习 大概3天课余时间阅读下面的两个教程. HTML文字教程 CSS文字教程 2.练习 看完教程后.做第一练习时,总结例如以下: 1)div居中 须要设置属性:margin-left:a ...

  9. Intent用法

    Intent是android系统中的最佳男主角,Intent翻译成中文的意思是"意图",说白了就是"我想要...",也就是说眼下运行中的Activity想要请其 ...

  10. Windows 8 键盘上推自定义处理

    原文:Windows 8 键盘上推自定义处理 在Windows 8 应用程序中,当TextBox控件获得焦点时,输入面板会弹出,如果TextBox控件处于页面下半部分,则系统会将页面上推是的TextB ...