在做报表或查询的时候,常常会预设一些可选的日期范围,如本周、本月、本年等,利用 C# 内置的DateTime基本上都可以实现这些功能。

当前时间:

  DateTime dt = DateTime.Now;  //当前时间 

今天、昨天、明天:

  dt.ToShortDateString();      //今天
  dt.AddDays(-).ToShortDateString(); //昨天 —— 今天的日期减一
  dt.AddDays().ToShortDateString(); //明天,同理,加一

本周周一、周日:

  DateTime startWeek = dt.AddDays( - Convert.ToInt32(dt.DayOfWeek.ToString("d")));  //本周周一
  DateTime endWeek = startWeek.AddDays();        //本周周日

上周周一、周日,下周周一、周日:

  DateTime lastWeekStart = dt.AddDays( - Convert.ToInt32(dt.DayOfWeek.ToString("d"))-);    //上周周一
  DateTime lastWeekEnd = lastWeekStart.AddDays();                          //上周周日   DateTime nextWeekStart = dt.AddDays( - Convert.ToInt32(dt.DayOfWeek.ToString("d"))+);    //下周周一
  DateTime nextWeekEnd = nextWeekStart.AddDays();                          //下周周日

显示今天星期几:

  string[] Day = new string[] { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
  Day[Convert.ToInt16(DateTime.Now.DayOfWeek)];

本月月初、月末:

  DateTime startMonth = dt.AddDays( - dt.Day);        //本月月初
  DateTime endMonth = startMonth.AddMonths().AddDays(-);  //本月月末
  //字符串形式
  dt.ToString("yyyy-MM-01");
  dt.AddMonths().AddDays(- dt.Day).ToShortDateString();

上个月:月份减1

下个月:月份加1

本季度:

  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, , ); //本年年末

DataTime 其他常用方法、操作:

DateTime dt = DateTime.Now;
dt.ToString();//2005-11-5 13:21:25

dt.ToLongDateString().ToString();//2005年11月5日
dt.ToLongTimeString().ToString();//13:21:25 dt.ToShortDateString().ToString();//2005-11-5
dt.ToShortTimeString().ToString();//13:21 dt.Year.ToString();//
dt.Date.ToString();//2005-11-5 0:00:00

dt.DayOfWeek.ToString();//Saturday
dt.DayOfYear.ToString();//

dt.Hour.ToString();//
dt.Millisecond.ToString();//
dt.Minute.ToString();//
dt.Month.ToString();//
dt.Second.ToString();//

dt.Ticks.ToString();//
dt.TimeOfDay.ToString();//13:30:28.4412864
dt.ToString();//2005-11-5 13:47:04
dt.AddYears().ToString();//2006-11-5 13:47:04
dt.AddDays(1.1).ToString();//2005-11-6 16:11:04
dt.AddHours(1.1).ToString();//2005-11-5 14:53:04
dt.AddMilliseconds(1.1).ToString();//2005-11-5 13:47:04
dt.AddMonths().ToString();//2005-12-5 13:47:04
dt.AddSeconds(1.1).ToString();//2005-11-5 13:47:05
dt.AddMinutes(1.1).ToString();//2005-11-5 13:48:10
dt.AddTicks().ToString();//2005-11-5 13:47:04

dt.CompareTo(dt).ToString();//
dt.Add(?).ToString();//问号为一个时间段
dt.Equals("2005-11-6 16:11:04").ToString();//False
dt.Equals(dt).ToString();//True
dt.GetHashCode().ToString();//
dt.GetType().ToString();//System.DateTime
dt.GetTypeCode().ToString();//DateTime dt.GetDateTimeFormats('s')[].ToString();//2005-11-05T14:06:25
dt.GetDateTimeFormats('t')[].ToString();//14:06
dt.GetDateTimeFormats('y')[].ToString();//2005年11月
dt.GetDateTimeFormats('D')[].ToString();//2005年11月5日
dt.GetDateTimeFormats('D')[].ToString();//2005 11 05
dt.GetDateTimeFormats('D')[].ToString();//星期六 2005 11 05
dt.GetDateTimeFormats('D')[].ToString();//星期六 2005年11月5日
dt.GetDateTimeFormats('M')[].ToString();//11月5日
dt.GetDateTimeFormats('f')[].ToString();//2005年11月5日 14:06
dt.GetDateTimeFormats('g')[].ToString();//2005-11-5 14:06
dt.GetDateTimeFormats('r')[].ToString();//Sat, 05 Nov 2005 14:06:25 GMT string.Format("{0:d}",dt);//2005-11-5
string.Format("{0:D}",dt);//2005年11月5日
string.Format("{0:f}",dt);//2005年11月5日 14:23
string.Format("{0:F}",dt);//2005年11月5日 14:23:23
string.Format("{0:g}",dt);//2005-11-5 14:23
string.Format("{0:G}",dt);//2005-11-5 14:23:23
string.Format("{0:M}",dt);//11月5日
string.Format("{0:R}",dt);//Sat, 05 Nov 2005 14:23:23 GMT
string.Format("{0:s}",dt);//2005-11-05T14:23:23
string.Format("{0:t}",dt);//14:23
string.Format("{0:T}",dt);//14:23:23
string.Format("{0:u}",dt);//2005-11-05 14:23:23Z
string.Format("{0:U}",dt);//2005年11月5日 6:23:23
string.Format("{0:Y}",dt);//2005年11月
string.Format("{0}",dt);//2005-11-5 14:23:23
string.Format("{0:yyyyMMddHHmmssffff}",dt); 计算2个日期之间的天数差
-----------------------------------------------
DateTime dt1 = Convert.DateTime("2007-8-1");
DateTime dt2 = Convert.DateTime("2007-8-15");
TimeSpan span = dt2.Subtract(dt1);
int dayDiff = span.Days + ; 计算某年某月的天数
-----------------------------------------------
int days = DateTime.DaysInMonth(, );
days = ;

