//C#  datetime 格式化
DateTime dt = DateTime.Now;
Label1.Text = dt.ToString();//2005-11-5 13:21:25
Label2.Text = dt.ToFileTime().ToString();//
Label3.Text = dt.ToFileTimeUtc().ToString();//
Label4.Text = dt.ToLocalTime().ToString();//2005-11-5 21:21:25
Label5.Text = dt.ToLongDateString().ToString();//2005年11月5日
Label6.Text = dt.ToLongTimeString().ToString();//13:21:25
Label7.Text = dt.ToOADate().ToString();//38661.5565508218
Label8.Text = dt.ToShortDateString().ToString();//2005-11-5
Label9.Text = dt.ToShortTimeString().ToString();//13:21
Label10.Text = dt.ToUniversalTime().ToString();//2005-11-5 5:21:25
//2005-11-5 13:30:28.4412864
Label1.Text = dt.Year.ToString();//
Label2.Text = dt.Date.ToString();//2005-11-5 0:00:00
Label3.Text = dt.DayOfWeek.ToString();//Saturday
Label4.Text = dt.DayOfYear.ToString();//
Label5.Text = dt.Hour.ToString();//
Label6.Text = dt.Millisecond.ToString();//
Label7.Text = dt.Minute.ToString();//
Label8.Text = dt.Month.ToString();//
Label9.Text = dt.Second.ToString();//
Label10.Text = dt.Ticks.ToString();//
Label11.Text = dt.TimeOfDay.ToString();//13:30:28.4412864
Label1.Text = dt.ToString();//2005-11-5 13:47:04
Label2.Text = dt.AddYears().ToString();//2006-11-5 13:47:04
Label3.Text = dt.AddDays(1.1).ToString();//2005-11-6 16:11:04
Label4.Text = dt.AddHours(1.1).ToString();//2005-11-5 14:53:04
Label5.Text = dt.AddMilliseconds(1.1).ToString();//2005-11-5 13:47:04
Label6.Text = dt.AddMonths().ToString();//2005-12-5 13:47:04
Label7.Text = dt.AddSeconds(1.1).ToString();//2005-11-5 13:47:05
Label8.Text = dt.AddMinutes(1.1).ToString();//2005-11-5 13:48:10
Label9.Text = dt.AddTicks().ToString();//2005-11-5 13:47:04
Label10.Text = dt.CompareTo(dt).ToString();//0
//Label11.Text = dt.Add(?).ToString();//问号为一个时间段
Label1.Text = dt.Equals("2005-11-6 16:11:04").ToString();//False
Label2.Text = dt.Equals(dt).ToString();//True
Label3.Text = dt.GetHashCode().ToString();//
Label4.Text = dt.GetType().ToString();//System.DateTime
Label5.Text = dt.GetTypeCode().ToString();//DateTime Label1.Text = dt.GetDateTimeFormats('s')[].ToString();//2005-11-05T14:06:25
Label2.Text = dt.GetDateTimeFormats('t')[].ToString();//14:06
Label3.Text = dt.GetDateTimeFormats('y')[].ToString();//2005年11月
Label4.Text = dt.GetDateTimeFormats('D')[].ToString();//2005年11月5日
Label5.Text = dt.GetDateTimeFormats('D')[].ToString();//2005 11 05
Label6.Text = dt.GetDateTimeFormats('D')[].ToString();//星期六 2005 11 05
Label7.Text = dt.GetDateTimeFormats('D')[].ToString();//星期六 2005年11月5日
Label8.Text = dt.GetDateTimeFormats('M')[].ToString();//11月5日
Label9.Text = dt.GetDateTimeFormats('f')[].ToString();//2005年11月5日 14:06
Label10.Text = dt.GetDateTimeFormats('g')[].ToString();//2005-11-5 14:06
Label11.Text = dt.GetDateTimeFormats('r')[].ToString();//Sat, 05 Nov 2005 14:06:25 GMT Label1.Text =? string.Format("{0:d}",dt);//2005-11-5
Label2.Text =? string.Format("{0:D}",dt);//2005年11月5日
Label3.Text =? string.Format("{0:f}",dt);//2005年11月5日 14:23
Label4.Text =? string.Format("{0:F}",dt);//2005年11月5日 14:23:23
Label5.Text =? string.Format("{0:g}",dt);//2005-11-5 14:23
Label6.Text =? string.Format("{0:G}",dt);//2005-11-5 14:23:23
Label7.Text =? string.Format("{0:M}",dt);//11月5日
Label8.Text =? string.Format("{0:R}",dt);//Sat, 05 Nov 2005 14:23:23 GMT
Label9.Text =? string.Format("{0:s}",dt);//2005-11-05T14:23:23
Label10.Text = string.Format("{0:t}",dt);//14:23
Label11.Text = string.Format("{0:T}",dt);//14:23:23
Label12.Text = string.Format("{0:u}",dt);//2005-11-05 14:23:23Z
Label13.Text = string.Format("{0:U}",dt);//2005年11月5日 6:23:23
Label14.Text = string.Format("{0:Y}",dt);//2005年11月
Label15.Text = string.Format("{0}",dt);//2005-11-5 14:23:23?
Label16.Text = string.Format("{0:yyyyMMddHHmmssffff}",dt); //yyyymm等可以设置,比如Label16.Text = string.Format("{0:yyyyMMdd}",dt);

