博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C# DateTime和时间戳
阅读量:2026 次
发布时间:2019-04-28

本文共 2021 字,大约阅读时间需要 6 分钟。

using System;namespace ConsoleApplication8{    class Program    {        /*        static void Main(string[] args)        {            DateTime dt = new DateTime();            Console.WriteLine(dt);//基督元年            Console.WriteLine(DateTime.MinValue);            Console.WriteLine(DateTime.MaxValue);            dt = DateTime.Today;            Console.WriteLine(dt);            dt = DateTime.UtcNow;            Console.WriteLine(dt);            dt = DateTime.Now;            Console.WriteLine(dt);            Console.WriteLine(dt.Year);            Console.WriteLine(dt.Month);            Console.WriteLine(dt.Day);            Console.WriteLine(dt.Date);//这个跟上面的DateTime.Today相等                        Console.WriteLine(dt.Hour);            Console.WriteLine(dt.Minute);            Console.WriteLine(dt.Second);            Console.WriteLine(dt.DayOfYear);            Console.WriteLine(dt.DayOfWeek);            Console.WriteLine(dt.TimeOfDay);            Console.WriteLine(dt.Kind);//是基于本地时间、协调通用时间 (UTC),还是两者皆否            Console.WriteLine(dt.Millisecond);//毫秒部分            Console.WriteLine(dt.Ticks);        }        */        static void Main(string[] args)        {            Console.WriteLine(ConvertDateTimeInt(DateTime.Now));            Console.WriteLine(ConvertIntDateTime(ConvertDateTimeInt(DateTime.Now)));        }        public static DateTime ConvertIntDateTime(double d)        {            DateTime time = DateTime.MinValue;            DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));            time = startTime.AddSeconds(d);            return time;        }        ///         /// 将c# DateTime时间格式转换为Unix时间戳格式        ///         /// 时间        /// 
double
public static double ConvertDateTimeInt(DateTime time) { double intResult = 0; DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1)); intResult = (time - startTime).TotalSeconds; return intResult; } }}

转载地址:http://utdaf.baihongyu.com/

你可能感兴趣的文章
如何写好代码?
查看>>
警惕软件复杂度困局
查看>>
工作笔记2
查看>>
Redis与MySQL双写一致性如何保证?
查看>>
服务端高并发分布式架构演进之路
查看>>
电商的千人千面系统,这样搞比较靠谱
查看>>
面试官:啥是集群策略啊?
查看>>
如何有效减少Java内存占用过高
查看>>
try-catch-finally中的4个大坑,老程序员也搞不定
查看>>
Java 6种延时队列的实现方法
查看>>
SpringBoot之CommandLineRunner
查看>>
SpringBoot之CommandLineRunner接口和ApplicationRunner接口
查看>>
Redis、Kafka 和 Pulsar 消息队列对比
查看>>
如何做到每天比别人少写200行代码?
查看>>
面试题系列:Java 夺命连环16问
查看>>
9种分布式ID生成方式,总有一款适合你
查看>>
把Redis当作队列来用,真的合适吗?
查看>>
高容错微服务架构设计思路
查看>>
工作几年了,API 网关还不懂?
查看>>
软件架构的演变历程:单体架构,垂直架构,SOA架构和微服务
查看>>