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. [LeetCode]Palindrome Partitioning 找出所有可能的组合回文

    给定一个字符串,切割字符串,这样每个子字符串是一个回文字符串. 要找出所有可能的组合. 办法:暴力搜索+回溯 class Solution { public: int *b,n; vector< ...

  2. 如何将IPhone应用软件发布到App Store的

    转自:http://www.shtion.com/667.html 怎样将IPhone应用程序软件公布到应用程序商店? 2009年10月19日公布 分类: App store, iphone, 手机应 ...

  3. swift的struct本节描述结构的类型

    <span style="font-size:24px;">struct David { var x = 0;//一个结构的定义,两个字段x,y var y = 0;/ ...

  4. myEclipse勿删文件怎么恢复

    今天码代码的时候项目里有一个jsp文件不小心被删了,又懒得重写,然后发现myEclipse竟然可以恢复被勿删的文件,当然,也仅仅限于最近被删的文件. 具体怎么恢复呢?-------右键点击被删文件所在 ...

  5. poj 3414 Pots (bfs+线索)

    Pots Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10071   Accepted: 4237   Special J ...

  6. POJ 2155 D区段树

    POJ 2155  D区段树 思考:D区段树是每个节点设置一个段树树. 刚開始由于题目是求A[I,J],然后在y查询那直接ans^=Map[i][j]的时候没看懂.后面自己把图画出来了才理解. 由于仅 ...

  7. 常用Android开源框架

    1.volley 项目地址 https://github.com/smanikandan14/Volley-demo  (1)  JSON,异步下载图片:  (2)  网络请求的排序(scheduli ...

  8. MAC随机修改批处理

    原文:MAC随机修改批处理 @echo off mode con cols=70 lines=20 title MAC随机修改工具 color 3F setlocal enabledelayedexp ...

  9. 安装framework 3.5出现0x800F0922的解决方法

    关闭Windows 防火墙,启动Windows model install服务 重启! 再安装 就ok了

  10. 2.Cocos2dx 3.2重力系统Box2D

     1 加入Box2D相关的库 步骤1:右击项目所在的解决方式à加入->现有项目àE:\Installed\cocos2d-x-3.2\cocos2d-x-3.2\external\Box2D ...