C#里内置的DateTime基本上都可以实现这些功能,巧用DateTime会使你处理这些事来变轻松多了
今天
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#里ToString的字符格式化更简便
DateTime.Now.ToString("yyyy-MM-01");
DateTime.Parse(DateTime.Now.ToString("yyyy-MM-01")).AddMonths(1).AddDays(-1).ToShortDateString();

上个月,减去一个月份
DateTime.Parse(DateTime.Now.ToString("yyyy-MM-01")).AddMonths(-1).ToShortDateString();
DateTime.Parse(DateTime.Now.ToString("yyyy-MM-01")).AddDays(-1).ToShortDateString();
下个月,加去一个月份
DateTime.Parse(DateTime.Now.ToString("yyyy-MM-01")).AddMonths(1).ToShortDateString();
DateTime.Parse(DateTime.Now.ToString("yyyy-MM-01")).AddMonths(2).AddDays(-1).ToShortDateString();
7天后
DateTime.Now.Date.ToShortDateString();
DateTime.Now.AddDays(7).ToShortDateString();
7天前
DateTime.Now.AddDays(-7).ToShortDateString();
DateTime.Now.Date.ToShortDateString();

本年度,用ToString的字符格式化我们也很容易地算出本年度的第一天和最后一天
DateTime.Parse(DateTime.Now.ToString("yyyy-01-01")).ToShortDateString();
DateTime.Parse(DateTime.Now.ToString("yyyy-01-01")).AddYears(1).AddDays(-1).ToShortDateString();
上年度,不用再解释了吧
DateTime.Parse(DateTime.Now.ToString("yyyy-01-01")).AddYears(-1).ToShortDateString();
DateTime.Parse(DateTime.Now.ToString("yyyy-01-01")).AddDays(-1).ToShortDateString();
下年度
DateTime.Parse(DateTime.Now.ToString("yyyy-01-01")).AddYears(1).ToShortDateString();
DateTime.Parse(DateTime.Now.ToString("yyyy-01-01")).AddYears(2).AddDays(-1).ToShortDateString();

本季度,很多人都会觉得这里难点,需要写个长长的过程来判断。其实不用的,我们都知道一年四个季度,一个季度三个月
首先我们先把日期推到本季度第一个月,然后这个月的第一天就是本季度的第一天了
DateTime.Now.AddMonths(0 - ((DateTime.Now.Month - 1) % 3)).ToString("yyyy-MM-01");
同理,本季度的最后一天就是下季度的第一天减一
DateTime.Parse(DateTime.Now.AddMonths(3 - ((DateTime.Now.Month - 1) % 3)).ToString("yyyy-MM-01")).AddDays(-1).ToShortDateString();
下季度,相信你们都知道了。。。。收工
DateTime.Now.AddMonths(3 - ((DateTime.Now.Month - 1) % 3)).ToString("yyyy-MM-01");
DateTime.Parse(DateTime.Now.AddMonths(6 - ((DateTime.Now.Month - 1) % 3)).ToString("yyyy-MM-01")).AddDays(-1).ToShortDateString();
上季度
DateTime.Now.AddMonths(-3 - ((DateTime.Now.Month - 1) % 3)).ToString("yyyy-MM-01");
DateTime.Parse(DateTime.Now.AddMonths(0 - ((DateTime.Now.Month - 1) % 3)).ToString("yyyy-MM-01")).AddDays(-1).ToShortDateString();

======简单补充======

c#中如何获取时间!
1、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 取中文年月日 
string strYMD=currentTime.ToString("D");

1.13 取当前时分,格式为:14:24 
string strT=currentTime.ToString("t");

1.14 取当前时间,格式为:2003-09-23T14:46:48 
string strT=currentTime.ToString("s");

1.15 取当前时间,格式为:2003-09-23 14:48:30Z 
string strT=currentTime.ToString("u");

1.16 取当前时间,格式为:2003-09-23 14:48 
string strT=currentTime.ToString("g");

1.17 取当前时间,格式为:Tue, 23 Sep 2003 14:52:40 GMT 
string strT=currentTime.ToString("r");

1.18获得当前时间 n 天后的日期时间 
DateTime newDay = DateTime.Now.AddDays(100);

string strT = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");

System.DateTime currentTime=new System.DateTime(); 
currentTime=System.DateTime.Now; //取当前年月日时分秒 
string Y=currentTime.Year.ToString(); //取当前年 
string M=currentTime.Month.ToString(); //取当前月 
string D=currentTime.Day.ToString(); //取当前日 
string T=currentTime.Hour.ToString(); //取当前时 
string MM=currentTime.Minute.ToString(); //取当前分 
string S=currentTime.Second.ToString(); //取当前秒 
string SS=currentTime.Millisecond.ToString(); //取当前毫秒 
string FileName=Y+M+D+T+MM+S+SS+".Html"; //联接后,得到长文件名

