c#时分秒毫秒微妙_你真的清楚DateTime in C#吗?
DateTime,就是一个世界的大融合。
日期和时间,在我们开发中非常重要。DateTime在C#中,专门用来表达和处理日期和时间。
本文算是多年使用DateTime的一个总结,包括DateTime对象的整体应用,以及如何处理不同的区域、时区、格式等内容。
一、什么是DateTime
跟我们想的不一样,DateTime不是一个类(class),而是一个结构(struct),它存在于System
命名空间下,在Dotnet Core中,处于System.Runtime.dll
中。
看一下DateTime的定义:
public struct DateTime : IComparable, IComparable, IConvertible, IEquatable, IFormattable, System.Runtime.Serialization.ISerializable
从定义可以知道,DateTime实现了IComparable
、IConvertible
、IEquatable
、IFormattable
、ISerializable
。因此,DateTime可以让我们使用有关日期和时间的很多相关信息。
二、构造
初始化一个DateTime对象,C#提供了11种方式进行初始化,根据需要,可以使用年月日时分秒,以及Ticks
。
Ticks
是C#里一个独特的定义,它是以公历0001年1月1日00:00:00.000以来所经历的以100纳秒为间隔的间隔数。我们知道,纳秒、微秒、毫秒和秒之间都是1000倍的关系,所以,1毫秒等于10000Ticks。这个数字很重要。在C#到Javascript时间转换时,需要很清楚这个对应关系。
DateTime date1 = new DateTime(2020, 7, 14);DateTime date2 = new DateTime(2020, 7, 14, 14, 23, 40);DateTime date3 = new DateTime(637303334200000000);
三、静态字段
DateTime包括三个静态字段:
MinValue - DateTime的最小值,对应公历0001年1月1日00:00:00.000,Ticks为0;
MaxValue - DateTime的最大值,对应公历9999看12月31日23:59:59.999,Ticks为3155378975999999999;
UnixEpoch - Unix、Javascript下的时间起点,对应公历1970年1月1日00:00:00.000,Ticks为621355968000000000;
在Javascript中,时间保存的是从UnixEpoch开始,即从1970年1月1日00:00:00.000开始到现在的毫秒数。所以,C#时间到Javascript时间的转换,可以用以下代码:
public static long ToUnixTicks(this DateTime time){ return (long)TimeSpan.FromTicks(time.Ticks - DateTime.UnixEpoch.Ticks).TotalMilliseconds - TimeZoneInfo.Local.GetUtcOffset(time).Hours * 60 * 60 * 1000;}
在Javascript中引入时间:
var time = new Date().setTime(unix_ticks);
就完成了转换。
四、方法
DateTime提供了很多种方法来操作DateTime对象,用于处理诸如向日期添加天数、小时、分钟、秒、日期差异、从字符串解析到datetime对象、获得通用时间等等。这儿就不详细说了,需要了可以查微软文档,很详细。
给几个例子:
TimeSpan duration = new System.TimeSpan(30, 0, 0, 0);DateTime newDate1 = DateTime.Now.Add(duration);
DateTime today = DateTime.Now;DateTime newDate2 = today.AddDays(30);
string dateString = "2020-07-14 14:23:40";DateTime dateTime12 = DateTime.Parse(dateString);
DateTime date1 = new System.DateTime(2020, 7, 13, 14, 20, 10);DateTime date2 = new System.DateTime(2020, 7, 14, 14, 25, 40);DateTime date3 = new System.DateTime(2020, 7, 14, 14, 25, 40);
TimeSpan diff1 = date2.Subtract(date1);DateTime date4 = date3.Subtract(diff1);TimeSpan diff2 = date3 - date2;
DateTime date5 = date2 - diff1;
五、属性
DateTime提供了年月日时分秒、以及其它一些属性,用来方便提取日期中的细节。
DateTime myDate = new DateTime(2020, 7, 14, 14, 23, 40);int year = myDate.Year; int month = myDate.Month;int day = myDate.Day;int hour = myDate.Hour;int minute = myDate.Minute;int second = myDate.Second;int weekDay = (int)myDate.DayOfWeek;string weekString = myDate.DayOfWeek.ToString();
其中,DayOfWeek,是用来判断日期是星期几的,它是一个枚举值。注意,按照惯例,一周是从周日开始的,所以,0表示周日,6表示周六。
DateTimeKind,用来定义实例表示的时间是基于本地时间(LocalTime)、UTC时间(UTC)或是不指定(Unspecified)。
在大多数情况下,我们定义时间就直接定义年月日时分秒,例如下面:
DateTime myDate = new DateTime(2020, 7, 14, 14, 23, 40);
这种定义下,这个时间就是Unspecified
的。
在使用时,如果应用过程中不做时间转换,始终以这种方式用,那不会有任何问题。但在某些情况下,时间有可能会发生转换,例如跨国应用的时间处理,再例如MongoDB,在数据库保存数据时,强制使用UTC时间。这种情况下,处理时间就必须采用LocalTime
或UTC
时间:
DateTime myDate = new DateTime(2020, 7, 14, 14, 23, 40, DateTimeKind.Local);
或
DateTime myDate = new DateTime(2020, 7, 14, 14, 23, 40, DateTimeKind.Unspecified);
否则,在时间类型不确定的情况下,时间转换会出现问题。
看看下面的例子:
DateTime myDate = new DateTime(2020, 7, 14, 14, 23, 40);
var date1 = myDate.ToLocalTime();Console.WriteLine(date1.ToString());/* 7/14/2020 22:23:40 PM */
var date2 = myDate.ToUniversalTime();Console.WriteLine(date2.ToString());/* 7/14/2020 6:23:40 AM */
当使用ToLocalTime
方法时,Unspecified
时间会认为自己是UTC
时间,而当使用ToUniversalTime
时,Unspecified
时间又会认为自己是LocalTime
时间,导致时间上的转换错误。
关于MongoDB处理时间的相关内容,可以去看我的另一个文章:MongoDB via Dotnet Core数据映射详解
六、时间对象的加减及比较
DateTime时间对象的加减及比较非常方便。看例子:
DateTime date1 = new System.DateTime(2020, 7, 14);
TimeSpan timeSpan = new System.TimeSpan(10, 5, 5, 1);DateTime addResult = date1 + timeSpan;DateTime substarctResult = date1 - timeSpan;
DateTime date2 = new DateTime(2020, 7, 14);DateTime date3 = new DateTime(2020, 7, 15);
bool isEqual = date2 == date3;
七、日期的格式化
日期的格式化是相关DateTime网上询问和查找最多的内容。
有这么一个表:
对照这个表就可以:
date.ToString("yyyy-MM-dd HH:mm:ss");
八、阴历
DateTime本身依赖于日历Calendar类。Calendar是一个抽象类,在System.Globalization
命名空间下,也在System.Runtime.dll
中。而在Calendar类下面,提供了很多不同类型的日历。跟我们有关系的,是中国的阴历ChineseLunisolarCalendar
。
使用也很简单:
Calendar calendar = new ChineseLunisolarCalendar();
DateTime date = new DateTime(2020, 06, 24, calendar);/* 7/14/2020 00:00:00 AM */
嗯嗯,经常看阴历的伙伴们会看出一点问题:今天是阴历5月24,为什么这儿写的是6月24呢?这个是因为今天闰4月,所以,阴历5月实际是这一个阴历年的第6个月。
那如何判断哪个月是否闰月呢?
Calendar calendar = new ChineseLunisolarCalendar();
bool is_leapYear = calendar.IsLeapYear(2020);bool is_leapMonth = calendar.IsLeapMonth(2020, 5);bool is_leapDay = calendar.IsLeapDay(2020, 5, 26);
同样,我们也可以用公历转阴历:
DateTime date = DateTime.Now;
Calendar calendar = new ChineseLunisolarCalendar();
int year = calendar.GetYear(date);/* 2020 */int month = calendar.GetMonth(date);/* 6 */int day = calendar.GetDayOfMonth(date);/* 24 */
以上就是全部内容了。
有没有发现,微软实现的功能,比我们想像的要多?
(全文完)
喜欢就来个三连,让更多人因你而受益
c#时分秒毫秒微妙_你真的清楚DateTime in C#吗?相关推荐
- c#时分秒毫秒微妙_C# 关于DateTime类型 精确到毫秒
展开全部 看下边代码帮你解答: System.DataTIme.Now.ToString(); System.DataTime.Now.ToString("yyyy-MM-dd hh:mm: ...
- c#时分秒毫秒微妙_c# - 将秒转换为(小时:分钟:秒:毫秒)时间的最佳方法是什么?...
我做了一些基准测试,看看最快的方法是什么,这些是我的结果和结论. 我运行了每个方法10M次并添加了评论,每次运行的平均时间. 如果您的输入毫秒不限于一天(结果可能是143:59:59.999),这些是 ...
- c#时分秒毫秒微妙_C# 日期格式精确到毫秒 【转】
有时候我们要对时间进行转换,达到不同的显示效果 默认格式为:2009-6-24 14:33:34 如果要换成成200906,06-2009,2009-6-24或更多的该怎么办呢 我们要用到:DateT ...
- python 时分秒毫秒_第一篇:Python处理时间日期
个人入门学习笔记的整理,有利于提升自己学习和归纳的能力. 希望对和我一样的入门菜鸟有帮助. 介绍:time.datetime是处理时间日期常用的库,datetime的time的再次封装,使用起来更加的 ...
- java获取时分秒毫秒_java实现时间格式转换(int整数类型的秒/毫秒---时分秒毫秒)...
1. 直接上干货:(可提取为工具类收藏哦 ^_^) package com.drew.utils; import java.text.SimpleDateFormat; import java.uti ...
- 批处理-取年月日、时分秒毫秒
批处理-取年月日.时分秒毫秒 echo 取年份: %date:~0,4% ECHO 取月份 %date:~5,2% ECHO 取日期:%date:~8,2% echo 取星期:%date:~1 ...
- android获取年月日时分秒毫秒,Android获取两个日期其间间隔的天数
/** * 获取两个日期之间的间隔天数 * @return */ public static int getGapCount(Date startDate, Date endDate) { Calen ...
- php只取时间的下士_php取当时的年月日时分秒毫秒
/** * 取当时的年月日时分秒毫秒:20140808121527000 */ function get_timestamp() { $datetime = date("Y-m-d H:i: ...
- JAVA 计算年月日时分秒毫秒
/**计算年月日时分秒毫秒**/ public static String Millisecond(String begins,String ends,String appointmentNo,Str ...
最新文章
- TensorFlow、PyTorch之后,“国产”AI框架还有没有机会?
- 死链对网站优化排名有哪些不利作用?
- matlab中partdata,[转载]meanshift算法
- 图书馆access的Google chrome 插件
- C语言学习之分别用while、 for双重循环的方式编程绘制如下图形
- python列向输出字符串_Python读取字符串按列分配后按行输出
- 上拉加载更多后台数据_6-7【微信小程序全栈开发课程】记录页面(七)--分页加载记录数据...
- 高效实用Kafka-深入理解Kafka启动配置(使用kafka自身内置Zookeeper)
- CSDN程序员研修院课程无法下载
- teststand 2020|ni teststand 2020(测试管理软件) 附安装教程
- 当管理遇上“人情”,你会怎么做?
- PhotoShop 之钢笔工具
- halcon入门之_提取遥控器字符并且写入txt文本
- 倾斜摄影的单体化建模研究 和osg 关系
- 分形理论在地理信息科学研究中的应用
- antdv 表格标题换行
- requests和xml的结合应用
- sharp s2 android 9,夏普AQUOS S2值得买吗?夏普S2全面深度评测
- Power query(Power BI) 自动生成贷款公司的还款计划表
- 不止音乐与露营——聊聊极狐汽车的微信生态营销