C# 中的时间(DataTime)的更多相关文章

  1. 【转】Python3 日期时间 相关模块(time(时间) / datatime(日期时间) / calendar(日历))

    Python3 日期时间 相关模块(time(时间) / datatime(日期时间) / calendar(日历)) 本文由 Luzhuo 编写,转发请保留该信息. 原文: http://blog. ...

  2. Python3 日期时间 相关模块(time(时间) / datatime(日期时间) / calendar(日历))

    Python3 日期时间 相关模块(time(时间) / datatime(日期时间) / calendar(日历)) 本文由 Luzhuo 编写,转发请保留该信息. 原文: http://blog. ...

  3. 借助JavaScript中的时间函数改变Html中Table边框的颜色

    借助JavaScript中的时间函数改变Html中Table边框的颜色 <html> <head> <meta http-equiv="Content-Type ...

  4. javaScript系列:js中获取时间new Date()详细介绍

    var myDate = new Date();myDate.getYear(); //获取当前年份(2位)myDate.getFullYear(); //获取完整的年份(4位,1970-????)m ...

  5. MVC中的时间js格式化

    记录下我遇到的一个,MVC中post请求返回一个JSON字符串,其中包含数据库中的时间格式(如:/Date(10000000000)/),不知道怎么处理.百度的方法都不适用,经自己研究,做成了一个Jq ...

  6. C程序中对时间的处理——time库函数详解

    包含文件:<sys/time.h> <time.h> 一.在C语言中有time_t, tm, timeval等几种类型的时间 1.time_t time_t实际上是长整数类型, ...

  7. [转]JDBC中日期时间的处理技巧

    Java中用类java.util.Date对日期/时间做了封装,此类提供了对年.月.日.时.分.秒.毫秒以及时区的控制方法,同时也提供一些工具方法,比如日期/时间的比较,前后判断等. java.uti ...

  8. java8 中的时间和数据的变化

    java8除了lambda表达式之外还对时间和数组这两块常用API做想应调整, Stream 有几个常用函数: store 排序 (a,b)-> a.compareTo(b)  排出来的结果是正 ...

  9. Linux中表示“时间”的结构体和相关函数

    转载于:http://blog.chinaunix.net/uid-25909722-id-2827364.html      Linux中表示“时间”的结构体和相关函数 2011-09-13 17: ...

  10. ylb:SQL Server中的时间函数

    ylbtech-SQL Server:SQL Server-SQL Server中的时间函数 SQL Server中的时间函数. 1,SQL Server中的时间函数 返回顶部 1.   当前系统日期 ...

随机推荐

  1. jquery的click无法触发事件

    一个页面需要在加载后勾选table中所有行的checkbox,于是就这样写 $("table thead tr th input[type='checkbox']").click( ...

  2. 一些应该使用mongodb或者其他文档存储而不是redis或mysql、oracle json的情形(最近更新场景)

    通常来说,我们应该使用应用的特性而不是自己的爱好或者规定而去选择一种合适的组件,选择的标准应该是这个组件最适合或者本身其设计就是为了解决这个问题,而不是这个组件能够做这事情为标准.就拿存储来说,任何时 ...

  3. oracle、Mysql数据库客户端DbVisualizer安装

    原文链接:https://jingyan.baidu.com/article/454316ab675302f7a7c03a9e.html

  4. 20145313张雪纯exp7

    问题 (1)通常在什么场景下容易受到DNS spoof攻击 处于局域网中的时候,例如连接了学校/公司/餐厅wifi. (2)在日常生活工作中如何防范以上两攻击方法 不要轻易点开未知网址.鼠标在链接处停 ...

  5. noip 邮票面值设计 - 搜索 - 动态规划

    描述 给定一个信封,最多只允许粘贴N张邮票,计算在给定M(N+M<=10)种邮票的情况下(假定所有的邮票数量都足够),如何设计邮票的面值,能得到最大max ,使得1-max之间的每一个邮资值都能 ...

  6. 认识epoll

    linux下的epoll(7)函数,其有着良好的就绪事件通知机制.Epoll 是被linux2.6开始引进的,但是不被其他的类UNIX系统支持,它提供了一种类似select或poll函数的机制:a. ...

  7. C++ 文件大小格式化

    #include <iostream> #include <windows.h> using namespace std; /*文件大小格式化 *param [in] dwSi ...

  8. zedgraph多个graphpane的处理

    这个问题需要研究,需要使用  zedgraph.masterpane.panelist 其他人做的效果--先预留一个官网的链接http://zedgraph.dariowiz.com/index113 ...

  9. Java 创建多线程的三种方法

    1. 继承Thread类2. 实现Runnable接口3. 匿名类的方式 注: 启动线程是start()方法,run()并不能启动一个新的线程

  10. UVa 1590 IP网络(简单位运算)

    Description   Alex is administrator of IP networks. His clients have a bunch of individual IP addres ...