【C#】日期格式转换的更多相关文章

  1. 日期格式转换 java 2016-09-03T00:00:00.000+08:00

    /**  * 日期格式转换yyyy-MM-dd'T'HH:mm:ss.SSSXXX  (yyyy-MM-dd'T'HH:mm:ss.SSSZ) TO  yyyy-MM-dd HH:mm:ss  * @ ...

  2. Oracle日期格式转换,tochar(),todate()

    Oracle日期格式转换 本文主要介绍Oracle中的日期转换. 1. 日期转化为字符串 (以2016年10月20日为例) select to_char(sysdate,'yyyy-mm-dd hh2 ...

  3. SpringMVC与MyBatis整合之日期格式转换

    在上一篇博客<SpringMVC与MyBatis整合(一)——查询人员列表>中遗留了日期格式转换的问题,在这篇记录解决过程. 对于controller形参中pojo对象,如果属性中有日期类 ...

  4. Python 日期格式转换

    经常需要爬取网站上的时间信息,不同的网站又有不同的日期显示方式.而我需要将日期格式转化为一种特定的格式,所以为了简便和学习,记录下各种不同的日期格式转换. 日期格式化符号: %y :两位数的年份表示( ...

  5. 在SQL Server中 获取日期、日期格式转换

    --常用日期转换参数: PRINT CONVERT(varchar, getdate(), 120 ) 2016-07-20 16:09:01 PRINT replace(replace(replac ...

  6. ABAP - 日期格式转换 &amp; ABAP经常使用日期处理函数

    ABAP - 日期格式转换 如今提供下面一些日期格式转换的函数: Below are several FMs which can be used to convert date format. 1. ...

  7. freemark声明变量,boolean,date,date日期格式转换成String类型的(五)

    <br/>assign用来定义变量<#assign name="刘德华"><br/> 获取assign定义变量的值:${name} <br ...

  8. ssm日期格式转换

    ssm日期格式转换 1      需求 前端传入字符串类型日期转化成java中的Date类型,存入数据库中;将数据库中的日期类型通过jstl标签在前端页面转换成字符串类型. 2      步骤 2.1 ...

  9. Nullable<System.DateTime>日期格式转换 (转载)

    一.问题 1.html页面中时间显示出错,数据库中时间是正确的. 原因:没有把DateTime转成String类型. 2.  在C#中,发现不能直接使用ToString("yyyy-MM-d ...

  10. java 获取当前日期和特殊日期格式转换

     1.获取当前日期: package com.infomorrow.dao; import java.sql.Timestamp; import java.util.Calendar; import ...

随机推荐

  1. [吴恩达机器学习笔记]14降维3-4PCA算法原理

    14.降维 觉得有用的话,欢迎一起讨论相互学习~Follow Me 14.3主成分分析原理Proncipal Component Analysis Problem Formulation 主成分分析( ...

  2. uva 11722 Joining with Friend

    https://vjudge.net/problem/UVA-11722 题意:你和朋友都要乘坐火车,并且都会途径A城市.你们很想会面,但是你们到达这个城市的准确时刻都无法确定.你会在时间区间[t1, ...

  3. 《JavaScript 实战》:实现拖放(Drag & Drop)效果

    拖放效果,也叫拖拽.拖动,学名Drag-and-drop ,是最常见的js特效之一.如果忽略很多细节,实现起来很简单,但往往细节才是难点所在.这个程序的原型是在做图片切割效果的时候做出来的,那时参考了 ...

  4. 【LibreOJ】#6298. 「CodePlus 2018 3 月赛」华尔兹 BFS

    [题意]给定n*m的网格,起点和终点位置,一些格指定下一步的方向,一些格任意.要求为方向任意的格确定方向,使起点可以走到终点.n,m<=50. [算法]BFS [题解]这道题最好用BFS,因为D ...

  5. Go Web 编程 第一章 Web相关概念

    第一章 Go与Web应用 Go学习群:415660935 1.1 Web应用 在计算机的世界里,应用(application)是一个与用户进行交互,并完成用户特定任务的软件程序.而Web应用则是部署在 ...

  6. PHP 5 MySQLi 函数总结

    连接数据库 mysqli_connect() 函数打开一个到 MySQL 服务器的新的连接. <?php $con=mysqli_connect("localhost",&q ...

  7. thinkphp中的验证器

  8. py,pyc,pyw文件的区别和使用

    熟悉python编程的都知道,python三种最常见的py文件格式,.py,.pyc,.pyw,下面说一说它们各自的使用. py文件 python最常见的文件,是python项目的源码: 文件执行时l ...

  9. ubuntu的su初始密码设置

    Ubuntu刚安装后,不能在terminal中运行su命令,因为root没有默认密码,需要手动设定. 以安装ubuntu时输入的用户名登陆,该用户在admin组中,有权限给root设定密码. 给roo ...

  10. Apache+jboss群集部署

    Jboss default方式上的Cluster配置[二] - 操作系统http://www.myexception.cn/operating-system/862858.html Jboss def ...