一、

DateTime dt = DateTime.Now 对象

1.获取当前时间

Console.WriteLine(dt.Year+"年");           //当前年份
Console.WriteLine(dt.Month + "月"); //当前月份
Console.WriteLine(dt.Day + "日"); //当前几号
Console.WriteLine(dt.Hour + "时"); //当前小时数
Console.WriteLine(dt.Minute + "分"); //当前分钟数
Console.WriteLine(dt.Millisecond + "秒"); //当前毫秒数

Console.WriteLine("今年的第"+dt.DayOfYear+"天");//当前年份的第几天
Console.WriteLine("今天是" + dt.DayOfWeek);//当前是星期几(英文输出)
Console.WriteLine("当前日期" + dt.Date);//当前日期
Console.WriteLine("当前时间" + dt.TimeOfDay);//当前时间

2.增加或减少时间

DateTime dt1 = DateTime.Now;
DateTime dt2;
dt2 = dt1.AddDays(+);//当前时间加1天(-1是减1天)
Console.WriteLine(dt1.Day);
Console.WriteLine(dt2.Day);

//AddYears,AddMonths,AddHours,AddMinutes,,AddSeconds,AddMilliseconds 用法一样

3.格式化输出

DateTime dt = DateTime.Now;
Console.WriteLine(dt.ToString());//默认格式
Console.ReadKey();

Console.WriteLine(dt.ToString("yyyy/MM/dd"));
Console.WriteLine(dt.ToString("yyyy-MM-dd"));
Console.WriteLine(dt.ToString("yyyy年MM月dd日"));
Console.WriteLine(dt.ToString("yyyy.MM.dd"));
Console.WriteLine(dt.ToString("dd/MM/yyyy"));
Console.WriteLine(dt.ToString("dd-MM-yyyy"));
Console.WriteLine(dt.ToString("yyyy-MM-dd HH:mm:ss.ffffff"));// f越多精度越高
Console.WriteLine(dt.ToString("yyyy-MM-dd HH:mm:ss:ms"));

注意区分大小写,一般都是小写,只有大写M表示月份,小写M表示分钟

对日期格式化(其他同理)

DateTime dt = DateTime.Now;
Console.WriteLine(dt.Date);
Console.WriteLine(dt.Date.ToString("yyyy年MM月dd日"));//
Console.ReadKey();

4.

Console.WriteLine("短日期:" + dt.ToShortTimeString());
Console.WriteLine("长日期:" + dt.ToLongDateString());
Console.WriteLine("短时间:"+dt.ToShortTimeString());
Console.WriteLine("长时间:"+dt.ToLongTimeString());
Console.WriteLine("本地时间:" + dt.ToLocalTime());

二、

 DateTime类

Console.WriteLine(DateTime.Now);//显示当前日期时间

Console.WriteLine(DateTime.Today);//显示当前日期

Console.WriteLine(DateTime.Now.AddDays(-));//当前日期数减1

Console.WriteLine(DateTime.MaxValue);//最大日期时间
Console.WriteLine(DateTime.MinValue);//最小日期时间

三、 TimeSpan 结构

最新文章

  1. ExtJs4之TreePanel
  2. 使用AutoMapper进行对象间映射
  3. Maximo子表中增加附件功能
  4. 设置二级域名共享一级域名Cookie和删除共享Cookie
  5. Python 的三目运算
  6. JSON基础使用
  7. 图像分类之特征学习ECCV-2010 Tutorial: Feature Learning for Image Classification
  8. oc-15-self
  9. CSS定位与浮动
  10. 华为u8800+ root 还是不能删除自带软件
  11. how to enable #ifdef macro in the command line of make?
  12. 【微信小程序】wx.openLocation调取失败
  13. JNA 备注
  14. 【学习总结】GirlsInAI ML-diary 总
  15. 枚举进程句柄File,Section,Mutant,Timer关闭Mutex句柄实现游戏多开
  16. 学习笔记56—Endnote参考文献格式调整
  17. mysql使用存储过程,自动生成新的表单
  18. CSS2.0实现面包屑
  19. python测试开发django-32.admin后台多对多关系ManyToManyField
  20. 26个你不知道的Python技巧

热门文章

  1. 《Cracking the Coding Interview》——第1章:数组和字符串——题目6
  2. Pascal数据结构与算法
  3. Nuget.config格式错误,请检查nuget.config配置文件
  4. JavaScript简易学习笔记
  5. Pytest+allure生成测试报告
  6. rownum浅谈(二)
  7. WMware给centos6.8虚拟机添加硬盘
  8. Axios & cookie
  9. BZOJ 1014 [JSOI2008]火星人prefix | Splay维护哈希值
  10. 【CZY选讲·逆序对】