(31)C#时间
2024-08-30 03:39:30
一、
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 结构
最新文章
- ExtJs4之TreePanel
- 使用AutoMapper进行对象间映射
- Maximo子表中增加附件功能
- 设置二级域名共享一级域名Cookie和删除共享Cookie
- Python 的三目运算
- JSON基础使用
- 图像分类之特征学习ECCV-2010 Tutorial: Feature Learning for Image Classification
- oc-15-self
- CSS定位与浮动
- 华为u8800+ root 还是不能删除自带软件
- how to enable #ifdef macro in the command line of make?
- 【微信小程序】wx.openLocation调取失败
- JNA 备注
- 【学习总结】GirlsInAI ML-diary 总
- 枚举进程句柄File,Section,Mutant,Timer关闭Mutex句柄实现游戏多开
- 学习笔记56—Endnote参考文献格式调整
- mysql使用存储过程,自动生成新的表单
- CSS2.0实现面包屑
- python测试开发django-32.admin后台多对多关系ManyToManyField
- 26个你不知道的Python技巧
热门文章
- 《Cracking the Coding Interview》——第1章:数组和字符串——题目6
- Pascal数据结构与算法
- Nuget.config格式错误,请检查nuget.config配置文件
- JavaScript简易学习笔记
- Pytest+allure生成测试报告
- rownum浅谈(二)
- WMware给centos6.8虚拟机添加硬盘
- Axios &; cookie
- BZOJ 1014 [JSOI2008]火星人prefix | Splay维护哈希值
- 【CZY选讲·逆序对】