DateTime

DateTime class

Represents a specific date and time value on the time continuum. 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 DateTime

Methods

MethodDescription
DateTime Add(TimeSpan) constReturns a new instance of DateTime class that represents a date and time value that results from addition of the specified time span to the date and time value represented by the current object.
DateTime AddDays(double) constReturns a new instance of the DateTime class representing the date and time value which is the sum of the value represented by the current object and the specified number of days.
DateTime AddHours(double) constReturns a new instance of the DateTime class representing the date and time value which is the sum of the value represented by the current object and the specified number of hours.
DateTime AddMilliseconds(double) constReturns a new instance of the DateTime class representing the date and time value which is the sum of the value represented by the current object and the specified number of milliseconds.
DateTime AddMinutes(double) constReturns a new instance of the DateTime class representing the date and time value which is the sum of the value represented by the current object and the specified number of minutes.
DateTime AddMonths(int) constReturns a new instance of the DateTime class representing the date and time value which is the sum of the value represented by the current object and the specified number of months.
DateTime AddSeconds(double) constReturns a new instance of the DateTime class representing the date and time value which is the sum of the value represented by the current object and the specified number of seconds.
DateTime AddTicks(int64_t) constReturns a new instance of the DateTime class representing the date and time value which is the sum of the value represented by the current object and the specified number of 100-nanosecond intervals.
DateTime AddYears(int) constReturns a new instance of the DateTime class representing the date and time value equal to that represented by the current object with the year component increased by the specified number.
static constexpr int Compare(DateTime, DateTime)Compares two values represented by the specified instances of DateTime class and returns the value indicating values’ relative positions on the time line.
constexpr int CompareTo(DateTime) constCompares two date and time values represented by the current object and the specified instance of DateTime class and returns the value indicating values’ relative positions on the time line.
constexpr DateTime()Constructs an instance that represents the smallest possible date and time value equal to MinValue.
DateTime(int, int, int)Constructs an instance that represents a date and time value specified as a particular year, month and day.
DateTime(int, int, int, const SharedPtr<Globalization::Calendar>&)Constructs an instance that represents a date and time value specified as a particular year, month and day in the specified calendar.
DateTime(int, int, int, int, int, int)Constructs an instance that represents a date and time value specified as a particular year, month, day, hour, minute and second.
DateTime(int, int, int, int, int, int, DateTimeKind)Constructs an instance that represents a date and time value specified as a particular year, month, day, hour, minute and second.
DateTime(int, int, int, int, int, int, const SharedPtr<Globalization::Calendar>&)Constructs an instance that represents a date and time value specified as a particular year, month, day, hour, minute and second in the specified calendar.
DateTime(int, int, int, int, int, int, int, DateTimeKind)Constructs an instance that represents a date and time value specified as a particular year, month, day, hour, minute, second and millisecond.
DateTime(int, int, int, int, int, int, int, const SharedPtr<Globalization::Calendar>&, DateTimeKind)Constructs an instance that represents a date and time value specified as a particular year, month, day, hour, minute, second and millisecond in the specified calendar.
DateTime(int64_t, DateTimeKind)Construct an instance that represents a date and time value specified as a number of ticks.
DateTime(int64_t, DateTimeKind, bool)Construct an instance that represents a date and time value specified as a number of ticks. FOR INTERNAL USE.
DateTime(const DateTime&)Copy-constructs an instance.
static int DaysInMonth(int, int)Returns the number of days in the specified month of the specified year.
static constexpr bool Equals(DateTime, DateTime)Determines if the specified instances of DateTime class represent the same date and time value.
constexpr bool Equals(DateTime) constDetermines if the specified instance of DateTime class represent the same date and time value as the current object.
static DateTime FromBinary(int64_t)Deserializes the date time value from the specified unsigned 64-bit integer and sets the new instance of DateTime class to that value.
static DateTime FromFileTime(int64_t)Converts the specified File time to an instance of DateTime class representing the same date and time value as local time.
static DateTime FromFileTimeUtc(int64_t)Converts the specified File time to an instance of DateTime class representing the same date and time value as UTC time.
static DateTime FromOADate(double)Returns an instance of DateTime class representing the date and time value equivalent to the specified OLE Automation Date.
static DateTime FromUnixTime(time_t)Converts the specified Unix time value to an instance of DateTime class. FOR INTERNAL USE.
constexpr DateTime get_Date() constReturns a new instance of DateTime class that represents the date portion of the date and time represented by the current object with each component of the time portion set to 0.
int get_Day() constReturns the ordinal number of the day in the month represented by the current object.
constexpr DayOfWeek get_DayOfWeek() constReturns a value representing a day of week that is represented by the current object.
int get_DayOfYear() constReturns the ordinal number of the day in the year represented by the current object.
constexpr int get_Hour() constReturns the hour component of the date and time value represented by the current object.
constexpr DateTimeKind get_Kind() constReturns the value representing if the date and time represented by the current object is a local or UTC date and time or neither.
constexpr int get_Millisecond() constReturns the millisecond component of the date and time value represented by the current object.
constexpr int get_Minute() constReturns the minute component of the date and time value represented by the current object.
int get_Month() constReturns the ordinal number of the month in the year represented by the current object.
static DateTime get_Now()Returns an instance of DateTime class that represents the current time as local time.
constexpr int get_Second() constReturns the second component of the date and time value represented by the current object.
constexpr int64_t get_Ticks() constReturns a number of 100-nanosecond intervals passed since 0:00:00 UTC, January 1, 0001, in the Gregorian calendar until the date and time represented by the current object.
constexpr TimeSpan get_TimeOfDay() constReturns the value that represents the time interval from the beginning of the day represented by the current object till the date and time value represented by the current object.
static DateTime get_Today()Returns an instance of DateTime class that represents the current date with each component of time portion of the value represented by the object set to 0.
static DateTime get_UtcNow()Returns an instance of DateTime class that represents the current time as UTC.
int get_Year() constReturns the year represented by the current object.
void GetDateComponents(int&, int&, int&) constGets date parts. FOR INTERNAL USE.
ArrayPtr<String> GetDateTimeFormats() constReturns array of strings where each element is the string representation of the current object formatted with one of the standard date and time format specifiers.
ArrayPtr<String> GetDateTimeFormats(char_t) constReturns array of strings where each element is the string representation of the current object formatted with the specified standard date and time format specifier.
ArrayPtr<String> GetDateTimeFormats(const SharedPtr<IFormatProvider>&) constReturns array of strings where each element is the string representation of the current object formatted with one of the standard date and time format specifiers and the specified format provider.
ArrayPtr<String> GetDateTimeFormats(char_t, const SharedPtr<IFormatProvider>&) constReturns array of strings where each element is the string representation of the current object formatted with the specified standard date and time format specifier and format provider.
int GetHashCode() constReturns a hash code for the current object.
bool IsDaylightSavingTime() constDetermines if the date and time value represented by the current object falls in the range of daylight saving time for the current time zone.
static bool IsLeapYear(int)Determines of the specified year is a leap year.
constexpr bool IsNull() const
constexpr bool operator!=(DateTime) constDetermines if the current object and the specified DateTime object represent distinct date and time values.
constexpr bool operator!=(std::nullptr_t) const
DateTime operator+(TimeSpan) constReturns a new instance of DateTime class that represents the date and time value that is the sum of the value represented by the current object and the specified time span.
DateTime& operator+=(TimeSpan)Sets the current object to the date and time value that is the sum of the value represented by the current object and the specified time span.
DateTime operator-(TimeSpan) constReturns a new instance of the DateTime class representing the date and time value which is the result of subtraction of the specified time span from the value represented by the current object.
constexpr TimeSpan operator-(DateTime) constReturns an instance of TimeSpan class that represents the time interval between the date and time values represented by the current and the specified objects.
DateTime& operator-=(TimeSpan)Sets the current object to the date and time value that is the result of subtraction of the specified time span from the date and time value represented by the current object.
constexpr bool operator<(DateTime) constDetermines if the current object represents the date and time value that is earlier than the value represented by the specified DateTime object.
constexpr bool operator<(std::nullptr_t) const
constexpr bool operator<=(DateTime) constDetermines if the current object represents the date and time value that is earlier than or the same as the value represented by the specified DateTime object.
constexpr bool operator<=(std::nullptr_t) const
DateTime& operator=(const DateTime&)Assigns the value represented by the specified DateTime instance to the current object.
constexpr bool operator==(DateTime) constDetermines if the current object and the specified DateTime object represent the same date and time value.
constexpr bool operator==(std::nullptr_t) const
constexpr bool operator>(DateTime) constDetermines if the current object represents the date and time value that is later than the value represented by the specified DateTime object.
constexpr bool operator>(std::nullptr_t) const
constexpr bool operator>=(DateTime) constDetermines if the current object represents the date and time value that is later than or the same as the value represented by the specified DateTime object.
constexpr bool operator>=(std::nullptr_t) const
static DateTime Parse(const String&)Converts the specified string representation of a date and time value to the equivalent DateTime object.
static DateTime Parse(const String&, const SharedPtr<IFormatProvider>&, Globalization::DateTimeStyles)Converts the specified string representation of a date and time value to the equivalent DateTime object using culture-specific format information.
static DateTime Parse(const String&, const SharedPtr<Globalization::CultureInfo>&, Globalization::DateTimeStyles)
static DateTime Parse(const String&, const SharedPtr<Globalization::DateTimeFormatInfo>&, Globalization::DateTimeStyles)
static DateTime Parse(const String&, std::nullptr_t, Globalization::DateTimeStyles)
static DateTime ParseExact(const String&, const String&, const SharedPtr<IFormatProvider>&, Globalization::DateTimeStyles)Converts the specified string representation of a date and time value to the equivalent DateTime object using the specified format and culture-specific format information. The format of the string representation must match the specified format exactly. Throws an exception if the conversion fails.
static DateTime ParseExact(const String&, const String&, const SharedPtr<Globalization::CultureInfo>&, Globalization::DateTimeStyles)
static DateTime ParseExact(const String&, const String&, const SharedPtr<Globalization::DateTimeFormatInfo>&, Globalization::DateTimeStyles)
static DateTime ParseExact(const String&, const String&, std::nullptr_t, Globalization::DateTimeStyles)
static DateTime ParseExact(const String&, const ArrayPtr<String>&, const SharedPtr<IFormatProvider>&, Globalization::DateTimeStyles)Converts the specified string representation of a date and time value to the equivalent DateTime object using the specified formats, culture-specific format information and style. The format of the string representation must match one or more of the specified formats exactly. Throws an exception if the conversion fails.
static DateTime ParseExact(const String&, const ArrayPtr<String>&, const SharedPtr<Globalization::CultureInfo>&, Globalization::DateTimeStyles)
static DateTime ParseExact(const String&, const ArrayPtr<String>&, const SharedPtr<Globalization::DateTimeFormatInfo>&, Globalization::DateTimeStyles)
static DateTime ParseExact(const String&, const ArrayPtr<String>&, std::nullptr_t, Globalization::DateTimeStyles)
static DateTime SpecifyKind(DateTime, DateTimeKind)Constructs a new DateTime object that represents the same number of ticks as the specified DateTime object and represents local time, UTC time or neither as specified by the argument kind.
DateTime Subtract(TimeSpan) constReturns a new instance of the DateTime class representing the date and time value which is the result of subtraction of the specified time span from the value represented by the current object.
constexpr TimeSpan Subtract(DateTime) constReturns an instance of TimeSpan class representing the time interval between the date and time values represented by the current and the specified objects.
int64_t ToBinary() constSerializes the current object.
int64_t ToFileTime() constReturns a value that represents the date and time value represented by the current object as File time.
int64_t ToFileTimeUtc() constConverts the date and time value represented by the current object to File time UTC.
DateTime ToLocalTime() constReturns a new instance of DateTime class that represents the date and time value represented by the current object as local time.
String ToLongDateString() constReturns a string that contains the long date string representation of the current object.
String ToLongTimeString() constReturns a string that contains the long time string representation of the current object.
double ToOADate() constReturns the date and time value represented by the current object as OLE Automation Date.
String ToShortDateString() constReturns a string that contains the short date string representation of the current object.
String ToShortTimeString() constReturns a string that contains the short time string representation of the current object.
String ToString() constReturns the string representation of the date and time value represented by the current object using the formatting conventions defined by the current culture.
String ToString(const String&) constReturns a string representation of the date and time value represented by the current object using the specified format and formatting conventions defined by the current culture.
String ToString(const SharedPtr<IFormatProvider>&) constReturns a string representation of the date and time value represented by the current object using the specified format information.
String ToString(const SharedPtr<Globalization::CultureInfo>&) const
String ToString(const SharedPtr<Globalization::DateTimeFormatInfo>&) const
String ToString(std::nullptr_t) const
String ToString(const String&, const SharedPtr<IFormatProvider>&) constReturns a string representation of the date and time value represented by the current object using the specified format information.
String ToString(const String&, const SharedPtr<Globalization::CultureInfo>&) const
String ToString(const String&, const SharedPtr<Globalization::DateTimeFormatInfo>&) const
String ToString(const String&, std::nullptr_t) const
DateTime ToUniversalTime() constReturns a new instance of DateTime class that represents the date and time value represented by the current object as UTC.
time_t ToUnixTime() constReturns a value that represents the date and time value represented by the current object as Unix time. FOR INTERNAL USE.
static bool TryParse(const String&, DateTime&)Converts the specified string representation of a date and time value to the equivalent DateTime object.
static bool TryParse(const String&, const SharedPtr<IFormatProvider>&, Globalization::DateTimeStyles, DateTime&)Converts the specified string representation of a date and time value to the equivalent DateTime object using the specified culture-specific format information and style.
static bool TryParse(const String&, const SharedPtr<Globalization::CultureInfo>&, Globalization::DateTimeStyles, DateTime&)
static bool TryParse(const String&, const SharedPtr<Globalization::DateTimeFormatInfo>&, Globalization::DateTimeStyles, DateTime&)
static bool TryParse(const String&, std::nullptr_t, Globalization::DateTimeStyles, DateTime&)
static bool TryParseExact(const String&, const String&, const SharedPtr<IFormatProvider>&, Globalization::DateTimeStyles, DateTime&)Converts the specified string representation of a date and time value to the equivalent DateTime object using the specified format, culture-specific format information and style. The format of the string representation must match the specified format exactly.
static bool TryParseExact(const String&, const String&, const SharedPtr<Globalization::CultureInfo>&, Globalization::DateTimeStyles, DateTime&)
static bool TryParseExact(const String&, const String&, const SharedPtr<Globalization::DateTimeFormatInfo>&, Globalization::DateTimeStyles, DateTime&)
static bool TryParseExact(const String&, const String&, std::nullptr_t, Globalization::DateTimeStyles, DateTime&)
static bool TryParseExact(const String&, const ArrayPtr<String>&, const SharedPtr<IFormatProvider>&, Globalization::DateTimeStyles, DateTime&)Converts the specified string representation of a date and time value to the equivalent DateTime object using the specified formats, culture-specific format information and style. The format of the string representation must match one or more of the specified formats exactly.
static bool TryParseExact(const String&, const ArrayPtr<String>&, const SharedPtr<Globalization::CultureInfo>&, Globalization::DateTimeStyles, DateTime&)
static bool TryParseExact(const String&, const ArrayPtr<String>&, const SharedPtr<Globalization::DateTimeFormatInfo>&, Globalization::DateTimeStyles, DateTime&)
static bool TryParseExact(const String&, const ArrayPtr<String>&, std::nullptr_t, Globalization::DateTimeStyles, DateTime&)
static const TypeInfo& Type()Returns a TypeInfo object that contains information about this class.

