C# 中的时间(DataTime)
在做报表或查询的时候,常常会预设一些可选的日期范围,如本周、本月、本年等,利用 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)的更多相关文章
- 【转】Python3 日期时间 相关模块(time(时间) / datatime(日期时间) / calendar(日历))
Python3 日期时间 相关模块(time(时间) / datatime(日期时间) / calendar(日历)) 本文由 Luzhuo 编写,转发请保留该信息. 原文: http://blog. ...
- Python3 日期时间 相关模块(time(时间) / datatime(日期时间) / calendar(日历))
Python3 日期时间 相关模块(time(时间) / datatime(日期时间) / calendar(日历)) 本文由 Luzhuo 编写,转发请保留该信息. 原文: http://blog. ...
- 借助JavaScript中的时间函数改变Html中Table边框的颜色
借助JavaScript中的时间函数改变Html中Table边框的颜色 <html> <head> <meta http-equiv="Content-Type ...
- javaScript系列:js中获取时间new Date()详细介绍
var myDate = new Date();myDate.getYear(); //获取当前年份(2位)myDate.getFullYear(); //获取完整的年份(4位,1970-????)m ...
- MVC中的时间js格式化
记录下我遇到的一个,MVC中post请求返回一个JSON字符串,其中包含数据库中的时间格式(如:/Date(10000000000)/),不知道怎么处理.百度的方法都不适用,经自己研究,做成了一个Jq ...
- C程序中对时间的处理——time库函数详解
包含文件:<sys/time.h> <time.h> 一.在C语言中有time_t, tm, timeval等几种类型的时间 1.time_t time_t实际上是长整数类型, ...
- [转]JDBC中日期时间的处理技巧
Java中用类java.util.Date对日期/时间做了封装,此类提供了对年.月.日.时.分.秒.毫秒以及时区的控制方法,同时也提供一些工具方法,比如日期/时间的比较,前后判断等. java.uti ...
- java8 中的时间和数据的变化
java8除了lambda表达式之外还对时间和数组这两块常用API做想应调整, Stream 有几个常用函数: store 排序 (a,b)-> a.compareTo(b) 排出来的结果是正 ...
- Linux中表示“时间”的结构体和相关函数
转载于:http://blog.chinaunix.net/uid-25909722-id-2827364.html Linux中表示“时间”的结构体和相关函数 2011-09-13 17: ...
- ylb:SQL Server中的时间函数
ylbtech-SQL Server:SQL Server-SQL Server中的时间函数 SQL Server中的时间函数. 1,SQL Server中的时间函数 返回顶部 1. 当前系统日期 ...
随机推荐
- 了解微信小程序
了解微信小程序 版权声明:未经博主授权,内容严禁转载分享! 微信小程序官方网址:https://mp.weixin.qq.com/cgi-bin/wx 某大神知乎专栏地址:七月在夏天 https:// ...
- C 运算优先级口诀
运算优先级口诀: 括号成员第一; //括号运算符[]() 成员运算符. -> 全体单目第二; //所有的单目运算符比如!.~.++. --. +(正). -(负) ...
- MySQL新建用户保存的时报错:The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement
又是这种错, 以前没遇过, 没办法, 直接google. 下面看解决办法: 登录mysql, 当然了如果您登录不上(密码错误情况), 直接扔这个属性进去my.cnf配置文件skip-grant-tab ...
- 20145309李昊 WEB基础实践
本实验同学帮助下完成 实验问题回答 1.什么是表单 表单在网页中主要负责数据采集功能 一个表单有三个基本组成部分: 表单标签 表单域:包含了文本框.密码框.隐藏域.多行文本框.复选框.单选框.下拉选择 ...
- 分布式系统一致性协议--Paxos算法
Paxos: Paxos算法背景介绍: Paxos算法是分布式技术大师Lamport提出的,主要目的是通过这个算法,让参与分布式处理的每个参与者逐步达成一致意见.用好理解的方式来说,就是在一个选举过程 ...
- Msys2的安装,并整合到cmder中
下载:msys2-x86_64-20161025.exe 下载安装包,然后装上. 打开msys的shell之后首先升级一下pacman,然后就可以愉快地Syu了. $ pacman -Sy pacma ...
- Unity3D学习笔记(五):坐标系、向量、3D数学
Unity复习 using System.Collections; using System.Collections.Generic; using UnityEngine; public class ...
- Select2下拉框总结
用了这么久的Select2插件,也该写篇文章总结总结.当初感觉Select2不是特别好用,但又找不到比它更好的下拉框插件. 在我的印象里Select2有2个版本,最新版本有一些新的特性,并且更新了一下 ...
- hdu 3183 A Magic Lamp rmq或者暴力
A Magic Lamp Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Pro ...
- ros 安装c++编译的可执行文件
xxx为c++源文件名 install( TARGETS xxx ARCHIVE DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION} LIBRARY DESTI ...