(1)获取上月 第1天和最后一天

int year = DateTime.Now.Year;//当前年
int mouth = DateTime.Now.Month;//当前月 int beforeYear = ;
int beforeMouth = ;
if (mouth <= )//如果当前月是一月,那么年份就要减1
{
beforeYear = year - ;
beforeMouth =;//上个月
}
else
{
beforeYear = year;
beforeMouth = mouth - ;//上个月
}
string beforeMouthOneDay = beforeYear + "年" + beforeMouth + "月" + + "日";//上个月第一天
string beforeMouthLastDay = beforeYear + "年" + beforeMouth + "月" + DateTime.DaysInMonth(year, beforeMouth) + "日";//上个月最后一天

上个月最后一天也可以这样写:

string beforeMouthLastDay = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-01")).AddDays(-).ToString("yyyy-MM-dd"); //获取上个月最后一天日期

(2)//当前时间

DateTime dt = DateTime.Now;

//本周周一

DateTime startWeek = dt.AddDays(-Convert.ToInt32(dt.DayOfWeek.ToString("d")));

//本周周日

DateTime endWeek = startWeek.AddDays();

//本月月初

DateTime startMonth = dt.AddDays( - dt.Day);

//本月月末

DateTime endMonth = dt.AddDays( - dt.Day).AddMonths().AddDays(-);

//本月月末

DateTime endMonth = startMonth.AddDays((dt.AddMonths() - dt).Days - );

//本季度初

DateTime startQuarter = dt.AddMonths( - (dt.Month - ) % ).AddDays( - dt.Day);

//本季度末

DateTime endQuarter = startQuarter.AddMonths().AddDays(-);

//本年年初

DateTime startYear = new DateTime(dt.Year, , );

//本年年末

DateTime endYear = new DateTime(dt.Year, , );

--DateTime 数字型
System.DateTime currentTime=new System.DateTime();
1.1 取当前年月日时分秒
currentTime=System.DateTime.Now;
1.2 取当前年
int 年=currentTime.Year;
1.3 取当前月
int 月=currentTime.Month;
1.4 取当前日
int 日=currentTime.Day;
1.5 取当前时
int 时=currentTime.Hour;
1.6 取当前分
int 分=currentTime.Minute;
1.7 取当前秒
int 秒=currentTime.Second;
1.8 取当前毫秒
int 毫秒=currentTime.Millisecond;
(变量可用中文)
1.9 取中文日期显示——年月日时分
string strY=currentTime.ToString("f"); //不显示秒
1.10 取中文日期显示_年月
string strYM=currentTime.ToString("y");
1.11 取中文日期显示_月日
string strMD=currentTime.ToString("m");
1.12 取当前年月日,格式为:2003-9-23
string strYMD=currentTime.ToString("d");
1.13 取当前时分,格式为:14:24
string strT=currentTime.ToString("t");
//今天
DateTime.Now.Date.ToShortDateString();
//昨天,就是今天的日期减一
DateTime.Now.AddDays(-1).ToShortDateString();
//明天,同理,加一
DateTime.Now.AddDays(1).ToShortDateString();
//本周(要知道本周的第一天就得先知道今天是星期几,从而得知本周的第一天就是几天前的那一天,要注意的是这里的每一周是从周日始至周六止
DateTime.Now.AddDays(Convert.ToDouble((0 - Convert.ToInt16(DateTime.Now.DayOfWeek)))).ToShortDateString();
DateTime.Now.AddDays(Convert.ToDouble((6 - Convert.ToInt16(DateTime.Now.DayOfWeek)))).ToShortDateString();
//如果你还不明白,再看一下中文显示星期几的方法就应该懂了
//
由于DayOfWeek返回的是数字的星期几,我们要把它转换成汉字方便我们阅读,有些人可能会用switch来一个一个地对照,其实不用那么麻烦的
string[] Day = new string[] { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五",
"星期六" };
Day[Convert.ToInt16(DateTime.Now.DayOfWeek)];
//上周,同理,一个周是7天,上周就是本周再减去7天,下周也是一样
DateTime.Now.AddDays(Convert.ToDouble((0 - Convert.ToInt16(DateTime.Now.DayOfWeek))) - 7).ToShortDateString();
DateTime.Now.AddDays(Convert.ToDouble((6 - Convert.ToInt16(DateTime.Now.DayOfWeek))) - 7).ToShortDateString();
//下周
DateTime.Now.AddDays(Convert.ToDouble((0 - Convert.ToInt16(DateTime.Now.DayOfWeek))) + 7).ToShortDateString();
DateTime.Now.AddDays(Convert.ToDouble((6 - Convert.ToInt16(DateTime.Now.DayOfWeek))) + 7).ToShortDateString();
//本月,很多人都会说本月的第一天嘛肯定是1号,最后一天就是下个月一号再减一天。当然这是对的
//一般的写法
DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + "1"; //第一天
DateTime.Parse(DateTime.Now.Year.ToString()
+ DateTime.Now.Month.ToString() +
"1").AddMonths(1).AddDays(-1).ToShortDateString();//最后一天

