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. PHP读取Excel里的文件

    下载phpExcelReader  http://sourceforge.net/projects/phpexcelreader 解压后得到以下这些文件 jxlrwtest.xls这个excel文件有 ...

  2. effective c++ 条款8 prevent exception from leaving destructor

    1 析构函数绝对不要吐出异常.如果一个析构函数可能调用产生异常的函数,析构函数应该不传播该异常或结束程序 2 如果客户需要对某个操作函数运行期间抛出的异常做出反应,那么class应该提供一个普通函数执 ...

  3. 升级到tomcat8时Artifact SpringMvcDemo:war exploded: Server is not connected. Deploy is not

    The method getDispatcherType() is undefined for the type HttpServletRequest 升级到tomcat8 http://segmen ...

  4. 解决visual studio空格变成很多点号的3种方法

    在用visual studio做网站时不知道按了什么快捷键,所有页面上的空格都变成了点号,就像下图那样. 要解决空格变点号的方法有两种:1.编辑->高级->查看空白2.Ctrl+E 然后按 ...

  5. 带项目的一些体会以及合格的 Leader 应该具备什么特质?(转)

    许多项目有这样几种 Leader: 1. 泛泛而谈型 很多时候 Leader 仅仅给出一个大方向,提一些高屋建瓴的理论方向,事情还是交由普通开发人员去做.完了可能又会回头埋怨开发人员的水平不行,没有达 ...

  6. w3wp占用CPU过高

    w3wp占用CPU过高 在此之前项目有发生过两次类似的状况,都得以解决,但最近又会发现偶尔CPU会跑满,虽然之前使用过WinDbg解决过两次问题但人的记忆是不可靠的,今天处理同样问题的时候还是遇到了一 ...

  7. STL--F - Sequence(n*m-&gt;之前的最低要求m个月)

    F - Sequence Time Limit:6000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit ...

  8. j经常使用ava应用server

    什么是应用server它?它主要提供的执行环境的应用程序,为组件提供服务. 要了解更多关于,您可以查看我的博客:何为容器? Java 的应用server非常多,从功能上分为两类:WEB 应用serve ...

  9. cocos2d-x删除本地存储的文件UserDefault.xml方法——白费

    许多其他的精彩分享:http://blog.csdn.net/u010229677 首先获取UserDefault的存储位置.然后remove就可以: remove( UserDefault::get ...

  10. 避免内存重叠memmove()性能

    #include <iostream> #include <string.h> using namespace std; void* memmove(void *dst, co ...