Fields

FieldDescription
static constexpr MaxTicksThe number of 100-nanosecond in the time interval between the minimal possible and maximal possible DateTime value.
static MaxValueAn instance of DateTime class that represents the maximal possible date and time value.
static constexpr MinTicksThe minimal number of ticks that an instance of DateTime class can represent.
static MinValueAn instance of DateTime class that represents the minimal possible date and time value.
static constexpr TicksPerDayThe number of ticks in a day.
static constexpr TicksPerHourThe number of ticks in a hour.
static constexpr TicksPerMicrosecondThe number of ticks in a microsecond.
static constexpr TicksPerMillisecondThe number of ticks in a millisecond.
static constexpr TicksPerMinuteThe number of ticks in a minute.
static constexpr TicksPerSecondThe number of ticks in a second.
static UnixEpochAn instance of DateTime class that represents the Unix epoch start (1970.01.01 00:00:00).

Remarks

#include "system/console.h"
#include "system/date_time.h"

int main()
{
  using namespace System;

  // Create the 'DateTime' class instance.
  DateTime dateTime{1990, 10, 30};

  // Print the instance in the multiple formats.
  Console::WriteLine(dateTime.ToShortDateString());
  Console::WriteLine(dateTime.ToShortTimeString());
  Console::WriteLine(dateTime.ToString());

  return 0;
}
/*
This code example produces the following output:
30.10.1990
0:00
30.10.1990 0:00:00
*/

See Also