C#中的DateTimeOffset.ToUnixTimeSeconds()方法
C#中的DateTimeOffset.ToUnixTimeSeconds()方法用于返回自1970-01-01T00:00:00Z以来经过的秒数。
语法
以下是语法-
public long ToUnixTimeSeconds ();
示例
现在让我们看一个实现DateTimeOffset.ToUnixTimeSeconds()方法的示例-
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 seconds: "+dateTimeOffset.ToUnixTimeSeconds()); DateTimeOffset res = dateTimeOffset.ToOffset(new TimeSpan(-5, 0, 0)); Console.WriteLine("\nDateTimeOffset (updated) = {0}", res); Console.WriteLine("Number of seconds: "+res.ToUnixTimeSeconds()); } }
输出结果
这将产生以下输出-
DateTimeOffset = 11/11/1967 6:15:45 AM +03:00 Number of seconds: -67553055 DateTimeOffset (updated) = 11/10/1967 10:15:45 PM -05:00 Number of seconds: -67553055
示例
现在让我们来看另一个实现DateTimeOffset.ToUnixTimeSeconds()方法的示例-
using System; public class Demo { public static void Main() { DateTimeOffset dateTimeOffset = new DateTimeOffset(2019, 11, 11, 6, 15, 45, new TimeSpan(3, 0, 0)); Console.WriteLine("DateTimeOffset = {0}", dateTimeOffset); Console.WriteLine("Number of seconds: "+dateTimeOffset.ToUnixTimeSeconds()); DateTimeOffset res = dateTimeOffset.ToOffset(new TimeSpan(-5, 0, 0)); Console.WriteLine("\nDateTimeOffset (updated) = {0}", res); Console.WriteLine("Number of seconds: "+res.ToUnixTimeSeconds()); } }
输出结果
这将产生以下输出-
DateTimeOffset = 11/11/2019 6:15:45 AM +03:00 Number of seconds: 1573442145 DateTimeOffset (updated) = 11/10/2019 10:15:45 PM -05:00 Number of seconds: 1573442145