c# datetime 格式化大全与使用总结的更多相关文章

  1. (转)C# DateTime格式化大全

    //c datetime 格式化 DateTime dt = DateTime.Now; Label1.Text = dt.ToString();//2005-11-5 13:21:25 Label2 ...

  2. c# datetime 格式化

    //c datetime 格式化 DateTime dt = DateTime.Now; Label1.Text = dt.ToString();//2005-11-5 13:21:25 Label2 ...

  3. DataTime格式化大全(转载)

    //c datetime 格式化 DateTime dt = DateTime.Now; Label1.Text = dt.ToString();//2005-11-5 13:21:25 Label2 ...

  4. Python datetime 格式化字符串:strftime()

    Python datetime 格式化字符串:strftime()   Python 的datetime模块 其实就是date和time 模块的结合, 常见的属性方法都比较常用 比如: datetim ...

  5. .tostring()格式化大全

    C 货币 2.5.ToString("C") ¥2.50 D 十进制数 25.ToString("D5") 00025 E 科学型 25000.ToString ...

  6. MS SQL Server中的CONVERT日期格式化大全

    CONVERT 将某种数据类型的表达式显式转换为另一种数据类型.由于某些需求经常用到取日期格式的不同. 现以下可在SQL Server中将日期格式化. SQL Server 支持使用科威特算法的阿拉伯 ...

  7. Python 2.7.3 Time与DateTime格式化

    import time import datetime class TimeX: '''时间工具,目前用于格式化时间''' @staticmethod def GetLocalTimeString_T ...

  8. 【SQL Server】MS SQL Server中的CONVERT日期格式化大全

    CONVERT 函数将某种数据类型的表达式显式转换为另一种数据类型.SQL Server中 将日期格式化. SQL Server 支持使用科威特算法的阿拉伯样式中的数据格式. 在表中,左侧的两列表示将 ...

  9. C# DateTime 格式化 奇怪问题!

    使用 DateTime.Now.ToString("MM/dd") 本地显示 06/16 window sevcie 2003  显示是 06-16 解决方法: DateTime. ...

随机推荐

  1. Android 数据库 ObjectBox 源码解析

    一.ObjectBox 是什么? greenrobot 团队(现有 EventBus.greenDAO 等开源产品)推出的又一数据库开源产品,主打移动设备.支持跨平台,最大的优点是速度快.操作简洁,目 ...

  2. Mercurial的使用心得

    本文发表地址:http://www.xiabingbao.com/mercurial/2015/01/22/mercurial-understanding/ 本人接触版本控制的历史 在很久很久以前,我 ...

  3. node查询mongo

    http://www.cnblogs.com/whoamme/p/3467374.html nosql的数据库的查询:可以分为查询所有,查询一个,条件查询,和表的关联查询.(这个另外在写一个独立的mo ...

  4. 图 Graph-图的表示及其遍历

    2018-03-05 16:19:46 图是计算机科学中的一个非常重要的概念,图是一种多对多的关系.从某种角度上来说树和链表都是图的一种特例. 一.图的抽象数据类型 二.表示图的方法 图是由结点和边构 ...

  5. Stretch的Uniform和UniformToFill

    通俗理解Stretch的Uniform和UniformToFill: Uniform,控件的高度和宽度会增加直到达到了容器的大小,也就是说控件的大小和容器的大小是有关系的,同时如果给控件设置了明确的高 ...

  6. 启用/禁用以太网的批处理,用于一个网卡切换本地网络和wifi使用(Win10)

    注意下面时英文版上默认网络使用,同时接入了网线和wifi时,本地网络优先wifi. 所以禁用本地网络就会自动连接到wifi,启用本地网络,就会禁用wifi. 批处理支持 -y 参数,跳过用户输入y,代 ...

  7. nyoj1007——欧拉求和

    GCD 时间限制:1000 ms  |  内存限制:65535 KB 难度:3   描述 The greatest common divisor GCD(a,b) of two positive in ...

  8. 数论基础算法总结(python版)

    /* Author: wsnpyo Update Date: 2014-11-16 Algorithm: 快速幂/Fermat, Solovay_Stassen, Miller-Rabin素性检验/E ...

  9. PIVOT 和 UNPIVOT 命令的SQL Server版本

    I:使用 PIVOT 和 UNPIVOT 命令的SQL Server版本要求 1.数据库的最低版本要求为 SQL Server 2005 或 更高 2.必须将数据库的兼容级别设置为 90 或 更高 3 ...

  10. 通过Fegin远程调用 ,返回JPA Page 对象报错

    Caused by: com.fasterxml.jackson.databind.JsonMappingException: Can not construct instance of org.sp ...