TimeSpan 结构
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
表示一个时间间隔。
public value class TimeSpan : IComparable, IComparable<TimeSpan>, IEquatable<TimeSpan>, IFormattable
public value class TimeSpan : IComparable, IComparable<TimeSpan>, IEquatable<TimeSpan>, ISpanFormattable
public value class TimeSpan : IComparable, IComparable<TimeSpan>, IEquatable<TimeSpan>, IParsable<TimeSpan>, ISpanFormattable, ISpanParsable<TimeSpan>
public value class TimeSpan : IComparable, IComparable<TimeSpan>, IEquatable<TimeSpan>, IParsable<TimeSpan>, ISpanFormattable, ISpanParsable<TimeSpan>, IUtf8SpanFormattable
public value class TimeSpan : IComparable
public value class TimeSpan : IComparable, IComparable<TimeSpan>, IEquatable<TimeSpan>
public struct TimeSpan : IComparable, IComparable<TimeSpan>, IEquatable<TimeSpan>, IFormattable
public readonly struct TimeSpan : IComparable, IComparable<TimeSpan>, IEquatable<TimeSpan>, IFormattable
public readonly struct TimeSpan : IComparable, IComparable<TimeSpan>, IEquatable<TimeSpan>, ISpanFormattable
public readonly struct TimeSpan : IComparable, IComparable<TimeSpan>, IEquatable<TimeSpan>, IParsable<TimeSpan>, ISpanFormattable, ISpanParsable<TimeSpan>
public readonly struct TimeSpan : IComparable, IComparable<TimeSpan>, IEquatable<TimeSpan>, IParsable<TimeSpan>, ISpanFormattable, ISpanParsable<TimeSpan>, IUtf8SpanFormattable
[System.Serializable]
public struct TimeSpan : IComparable
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public struct TimeSpan : IComparable, IComparable<TimeSpan>, IEquatable<TimeSpan>
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public struct TimeSpan : IComparable, IComparable<TimeSpan>, IEquatable<TimeSpan>, IFormattable
type TimeSpan = struct
interface IFormattable
type TimeSpan = struct
interface ISpanFormattable
interface IFormattable
type TimeSpan = struct
interface IFormattable
interface IParsable<TimeSpan>
interface ISpanFormattable
interface ISpanParsable<TimeSpan>
type TimeSpan = struct
interface IFormattable
interface IParsable<TimeSpan>
interface ISpanFormattable
interface ISpanParsable<TimeSpan>
interface IUtf8SpanFormattable
[<System.Serializable>]
type TimeSpan = struct
[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type TimeSpan = struct
[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type TimeSpan = struct
interface IFormattable
Public Structure TimeSpan
Implements IComparable, IComparable(Of TimeSpan), IEquatable(Of TimeSpan), IFormattable
Public Structure TimeSpan
Implements IComparable, IComparable(Of TimeSpan), IEquatable(Of TimeSpan), ISpanFormattable
Public Structure TimeSpan
Implements IComparable, IComparable(Of TimeSpan), IEquatable(Of TimeSpan), IParsable(Of TimeSpan), ISpanFormattable, ISpanParsable(Of TimeSpan)
Public Structure TimeSpan
Implements IComparable, IComparable(Of TimeSpan), IEquatable(Of TimeSpan), IParsable(Of TimeSpan), ISpanFormattable, ISpanParsable(Of TimeSpan), IUtf8SpanFormattable
Public Structure TimeSpan
Implements IComparable
Public Structure TimeSpan
Implements IComparable, IComparable(Of TimeSpan), IEquatable(Of TimeSpan)
- 继承
- 属性
- 实现
示例
以下示例实例化一个 对象,该对象表示两个 TimeSpan 日期之间的差异。 然后, TimeSpan 它显示对象的属性。
// Define two dates.
DateTime date1 = new DateTime(2010, 1, 1, 8, 0, 15);
DateTime date2 = new DateTime(2010, 8, 18, 13, 30, 30);
// Calculate the interval between the two dates.
TimeSpan interval = date2 - date1;
Console.WriteLine("{0} - {1} = {2}", date2, date1, interval.ToString());
// Display individual properties of the resulting TimeSpan object.
Console.WriteLine(" {0,-35} {1,20}", "Value of Days Component:", interval.Days);
Console.WriteLine(" {0,-35} {1,20}", "Total Number of Days:", interval.TotalDays);
Console.WriteLine(" {0,-35} {1,20}", "Value of Hours Component:", interval.Hours);
Console.WriteLine(" {0,-35} {1,20}", "Total Number of Hours:", interval.TotalHours);
Console.WriteLine(" {0,-35} {1,20}", "Value of Minutes Component:", interval.Minutes);
Console.WriteLine(" {0,-35} {1,20}", "Total Number of Minutes:", interval.TotalMinutes);
Console.WriteLine(" {0,-35} {1,20:N0}", "Value of Seconds Component:", interval.Seconds);
Console.WriteLine(" {0,-35} {1,20:N0}", "Total Number of Seconds:", interval.TotalSeconds);
Console.WriteLine(" {0,-35} {1,20:N0}", "Value of Milliseconds Component:", interval.Milliseconds);
Console.WriteLine(" {0,-35} {1,20:N0}", "Total Number of Milliseconds:", interval.TotalMilliseconds);
Console.WriteLine(" {0,-35} {1,20:N0}", "Ticks:", interval.Ticks);
// This example displays the following output:
// 8/18/2010 1:30:30 PM - 1/1/2010 8:00:15 AM = 229.05:30:15
// Value of Days Component: 229
// Total Number of Days: 229.229340277778
// Value of Hours Component: 5
// Total Number of Hours: 5501.50416666667
// Value of Minutes Component: 30
// Total Number of Minutes: 330090.25
// Value of Seconds Component: 15
// Total Number of Seconds: 19,805,415
// Value of Milliseconds Component: 0
// Total Number of Milliseconds: 19,805,415,000
// Ticks: 198,054,150,000,000
// Define two dates.
let date1 = DateTime(2010, 1, 1, 8, 0, 15)
let date2 = DateTime(2010, 8, 18, 13, 30, 30)
// Calculate the interval between the two dates.
let interval = date2 - date1
printfn $"{date2} - {date1} = {interval}"
// Display individual properties of the resulting TimeSpan object.
printfn $""" {"Value of Days Component:",-35} {interval.Days,20}"""
printfn $""" {"Total Number of Days:",-35} {interval.TotalDays,20}"""
printfn $""" {"Value of Hours Component:",-35} {interval.Hours,20}"""
printfn $""" {"Total Number of Hours:",-35} {interval.TotalHours,20}"""
printfn $""" {"Value of Minutes Component:",-35} {interval.Minutes,20}"""
printfn $""" {"Total Number of Minutes:",-35} {interval.TotalMinutes,20}"""
printfn $""" {"Value of Seconds Component:",-35} {interval.Seconds,20:N0}"""
printfn $""" {"Total Number of Seconds:",-35} {interval.TotalSeconds,20:N0}"""
printfn $""" {"Value of Milliseconds Component:",-35} {interval.Milliseconds,20:N0}"""
printfn $""" {"Total Number of Milliseconds:",-35} {interval.TotalMilliseconds,20:N0}"""
printfn $""" {"Ticks:",-35} {interval.Ticks,20:N0}"""
// This example displays the following output:
// 8/18/2010 1:30:30 PM - 1/1/2010 8:00:15 AM = 229.05:30:15
// Value of Days Component: 229
// Total Number of Days: 229.229340277778
// Value of Hours Component: 5
// Total Number of Hours: 5501.50416666667
// Value of Minutes Component: 30
// Total Number of Minutes: 330090.25
// Value of Seconds Component: 15
// Total Number of Seconds: 19,805,415
// Value of Milliseconds Component: 0
// Total Number of Milliseconds: 19,805,415,000
// Ticks: 198,054,150,000,000
' Define two dates.
Dim date1 As Date = #1/1/2010 8:00:15AM#
Dim date2 As Date = #8/18/2010 1:30:30PM#
' Calculate the interval between the two dates.
Dim interval As TimeSpan = date2 - date1
Console.WriteLine("{0} - {1} = {2}", date2, date1, interval.ToString())
' Display individual properties of the resulting TimeSpan object.
Console.WriteLine(" {0,-35} {1,20}", "Value of Days Component:", interval.Days)
Console.WriteLine(" {0,-35} {1,20}", "Total Number of Days:", interval.TotalDays)
Console.WriteLine(" {0,-35} {1,20}", "Value of Hours Component:", interval.Hours)
Console.WriteLine(" {0,-35} {1,20}", "Total Number of Hours:", interval.TotalHours)
Console.WriteLine(" {0,-35} {1,20}", "Value of Minutes Component:", interval.Minutes)
Console.WriteLine(" {0,-35} {1,20}", "Total Number of Minutes:", interval.TotalMinutes)
Console.WriteLine(" {0,-35} {1,20:N0}", "Value of Seconds Component:", interval.Seconds)
Console.WriteLine(" {0,-35} {1,20:N0}", "Total Number of Seconds:", interval.TotalSeconds)
Console.WriteLine(" {0,-35} {1,20:N0}", "Value of Milliseconds Component:", interval.Milliseconds)
Console.WriteLine(" {0,-35} {1,20:N0}", "Total Number of Milliseconds:", interval.TotalMilliseconds)
Console.WriteLine(" {0,-35} {1,20:N0}", "Ticks:", interval.Ticks)
' The example displays the following output:
' 8/18/2010 1:30:30 PM - 1/1/2010 8:00:15 AM = 229.05:30:15
' Value of Days Component: 229
' Total Number of Days: 229.229340277778
' Value of Hours Component: 5
' Total Number of Hours: 5501.50416666667
' Value of Minutes Component: 30
' Total Number of Minutes: 330090.25
' Value of Seconds Component: 15
' Total Number of Seconds: 19,805,415
' Value of Milliseconds Component: 0
' Total Number of Milliseconds: 19,805,415,000
' Ticks: 198,054,150,000,000
# Define two dates.
$Date2 = Get-Date -Date '2010/8/18' -Hour 13 -Minute 30 -Second 30
$Date1 = Get-Date -Date '2010/1/1' -Hour 8 -Minute 0 -Second 15
# Calculate the interval between the two dates.
$Interval = $Date2 - $Date1
"{0} - {1} = {2}" -f $Date2, $Date1, ($Interval.ToString())
# Display individual properties of the resulting TimeSpan object.
" {0,-35} {1,20}" -f "Value of Days Component:", $Interval.Days
" {0,-35} {1,20}" -f "Total Number of Days:", $Interval.TotalDays
" {0,-35} {1,20}" -f "Value of Hours Component:", $Interval.Hours
" {0,-35} {1,20}" -f "Total Number of Hours:", $Interval.TotalHours
" {0,-35} {1,20}" -f "Value of Minutes Component:", $Interval.Minutes
" {0,-35} {1,20}" -f "Total Number of Minutes:", $Interval.TotalMinutes
" {0,-35} {1,20:N0}" -f "Value of Seconds Component:", $Interval.Seconds
" {0,-35} {1,20:N0}" -f "Total Number of Seconds:", $Interval.TotalSeconds
" {0,-35} {1,20:N0}" -f "Value of Milliseconds Component:", $Interval.Milliseconds
" {0,-35} {1,20:N0}" -f "Total Number of Milliseconds:", $Interval.TotalMilliseconds
" {0,-35} {1,20:N0}" -f "Ticks:", $Interval.Ticks
<# This sample produces the following output:
18/08/2010 13:30:30 - 01/01/2010 08:00:15 = 229.05:30:15
Value of Days Component: 229
Total Number of Days: 229.229340277778
Value of Hours Component: 5
Total Number of Hours: 5501.50416666667
Value of Minutes Component: 30
Total Number of Minutes: 330090.25
Value of Seconds Component: 15
Total Number of Seconds: 19,805,415
Value of Milliseconds Component: 0
Total Number of Milliseconds: 19,805,415,000
Ticks: 198,054,150,000,000
#>
注解
有关此 API 的详细信息,请参阅 TimeSpan 的补充 API 说明。
构造函数
TimeSpan(Int32, Int32, Int32) |
将 TimeSpan 结构的新实例初始化为指定的小时数、分钟数和秒数。 |
TimeSpan(Int32, Int32, Int32, Int32) |
将 TimeSpan 结构的新实例初始化为指定的天数、小时数、分钟数和秒数。 |
TimeSpan(Int32, Int32, Int32, Int32, Int32) |
将 TimeSpan 结构的新实例初始化为指定的天数、小时数、分钟数、秒数和毫秒数。 |
TimeSpan(Int32, Int32, Int32, Int32, Int32, Int32) |
将 结构的新实例 TimeSpan 初始化为指定的天数、小时数、分钟数、秒数、毫秒数和微秒数。 |
TimeSpan(Int64) |
将 TimeSpan 结构的新实例初始化为指定的刻度数。 |
字段
MaxValue |
表示最大的 TimeSpan 值。 此字段为只读。 |
MinValue |
表示最小的 TimeSpan 值。 此字段为只读。 |
NanosecondsPerTick |
表示每个刻度数的纳秒数。 此字段为常数。 |
TicksPerDay |
表示一天中的刻度数。 此字段为常数。 |
TicksPerHour |
表示 1 小时的刻度数。 此字段为常数。 |
TicksPerMicrosecond |
表示以 1 微秒为单位的刻度数。 此字段为常数。 |
TicksPerMillisecond |
表示 1 毫秒的刻度数。 此字段为常数。 |
TicksPerMinute |
表示 1 分钟的刻度数。 此字段为常数。 |
TicksPerSecond |
表示 1 秒的刻度数。 |
Zero |
表示零 TimeSpan 值。 此字段为只读。 |
属性
Days |
获取当前 TimeSpan 结构所表示的时间间隔的天数部分。 |
Hours |
获取当前 TimeSpan 结构所表示的时间间隔的小时数部分。 |
Microseconds |
获取由当前 TimeSpan 结构表示的时间间隔的微秒分量。 |
Milliseconds |
获取当前 TimeSpan 结构所表示的时间间隔的毫秒数部分。 |
Minutes |
获取当前 TimeSpan 结构所表示的时间间隔的分钟数部分。 |
Nanoseconds |
获取由当前 TimeSpan 结构表示的时间间隔的纳秒分量。 |
Seconds |
获取当前 TimeSpan 结构所表示的时间间隔的秒数部分。 |
Ticks |
获取表示当前 TimeSpan 结构的值的刻度数。 |
TotalDays |
获取以整天数和天的小数部分表示的当前 TimeSpan 结构的值。 |
TotalHours |
获取以整小时数和小时的小数部分表示的当前 TimeSpan 结构的值。 |
TotalMicroseconds |
获取以整数和小数微秒表示的当前 TimeSpan 结构的值。 |
TotalMilliseconds |
获取以整毫秒数和毫秒的小数部分表示的当前 TimeSpan 结构的值。 |
TotalMinutes |
获取以整分钟数和分钟的小数部分表示的当前 TimeSpan 结构的值。 |
TotalNanoseconds |
获取以整数和小数纳秒表示的当前 TimeSpan 结构的值。 |
TotalSeconds |
获取以整秒数和秒的小数部分表示的当前 TimeSpan 结构的值。 |
方法
运算符
Addition(TimeSpan, TimeSpan) |
添加两个指定的 TimeSpan 实例。 |
Division(TimeSpan, Double) |
返回一个新的 TimeSpan 对象,该对象的值是将指定的 除以指定的 |
Division(TimeSpan, TimeSpan) |
返回一个新 Double 值,该值是除以 |
Equality(TimeSpan, TimeSpan) |
指示两个 TimeSpan 实例是否相等。 |
GreaterThan(TimeSpan, TimeSpan) | |
GreaterThanOrEqual(TimeSpan, TimeSpan) | |
Inequality(TimeSpan, TimeSpan) |
指示两个 TimeSpan 实例是否不相等。 |
LessThan(TimeSpan, TimeSpan) | |
LessThanOrEqual(TimeSpan, TimeSpan) | |
Multiply(Double, TimeSpan) |
返回一个新的 TimeSpan 对象,其值是指定的 |
Multiply(TimeSpan, Double) |
返回一个新的 TimeSpan 对象,其值是指定的 |
Subtraction(TimeSpan, TimeSpan) | |
UnaryNegation(TimeSpan) |
返回一个 TimeSpan,它的值为这个指定实例的相反值。 |
UnaryPlus(TimeSpan) |
返回指定的 TimeSpan 的实例。 |
显式接口实现
IComparable.CompareTo(Object) |
将此实例与指定对象进行比较,并返回一个整数,该整数指示此实例是短于、等于还是长于指定对象。 |
适用于
另请参阅
反馈
https://aka.ms/ContentUserFeedback。
即将发布:在整个 2024 年,我们将逐步淘汰作为内容反馈机制的“GitHub 问题”,并将其取代为新的反馈系统。 有关详细信息,请参阅:提交和查看相关反馈