//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. Linux系统用户及用户组管理

    目录一.新增/删除用户和用户组二.创建/修改密码三.用户身份切换--su和sudo 一.新增/删除用户和用户组1.用户组 命令 : groupadd 语法 : groupadd [-g GID] gr ...

  2. LeetCode第[98]题(Java):Validate Binary Search Tree(验证二叉搜索树)

    题目:验证二叉搜索树 难度:Medium 题目内容: Given a binary tree, determine if it is a valid binary search tree (BST). ...

  3. mongodb禁止外网访问以及添加账号

    未曾料到被黑客勒索比特币的戏码竟然降临到我的身上,几个月的技术积累付之一炬.怪只怪自己学艺不精,心存侥幸和无知,不过经此一役,方知网络安全防护的重要性. 一直未给自己的mongodb数据库设置账号密码 ...

  4. CSS之按钮过滤

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...

  5. 数据挖掘之Python调用R包、函数、脚本

    Python中集成R :参考博客http://blog.csdn.net/weidelight/article/details/44946785

  6. mysql too many connections解决方法

    MySQL提示“too many connections”的解决办法   今天生产服务器上的MySQL出现了一个不算太陌生的错误“Too many connections”.平常碰到这个问题,我基本上 ...

  7. nyoj762——分解质因数+容斥+二分

    第k个互质数 时间限制:1000 ms  |  内存限制:65535 KB 难度:4   描述 两个数的a,b的gcd为1,即a,b互质,现在给你一个数m,你知道与它互质的第k个数是多少吗?与m互质的 ...

  8. linux中的/usr,/var,/opt目录详解

    转自:http://it.greenblogs.org/archives/2008/20113.shtml/ /usr文件系统  /usr 文件系统经常很大,因为所有程序安装在这里. /usr 里的所 ...

  9. 【WebGL】2.基础概念

    引入Three.js <!DOCTYPE html> <html> <head> <title></title> </head> ...

  10. 为什么叫金拱门- golden arch

    不要再纠结为什么叫这么难理解的名字了.因为从golden arch直译过来的撒.金色的拱门.就叫金拱门咯. 关于M的商标的历史来源如下: "McDonald's logo" red ...