C# 中的 DateTimeOffset.ToUnixTimeMilliseconds() 方法
C# 中的 DateTimeOffset.ToUnixTimeMilliseconds() 方法用于返回从 1970-01-01T00:00:00.000Z 开始经过的毫秒数。
语法
以下是语法 −
public long ToUnixTimeMilliseconds ();
示例
现在,我们来看一个示例,来实现 DateTimeOffset.ToUnixTimeMilliseconds() 方法 −
using System; public class Demo { public static void Main() { DateTimeOffset dateTimeOffset = new DateTimeOffset(2019, 9, 10, 4, 20, 10, new TimeSpan(5, 0, 0)); Console.WriteLine("DateTimeOffset = {0}", dateTimeOffset); Console.WriteLine("Number of milliseconds: "+dateTimeOffset.ToUnixTimeMilliseconds()); DateTimeOffset res = dateTimeOffset.ToOffset(new TimeSpan(-5, 0, 0)); Console.WriteLine("
DateTimeOffset (updated) = {0}", res); Console.WriteLine("Number of milliseconds: "+res.ToUnixTimeMilliseconds()); } }
输出
这将生成以下输出 −
DateTimeOffset = 9/10/2019 4:20:10 AM +05:00 Number of milliseconds: 1568071210000 DateTimeOffset (updated) = 9/9/2019 6:20:10 PM -05:00 Number of milliseconds: 1568071210000
示例
现在,我们来看另一个示例,来实现 DateTimeOffset.ToUnixTimeMilliseconds() 方法 −
using System; public class Demo { public static void Main() { DateTimeOffset dateTimeOffset = new DateTimeOffset(1967, 11, 11, 6, 15, 45, new TimeSpan(3, 0, 0)); Console.WriteLine("DateTimeOffset = {0}", dateTimeOffset); Console.WriteLine("Number of milliseconds: "+dateTimeOffset.ToUnixTimeMilliseconds()); DateTimeOffset res = dateTimeOffset.ToOffset(new TimeSpan(-5, 0, 0)); Console.WriteLine("
DateTimeOffset (updated) = {0}", res); Console.WriteLine("Number of milliseconds: "+res.ToUnixTimeMilliseconds()); } }
输出
这将生成以下输出 −
DateTimeOffset = 11/11/1967 6:15:45 AM +03:00 Number of milliseconds: -67553055000 DateTimeOffset (updated) = 11/10/1967 10:15:45 PM -05:00 Number of milliseconds: -67553055000
广告