System::TimeSpan class

TimeSpan class

Represents a time interval. This type should be allocated on stack and passed to functions by value or by reference. Never use System::SmartPtr class to manage objects of this type.

class TimeSpan

Methods

MethodDescription
Add(TimeSpan) constReturns a new instance of TimeSpan class that represents a time interval which is the sum of the time intervals represented by the current and the specified objects.
static Compare(TimeSpan, TimeSpan)Compares two TimeSpan objects.
CompareTo(TimeSpan) constCompares current and the specified objects.
CompareTo(const SharedPtr<Object>&) constCompares current and the specified objects.
Duration() constReturns a new instance of TimeSpan object whose value is the absolute value of the current object.
Equals(TimeSpan) constDetermines if the time interval represented by the current object is equal to the time interval represented by the specified object.
Equals(const SharedPtr<Object>&) constDetermines if the time interval represented by the current object is equal to the time interval represented by the specified object.
static Equals(TimeSpan, TimeSpan)Returns true if the specified objects represent the same time interval, otherwise - false.
static FromDays(double)Returns a new TimeSpan object that represents the specified interval.
static FromHours(double)Returns a new TimeSpan object that represents the specified interval.
static FromMilliseconds(double)Returns a new TimeSpan object that represents the specified interval.
static FromMinutes(double)Returns a new TimeSpan object that represents the specified interval.
static FromSeconds(double)Returns a new TimeSpan object that represents the specified interval.
static FromTicks(int64_t)Returns a new TimeSpan object that represents the specified interval.
get_Days() constReturns the days component of the time interval represented by the current TimeSpan object.
get_Hours() constReturns the hours component of the time interval represented by the current TimeSpan object.
get_Milliseconds() constReturns the milliseconds component of the time interval represented by the current TimeSpan object.
get_Minutes() constReturns the minutes component of the time interval represented by the current TimeSpan object.
get_Seconds() constReturns the seconds component of the time interval represented by the current TimeSpan object.
get_Ticks() constReturns the number of 100-nanoseconds intervals that constitute the time interval represented by the current TimeSpan object.
get_TotalDays() constReturns the value of the current TimeSpan object expressed in whole and fractional days.
get_TotalHours() constReturns the value of the current TimeSpan object expressed in whole and fractional hours.
get_TotalMilliseconds() constReturns the value of the current TimeSpan object expressed in whole and fractional milliseconds.
get_TotalMinutes() constReturns the value of the current TimeSpan object expressed in whole and fractional minutes.
get_TotalSeconds() constReturns the value of the current TimeSpan object expressed in whole and fractional seconds.
GetHashCode() constReturns a hash code for the current object.
IsNull() const
Negate() constReturns a new instance of TimeSpan object that represents negated value represented by the current TimeSpan object.
operator!=(TimeSpan) constDetermines if the time interval represented by the current object is not equal to the time interval represented by the specified object.
operator!=(std::nullptr_t) const
operator+(TimeSpan) constReturns a new instance of TimeSpan class that represents a time interval which is the sum of the time intervals represented by the current and the specified objects.
operator+() constReturns self.
operator+=(TimeSpan)Assigns to the current object the time interval which is the sum of the time interval represented by the current and the specified objects.
operator-(TimeSpan) constReturns a new instance of TimeSpan class that represents a time interval which is the result of subtraction of the time interval represented by the specified object from the time interval represented by the current object.
operator-() constReturns a new instance of TimeSpan object that represents negated value represented by the current TimeSpan object.
operator-=(TimeSpan)Assigns to the current object the time interval which is the result of subtraction of the time interval represented by the specified object from the time interval represented by the current object.
operator/(double) const
operator/(TimeSpan) const
operator/=(double)
operator<(TimeSpan) constDetermines if the time interval represented by the current object is shorter than the time interval represented by the specified object.
operator<(std::nullptr_t) const
operator<=(TimeSpan) constDetermines if the time interval represented by the current object is shorter than or equal to the time interval represented by the specified object.
operator<=(std::nullptr_t) const
operator=(const TimeSpan&)Sets the time interval represented by the specified TimeSpan object to the current TimeSpan object.
operator==(TimeSpan) constDetermines if the time interval represented by the current object is equal to the time interval represented by the specified object.
operator==(std::nullptr_t) const
operator>(TimeSpan) constDetermines if the time interval represented by the current object is longer than the time interval represented by the specified object.
operator>(std::nullptr_t) const
operator>=(TimeSpan) constDetermines if the time interval represented by the current object is longer than or equal to the time interval represented by the specified object.
operator>=(std::nullptr_t) const
static Parse(const String&)Converts string to equivalent TimeSpan object.
static Parse(const String&, const SharedPtr<IFormatProvider>&)Converts string to equivalent TimeSpan object using the specified format provider.
static Parse(const String&, const SharedPtr<Globalization::CultureInfo>&)
static Parse(const String&, const SharedPtr<Globalization::DateTimeFormatInfo>&)
static Parse(const String&, std::nullptr_t)
static ParseExact(const String&, const ArrayPtr<String>&, const SharedPtr<IFormatProvider>&, Globalization::TimeSpanStyles)Converts string to equivalent TimeSpan object using the specified formats, format provider and styles.
static ParseExact(const String&, const ArrayPtr<String>&, const SharedPtr<Globalization::CultureInfo>&, Globalization::TimeSpanStyles)
static ParseExact(const String&, const ArrayPtr<String>&, const SharedPtr<Globalization::DateTimeFormatInfo>&, Globalization::TimeSpanStyles)
static ParseExact(const String&, const ArrayPtr<String>&, std::nullptr_t, Globalization::TimeSpanStyles)
static ParseExact(const String&, const String&, const SharedPtr<IFormatProvider>&, Globalization::TimeSpanStyles)Converts string to equivalent TimeSpan object using the specified format, format provider and styles.
static ParseExact(const String&, const String&, const SharedPtr<Globalization::CultureInfo>&, Globalization::TimeSpanStyles)
static ParseExact(const String&, const String&, const SharedPtr<Globalization::DateTimeFormatInfo>&, Globalization::TimeSpanStyles)
static ParseExact(const String&, const String&, std::nullptr_t, Globalization::TimeSpanStyles)
Subtract(TimeSpan) constReturns a new instance of TimeSpan class that represents a time interval which is the result of subtraction of the time interval represented by the specified object from the time interval represented by the current object.
TimeSpan()Constructs a TimeSpan object that represents a zero time interval.
explicit TimeSpan(int64_t)Constructs an instance of TimeSpan class that represents the specified time interval.
TimeSpan(int, int, int)Constructs an instance of TimeSpan class that represents the time interval which is equal to the sum of the specified number of hours, minutes and seconds.
TimeSpan(int, int, int, int, int)Constructs an instance of TimeSpan class that represents the time interval which is equal to the sum of the specified number of hours, minutes, seconds and milliseconds.
TimeSpan(const TimeSpan&)Constructs a TimeSpan object that represents the time interval equal to the time interval represented by the specified TimeSpan object.
ToString() constReturns the string representation of the time interval represented by the current object.
ToString(const String&) constConverts the value of the current object to equivalent string representation, using the specified format.
ToString(const String&, const SharedPtr<IFormatProvider>&) constConverts the value of the current object to equivalent string representation, using the specified format and format provider.
ToString(const String&, const SharedPtr<Globalization::CultureInfo>&) const
ToString(const String&, const SharedPtr<Globalization::DateTimeFormatInfo>&) const
ToString(const String&, std::nullptr_t) const
static TryParse(const String&, TimeSpan&)Converts string to equivalent TimeSpan object and returns result of conversion.
static TryParse(const String&, const SharedPtr<IFormatProvider>&, TimeSpan&)Converts string to equivalent TimeSpan object using the specified format provider and returns result of conversion.
static TryParse(const String&, const SharedPtr<Globalization::CultureInfo>&, TimeSpan&)
static TryParse(const String&, const SharedPtr<Globalization::DateTimeFormatInfo>&, TimeSpan&)
static TryParse(const String&, std::nullptr_t, TimeSpan&)
static TryParseExact(const String&, const ArrayPtr<String>&, const SharedPtr<IFormatProvider>&, TimeSpan&)Converts string to equivalent TimeSpan object using the specified formats and format provider, and returns result of conversion.
static TryParseExact(const String&, const ArrayPtr<String>&, const SharedPtr<Globalization::CultureInfo>&, TimeSpan&)
static TryParseExact(const String&, const ArrayPtr<String>&, const SharedPtr<Globalization::DateTimeFormatInfo>&, TimeSpan&)
static TryParseExact(const String&, const ArrayPtr<String>&, std::nullptr_t, TimeSpan&)
static TryParseExact(const String&, const String&, const SharedPtr<IFormatProvider>&, Globalization::TimeSpanStyles, TimeSpan&)Converts string to equivalent TimeSpan object using the specified format, format provider and styles, and returns result of conversion.
static TryParseExact(const String&, const String&, const SharedPtr<Globalization::CultureInfo>&, Globalization::TimeSpanStyles, TimeSpan&)
static TryParseExact(const String&, const String&, const SharedPtr<Globalization::DateTimeFormatInfo>&, Globalization::TimeSpanStyles, TimeSpan&)
static TryParseExact(const String&, const String&, std::nullptr_t, Globalization::TimeSpanStyles, TimeSpan&)
static TryParseExact(const String&, const ArrayPtr<String>&, const SharedPtr<IFormatProvider>&, Globalization::TimeSpanStyles, TimeSpan&)Converts string to equivalent TimeSpan object using the specified formats, format provider and styles, and returns result of conversion.
static TryParseExact(const String&, const ArrayPtr<String>&, const SharedPtr<Globalization::CultureInfo>&, Globalization::TimeSpanStyles, TimeSpan&)
static TryParseExact(const String&, const ArrayPtr<String>&, const SharedPtr<Globalization::DateTimeFormatInfo>&, Globalization::TimeSpanStyles, TimeSpan&)
static TryParseExact(const String&, const ArrayPtr<String>&, std::nullptr_t, Globalization::TimeSpanStyles, TimeSpan&)
static TryParseExact(const String&, const String&, const SharedPtr<IFormatProvider>&, TimeSpan&)Converts string to equivalent TimeSpan object using the specified format and format provider, and returns result of conversion.
static TryParseExact(const String&, const String&, const SharedPtr<Globalization::CultureInfo>&, TimeSpan&)
static TryParseExact(const String&, const String&, const SharedPtr<Globalization::DateTimeFormatInfo>&, TimeSpan&)
static TryParseExact(const String&, const String&, std::nullptr_t, TimeSpan&)
static Type()Returns a TypeInfo object that represent TimeSpan structure.

