DateTimeOffset.ToUnixTimeMilliseconds方法用于返回自1970-01-01T00:00:00.000Z以来经过的毫秒数。此方法将为1970-01-01T00:00:00Z之前的日期和时间值返回负值。
Syntax: public long ToUnixTimeMilliseconds ();
Return Value: This method returns the number of milliseconds that have elapsed since 1970-01-01T00:00:00.000Z.
下面的程序说明了DateTimeOffset.ToUnixTimeMilliseconds()方法的用法:
范例1:
// C# program to demonstrate the
// DateTimeOffset.ToUnixTimeMilliseconds()
// Method
using System;
using System.Globalization;
class GFG {
// Main Method
public static void Main()
{
// creating object of DateTimeOffset
DateTimeOffset offset = new DateTimeOffset(2007,
6, 1, 7, 55, 0, new TimeSpan(-5, 0, 0));
// Returns the number of milliseconds
// instance using ToUnixTimeMilliseconds() method
long value = offset.ToUnixTimeMilliseconds();
// Display the time
Console.WriteLine("Returns the number of"+
" milliseconds : {0}", value);
}
}
输出:
Returns the number of milliseconds : 1180702500000
范例2:
// C# program to demonstrate the
// DateTimeOffset.ToUnixTimeMilliseconds()
// Method
using System;
using System.Globalization;
class GFG {
// Main Method
public static void Main()
{
// Creating object of DateTimeOffset
DateTimeOffset offset = new DateTimeOffset(1960,
6, 1, 7, 55, 0, new TimeSpan(-5, 0, 0));
// Returns the number of milliseconds
// instance using ToUnixTimeMilliseconds() method
long value = offset.ToUnixTimeMilliseconds();
// Display the time
Console.WriteLine("Returns the number "+
"of milliseconds : {0}", value);
}
}
输出:
Returns the number of milliseconds : -302439900000
参考:
- https://docs.microsoft.com/zh-cn/dotnet/api/system.datetimeoffset.tounixtimemilliseconds?view=netframework-4.7.2