--DateTime 数字型 
System.DateTime currentTime=new System.DateTime(); 
取当前年月日时分秒      currentTime=System.DateTime.Now; 
取当前年     int 年=currentTime.Year; 
取当前月     int 月=currentTime.Month; 
取当前日     int 日=currentTime.Day; 
取当前时     int 时=currentTime.Hour; 
取当前分     int 分=currentTime.Minute; 
取当前秒     int 秒=currentTime.Second; 
取当前毫秒    int 毫秒=currentTime.Millisecond; (变量可用中文)

取中文日期显示——年月日时分    string strY=currentTime.ToString("f"); //不显示秒

取中文日期显示_年月       string strYM=currentTime.ToString("y");

取中文日期显示_月日     string strMD=currentTime.ToString("m");

取当前年月日,格式为:2003-9-23      string strYMD=currentTime.ToString("d");

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

DateTime.Now.ToString();//获取当前系统时间 完整的日期和时间
DateTime.Now.ToLongDateString();//只显示日期 xxxx年xx月xx日 ,一个是长日期
DateTime.Now.ToShortDateString();//只显示日期 xxxx-xx-xx 一个是短日期

//今天        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();

//上周,上周就是本周再减去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();

//下周    本周再加上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();

//本月    本月的第一天是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();//最后一天

另一种方法:

DateTime now = DateTime.Now; 
DateTime d1 = new DateTime(now.Year, now.Month, 1); //本月第一天

DateTime d2 = d1.AddMonths(1).AddDays(-1); //本月最后一天

PS:

DateTime.Now.DayOfWeek.ToString();//英文星期显示,Wednesday

(int)DateTime.Now.DayOfWeek     数字,若是周三,结果对应为3

DateTime.Now.ToString("dddd", new System.Globalization.CultureInfo("zh-cn")); //中文星期显示
DateTime.Now.ToString("dddd");//中文星期显示

DateTime.Now.ToString("dddd,MMMM,dd ,yyyy", new System.Globalization.DateTimeFormatInfo());//显示日期格式Friday,July, 01,2009

DateTime.Now.ToString("dddd,dd MMMM,yyyy") //输出   星期三,30 一月,2008

出处:http://msdn.microsoft.com/zh-cn/vstudio/bb762911(VS.95).aspx,如何:从特定日期中提取星期几

datetime类型在tostring()format的格式设置

参数format格式详细用法 
 格式字符 关联属性/说明 
 d ShortDatePattern 
 D LongDatePattern 
 f 完整日期和时间(长日期和短时间) 
 F FullDateTimePattern(长日期和长时间) 
 g 常规(短日期和短时间) 
 G 常规(短日期和长时间) 
 m、M MonthDayPattern 
 r、R RFC1123Pattern 
 s 使用当地时间的 SortableDateTimePattern(基于 ISO 8601) 
 t ShortTimePattern 
 T LongTimePattern 
 u UniversalSortableDateTimePattern 用于显示通用时间的格式 
 U 使用通用时间的完整日期和时间(长日期和长时间) 
 y、Y YearMonthPattern

下表列出了可被合并以构造自定义模式的模式。这些模式是区分大小写的

d 月中的某一天。一位数的日期没有前导零。 
 dd 月中的某一天。一位数的日期有一个前导零。 
 ddd 周中某天的缩写名称,在 AbbreviatedDayNames 中定义。 
 dddd 周中某天的完整名称,在 DayNames 中定义。 
 M 月份数字。一位数的月份没有前导零。 
 MM 月份数字。一位数的月份有一个前导零。 
 MMM 月份的缩写名称,在 AbbreviatedMonthNames 中定义。 
 MMMM 月份的完整名称,在 MonthNames 中定义。 
 y 不包含纪元的年份。如果不包含纪元的年份小于 10,则显示不具有前导零的年份。 
 yy 不包含纪元的年份。如果不包含纪元的年份小于 10,则显示具有前导零的年份。 
 yyyy 包括纪元的四位数的年份。 
 gg 时期或纪元。如果要设置格式的日期不具有关联的时期或纪元字符串,则忽略该模式。

h 12 小时制的小时。一位数的小时数没有前导零。 
 hh 12 小时制的小时。一位数的小时数有前导零。 
 H 24 小时制的小时。一位数的小时数没有前导零。 
 HH 24 小时制的小时。一位数的小时数有前导零。