Fields

FieldDescription
static MaxValueThe TimeSpan object that represents the longest possible interval.
static MinValue/// The TimeSpan object that represents the shortest possible interval.
static constexpr TicksPerDayThe number of 100-nanoseconds intervals in a day (24-hour interval).
static constexpr TicksPerHourThe number of 100-nanoseconds intervals in a hour.
static constexpr TicksPerMillisecondThe number of 100-nanoseconds intervals in a millisecond.
static constexpr TicksPerMinuteThe number of 100-nanoseconds intervals in a minute.
static constexpr TicksPerSecondThe number of 100-nanoseconds intervals in a second.
static ZeroThe TimeSpan object that represents zero-interval.

Remarks

#include "system/datetime.h"
#include "system/timespan.h"
#include <iostream>

int main()
{
  const auto date1 = System::DateTime(2021, 01, 01);
  const auto date2 = System::DateTime(2021, 10, 30);

  const auto interval = date2 - date1;

  std::cout << "Number of ticks: " << interval.get_Ticks() << std::endl;
  std::cout << "Number of milliseconds: " << interval.get_Milliseconds() << std::endl;
  std::cout << "Total number of milliseconds: " << interval.get_TotalMilliseconds() << std::endl;
  std::cout << "Number of minutes: " << interval.get_Minutes() << std::endl;
  std::cout << "Total number of minutes: " << interval.get_TotalMinutes() << std::endl;
  std::cout << "Number of hours: " << interval.get_Hours() << std::endl;
  std::cout << "Total number of hours: " << interval.get_Hours() << std::endl;
  std::cout << "Number of days: " << interval.get_Days() << std::endl;
  std::cout << "Total number of days: " << interval.get_TotalDays() << std::endl;

  return 0;
}
/*
This code example produces the following output:
Number of ticks: 260928000000000
Number of milliseconds: 0
Total number of milliseconds: 2.60928e+10
Number of minutes: 0
Total number of minutes: 434880
Number of hours: 0
Total number of hours: 0
Number of days: 302
Total number of days: 302
*/

See Also