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. ...
随机推荐
- springboot数据库操作及事物管理操作例子
一.配置文件 pom.xml <dependency> <groupId>org.springframework.boot</groupId> <artifa ...
- 【Python】@staticmethod和@classmethod的作用与区别
前言 Python其实有3个方法,即静态方法(staticmethod),类方法(classmethod)和实例方法,一般来说,要使用某个类的方法,需要先实例化一个对象再调用方法.而使用@static ...
- 设计模式--观察者模式C++实现
观察者模式C++实现 1定义 Observer/Publish/subscribe发布订阅模式 定义对象间一种一对多的依赖关系,使得当一个对象改变状态时,所有依赖他的对象都能获得通知并被自动更新 2类 ...
- 【转】 JavaScript:history.go() 的妙用(转) 处理post回发后返回
在Web开发中,会遇到从一页(父页)导向另一页(子页),并且要求“返回”父页的情况,在这里如果用ASP.NET提供的 Response.Redirect()方法,往往不会达到理想的效果,例如:返回后, ...
- IOS-相机、相册
// // ViewController.m // IOS_0301_相册和相机 // // Created by ma c on 16/3/1. // Copyright © 2016年 博文科技. ...
- nodejs pm2配置使用
nodejs pm2配置使用教程参考链接:http://www.111cn.net/sys/linux/100927.htm 安装步骤: yum -y install npmnpm install - ...
- guava学习--cache
转载:http://outofmemory.cn/java/guava/cache/how-to-use-guava-cache http://www.cnblogs.com/parryyang/ ...
- JS中将对象转化为数组
前言 其实这本来应该是一个很基础的问题了,但我之做一想记录一下是因为之前因为对象转数组的时候卡住了后来弄了出来,但最近再遇到这个问题时竟然又卡主了,所以,关于这个问题,如何把一个对象{'未完成':5, ...
- 当vue路由变化时 改变导航条式样
这个是导航栏: <router-link to="/unusedOrder"> <div class="">路由1</div> ...
- 小练习:补数 (Number Complement)
1.eamples Input: Output: Explanation: The binary representation of (no leading zero bits), and its c ...