C# 获取系统时间及时间格式的更多相关文章

  1. bat 获取系统日期,时间,并去掉时间小时前面的空格和时间后面的空格

    @echo off rem BAT获取系统日期,时间,并去掉时间小时前面的空格和时间后面的空格 echo *** %DATE% echo *** %TIME% set THISDATE=%DATE:~ ...

  2. java中获取系统的当前时间

    转自:http://www.cnblogs.com/Matrix54/archive/2012/05/01/2478158.html 一. 获取当前系统时间和日期并格式化输出: import java ...

  3. 获取系统开机的时间(Windows、Linux)

    获取系统启动的时间.Windows系统和Linux系统 1.Windows系统 1)代码如下 #include <stdio.h> #include <time.h> #inc ...

  4. 【转】QT获取系统时间,以及设置日期格式

    http://blog.csdn.net/zzk197/article/details/7498593 例如我要在一个label上设置当前时间 QDateTime time = QDateTime:: ...

  5. T_SQL 获取系统当前时间与明天时间的两种格式

    --获取系统明天的时间 select CONVERT(nvarchar(20),dateadd(d,1,getdate()),120)         2017-01-21 15:04:10 sele ...

  6. MySQL的数据类型,MySQL增删改--添加主外键、添加属性、删除主外键、改表名、获取系统当前时间等

    ls /etc/rc.d/init.d/mysql56service mysql56 start ps aux |grep "mysql"|grep "socket=&q ...

  7. 使用Calender类获取系统时间和时间和运算

    使用Calender类获取系统时间和时间和运算: @Test public void testCal(){ //使用Calender对象获取时间,并对时间进行计算: Calendar instance ...

  8. Unity3D获取系统当前时间,并格式化显示

    Unity 获取系统当前时间,并格式化显示.通过“System.DateTime”获取系统当前的时间,然后通过格式化把获得的时间格式化显示出来,具体如下: 1.打开Unity,新建一个空工程,Unit ...

  9. (C/C++学习)12.获取系统时间制作时钟(system()略解)

    说明:通过调用函数来获取系统当前时间,并制作一个数字式的时钟,时钟的显示包括年.月.日.小时.分以及秒,通过系统屏幕的刷新来对不断更新的时间进行屏幕的显示. 一.对相关函数的学习 1.time_t t ...

  10. C/C++获取系统当前时间

    C/C++获取系统当前时间   C库中与系统时间相关的函数定义在<time.h>头文件中, C++定义在<ctime>头文件中. 一.time(time_t*)函数 函数定义如 ...

随机推荐

  1. .NET批量删除代码前的行号

    1 EmEditor Pro.EditPlus .visual studio   ,把有行号的代码粘贴进去,按住键盘的Alt键,然后用鼠标拖出选择框列选行号,最后按Delete删除行号; 2 使用正则 ...

  2. [JBoss] - 解决URI提交时乱码问题

    JBoss 7 AS解决url提交数据乱码的问题: 打开jboss-as-7.1.1.Final\standalone\configuration\standalone.xml文件,在<exte ...

  3. Spring事务配置的五种方式

    Spring配置文件中关于事务配置总是由三个组成部分,分别是DataSource.TransactionManager和代理机制这三部分,无论哪种配置方式,一般变化的只是代理机制这部分. DataSo ...

  4. PHPWord

    PHPWord中文乱码 我在 使用PHPWord$section->addText(),输出中文是遇到乱码,PHPWord 中文乱码解决如下: 第一步:打开phpword/Section.php ...

  5. 强势回归,Linux blk用实力证明自己并不弱!

    Flash的出现把存储的世界搅翻了天,仿佛一夜之间发现了新大陆,所有旧世界的东西都变得笨拙.NVMe驱动义无反顾地抛弃了Linux blk,开发自己的队列管理. 当第一次看到NVMe重新使用Linux ...

  6. 关于Failed to install helloworld.apk on device 'emulator-5554!的一个解决办法

    好不容易架好了android环境,把该安得都安好了,结果发现在安装过程中创建一个虚拟设备映象就占用了c盘34g的空间,我的系统盘差点瘫了,看来以后就只能先用这一个虚拟设备调试了, 接着说上边这个问题, ...

  7. 安装lua和openresty

    #### ubuntu 16.04 64bit 安装Lua luajit 及openresty 1 安装lua ,因为luajit 支持lua5.1较好.貌似不支持5.2和5.3作为学习,我就安装5. ...

  8. LeetCode 168. Excel Sheet Column Title

    Given a positive integer, return its corresponding column title as appear in an Excel sheet. -> A ...

  9. XShell 无法匹配的outgoing encryption算法 ,No matching outgoing encryption algorithm found

    在链接的属性(SSH -> 安全性) 的加密算法列表中选择 aes256-ctr, mac加密列表中选择hmac-sha2-256,保存即可 To enable hmac-sha2-256 an ...

  10. 细说IIS异常日志 — 你必须知道的功能

    最近在跟QAD用Webservice搞接口做数据维护,搞的哥那个叫头大,遇到很多问题,系统的log4net根本就无法记录.话说QAD调我某一个接口,可能包含几百个字段,而且QAD是个产品,所以我这边提 ...