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 ...
随机推荐
- 采用ACE登录设施(一)HelloWorld
(1)开始使用日志设施 使用日志设施,总是要包括头文件: #include "ace/Log_Msg.h" ACE日志的Hello World #ifdef _DEBUG #pra ...
- Byte[]和BASE64之间的转换
一. BASE64编码 把byte[]中的元素当做无符号八位整数转换成只含有64个基本字符的字符串,这些基本字符是: l 大写的A-Z l 小写的a-z l 数字0-9 l '+' 和 '/' l 空 ...
- Linux下如何查看高CPU占用率线程 LINUX CPU利用率计算(转)
Java 系统性能分析 命令 1. cpu分析 top , pidstat(sysstat) pid -p PID -t 1 10 vmstat 1 CPU上下文切换.运行队列.利用率 ps Hh - ...
- NET5实践:项目创建-结构概述-程序运行-发布部署
ASP.NET5实践01:项目创建-结构概述-程序运行-发布部署 1.项目创建 ASP.NET5项目模板有三种: 新建项目: 选择模板: 2.结构概述 References对应配置是project ...
- AndroidUI的组成部分ProgressBar
package com.gc.progressbar; /* * 1.ProgressBar组件也是一组重要的组件,ProgressBar本身代表了进度条组件, * 它还派生了两个经常使用的组件:Se ...
- HDU2516-取石子游戏
取石子游戏 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Subm ...
- BZOJ3362 [Usaco2004 Feb]Navigation Nightmare 导航噩梦
标题效果:自脑补. 思维:与维护两个维度和可设置为检查右. 注意,标题给予一堆关系的.我们应该加入两对关系. Code: #include <cstdio> #include <cs ...
- ubuntu 下搭建apache+python的运行环境
ubuntu下怎么搭建apache+python运行环境,可以参考http://www.01happy.com/ubuntu-apache-mod-python/ ,这里只是简单的记录下步骤,本文主要 ...
- 成不了天才,但为何也没成"人材"?(转)
长期以来,"软件业"一直被视为"智力密集"型的"朝阳"产业,大多数从业者都受过高等教育,其平均素质居于社会各行业的前列,这个产业的顶尖人物被 ...
- LeetCode之Sort List
称号:Sort a linked list in O(n log n) time using constant space complexity. 对一个单链表进行排序,要求时间复杂度为O(n log ...