c# datetime 格式化大全与使用总结
//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 格式化大全与使用总结的更多相关文章
- (转)C# DateTime格式化大全
//c datetime 格式化 DateTime dt = DateTime.Now; Label1.Text = dt.ToString();//2005-11-5 13:21:25 Label2 ...
- c# datetime 格式化
//c datetime 格式化 DateTime dt = DateTime.Now; Label1.Text = dt.ToString();//2005-11-5 13:21:25 Label2 ...
- DataTime格式化大全(转载)
//c datetime 格式化 DateTime dt = DateTime.Now; Label1.Text = dt.ToString();//2005-11-5 13:21:25 Label2 ...
- Python datetime 格式化字符串:strftime()
Python datetime 格式化字符串:strftime() Python 的datetime模块 其实就是date和time 模块的结合, 常见的属性方法都比较常用 比如: datetim ...
- .tostring()格式化大全
C 货币 2.5.ToString("C") ¥2.50 D 十进制数 25.ToString("D5") 00025 E 科学型 25000.ToString ...
- MS SQL Server中的CONVERT日期格式化大全
CONVERT 将某种数据类型的表达式显式转换为另一种数据类型.由于某些需求经常用到取日期格式的不同. 现以下可在SQL Server中将日期格式化. SQL Server 支持使用科威特算法的阿拉伯 ...
- Python 2.7.3 Time与DateTime格式化
import time import datetime class TimeX: '''时间工具,目前用于格式化时间''' @staticmethod def GetLocalTimeString_T ...
- 【SQL Server】MS SQL Server中的CONVERT日期格式化大全
CONVERT 函数将某种数据类型的表达式显式转换为另一种数据类型.SQL Server中 将日期格式化. SQL Server 支持使用科威特算法的阿拉伯样式中的数据格式. 在表中,左侧的两列表示将 ...
- C# DateTime 格式化 奇怪问题!
使用 DateTime.Now.ToString("MM/dd") 本地显示 06/16 window sevcie 2003 显示是 06-16 解决方法: DateTime. ...
随机推荐
- 基于usb4java实现的java下的usb通信
项目地址:点击打开 使用java开发的好处就是跨平台,基本上java的开发的程序在linux.mac.MS上都可以运行,对应这java的那句经典名言:一次编写,到处运行.这个项目里面有两种包选择,一个 ...
- flutter自定义View(CustomPainter) 之 canvas的方法总结
画布canvas 画布是一个矩形区域,我们可以控制其每一像素来绘制我们想要的内容 canvas 拥有多种绘制点.线.路径.矩形.圆形.以及添加图像的方法,结合这些方法我们可以绘制出千变万化的画面. 虽 ...
- Linux shell常用命令
1. sz 和 rz sz命令发送文件到本地: # sz filename rz命令本地上传文件到服务器: # rz 执行该命令后,在弹出框中选择要上传的文件即可.
- hdu4009最小树形图
多建一个根,连到每一个点,然后花费是建水井的钱 然后跑一边最小树形图即可,这题必定有解,因为可以从根开始到每一点,可以不用判无解的情况 #include<map> #include< ...
- NVML查询显卡信息
前段时间做英伟达硬解得时候,显卡总是莫名挂掉,后来发现是因为显卡温度过高掉了.这几天找到CUDA中有NVML工具可以查看显卡信息,nvidia-smi也是基于这个工具包. 使用的CUDA版本为CUDA ...
- HTML通过jQuery传值赋值
网页传值很常见,如果通过动态网页传值,我们很容易实现.但是如果静态网页传值,这个就要找资料,方法或者询问大牛们了.这个原来还真的没有做过.今天一同事需要做这个类似的功能,应该是昨天了.本来是昨天写的, ...
- 利用Docker编译Hadoop 3.1.0
前言 为什么要使用Docker编译,请自行百度 操作系统环境:Centos 6.8 uname -r 内核版本:2.6.32-642.el6.x86_64 除非有把握否则不要在Centos6.8中直接 ...
- ZOJ 1609 Equivalence(状压+dfs减枝)
ZOJ Problem Set - 1609 Equivalence Time Limit: 5 Seconds Memory Limit: 32768 KB When learning m ...
- Java中String两种不同创建方式的区别及intern的用法
一, Java有两种创建字符串的方式, String str1 = "abc"; String str2 = new String("abc"); 用双引号创建 ...
- 网络编程的基本概念,TCP/IP协议简介
8.1.1 网络基础知识 计算机网络形式多样,内容繁杂.网络上的计算机要互相通信,必须遵循一定的协议.目前使用最广泛的网络协议是Internet上所使用的TCP/IP协议. 网络编程的目的就是指直接或 ...