< C++ .NET Formatting 4 | Index #1 | Index # 2 | C++ .NET Formatting 6 >


 

 

Playing With C++ .NET Formatting 5

 

 

The following is the topic available in this part.

  1. Standard DateTime Format Strings

 

 

Standard DateTime Format Strings

 

A standard DateTime format string consists of a single format specifier character from the following table. If the format specifier is not found in the table below, a runtime exception is thrown. If the format string is longer than a single character (even if the extra characters are white spaces), the format string is interpreted as a custom format string. Note that the result string produced by these format specifiers is influenced by the settings in the Regional Options control panel. Computers with different cultures or different date and time settings will generate different result strings.

The date and time separators displayed by format strings are defined by the DateSeparator and TimeSeparator characters associated with the DateTimeFormat property of the current culture. However, in cases where the InvariantCulture is referenced by the 'r', 's', and 'u' specifiers, the characters associated with the DateSeparator and TimeSeparator characters do not change based on the current culture. The following table describes the standard format specifiers for formatting the DateTime object.

 

Format specifier

Name

Description

d

Short date pattern

Displays a pattern defined by the DateTimeFormatInfo.ShortDatePattern property associated with the current thread or by a specified format provider.

D

Long date pattern

Displays a pattern defined by the DateTimeFormatInfo.LongDatePattern property associated with the current thread or by a specified format provider.

t

Short time pattern

Displays a pattern defined by the DateTimeFormatInfo.ShortTimePattern property associated with the current thread or by a specified format provider.

T

Long time pattern

Displays a pattern defined by the DateTimeFormatInfo.LongTimePattern property associated with the current thread or by a specified format provider.

f

Full date/time pattern (short time)

Displays a combination of the long date and short time patterns, separated by a space.

F

Full date/time pattern (long time)

Displays a pattern defined by the DateTimeFormatInfo.FullDateTimePattern property associated with the current thread or by a specified format provider.

g

General date/time pattern (short time)

Displays a combination of the short date and short time patterns, separated by a space.

G

General date/time pattern (long time)

Displays a combination of the short date and long time patterns, separated by a space.

M or m

Month day pattern

Displays a pattern defined by the DateTimeFormatInfo.MonthDayPattern property associated with the current thread or by a specified format provider.

R or r

RFC1123 pattern

Displays a pattern defined by the DateTimeFormatInfo.RFC1123Pattern property associated with the current thread or by a specified format provider. This is a defined standard and the property is read-only; therefore, it is always the same regardless of the culture used, or the format provider supplied. The property references the CultureInfo.InvariantCulture property and follows the custom pattern "ddd, dd MMM yyyy HH:mm:ss G\MT". Note that the 'M' in "GMT" needs an escape character so it is not interpreted. Formatting does not modify the value of the DateTime; therefore, you must adjust the value to GMT before formatting.

s

Sortable date/time pattern; conforms to ISO 8601

Displays a pattern defined by the DateTimeFormatInfo.SortableDateTimePattern property associated with the current thread or by a specified format provider. The property references the CultureInfo.InvariantCulture property, and the format follows the custom pattern "yyyy-MM-ddTHH:mm:ss".

u

Universal sortable date/time pattern

Displays a pattern defined by the DateTimeFormatInfo.UniversalSortableDateTimePattern property associated with the current thread or by a specified format provider. Because it is a defined standard and the property is read-only, the pattern is always the same regardless of culture or format provider. Formatting follows the custom pattern "yyyy-MM-dd HH:mm:ssZ". No time zone conversion is done when the date and time is formatted; therefore, convert a local date and time to universal time before using this format specifier.

U

Universal sortable date/time pattern

Displays a pattern defined by the DateTimeFormatInfo.FullDateTimePattern property associated with the current thread or by a specified format provider. The time displayed is the universal time, rather than the local time, equivalent to the DateTime value.

Y or y

Year month pattern

Displays a pattern defined by the DateTimeFormatInfo.YearMonthPattern property associated with the current thread or by a specified format provider.

Any other single character

Unknown specifier

   -

 

Table 5

 

The following code example demonstrates how to compare roughly equivalent DateTime values, accepting a small margin of difference when declaring them equal.

// testprog.cpp : main project file.

 

#include "stdafx.h"

 

using namespace System;

 

bool RoughlyEquals( DateTime time, DateTime timeWithWindow, int windowInSeconds, int frequencyInSeconds )

{

   long delta = (long)((TimeSpan)(timeWithWindow - time)).TotalSeconds % frequencyInSeconds;

   delta = delta > windowInSeconds ? frequencyInSeconds - delta : delta;

   return Math::Abs( delta ) < windowInSeconds;

}

 

int main()

{

      int window = 10;

      int freq = 60 * 60 * 2; // 2 hours;

 

      DateTime d1 = DateTime::Now;

      DateTime d2 = d1.AddSeconds(2 * window);

      DateTime d3 = d1.AddSeconds( -2 * window);

      DateTime d4 = d1.AddSeconds(window / 2);

      DateTime d5 = d1.AddSeconds( -window / 2);

      DateTime d6 = (d1.AddHours(2)).AddSeconds(2 * window);

      DateTime d7 = (d1.AddHours(2)).AddSeconds(-2 * window);

      DateTime d8 = (d1.AddHours(2)).AddSeconds(window / 2);

      DateTime d9 = (d1.AddHours(2)).AddSeconds(-window / 2);

     

      Console::WriteLine("d1 = {0}", d1);

      Console::WriteLine("d1 ~= d1 [true]: {0}\n", RoughlyEquals(d1, d1, window, freq));

      Console::WriteLine("d1 = {0}, d2 = {1}", d1, d2);

      Console::WriteLine("d1 ~= d2 [false]: {0}\n", RoughlyEquals(d1, d2, window, freq));

      Console::WriteLine("d1 = {0}, d3 = {1}", d1, d3);

      Console::WriteLine("d1 ~= d3 [false]: {0}\n", RoughlyEquals(d1, d3, window, freq));

      Console::WriteLine("d1 = {0}, d4 = {1}", d1, d4);

      Console::WriteLine("d1 ~= d4 [true]: {0}\n", RoughlyEquals(d1, d4, window, freq));

      Console::WriteLine("d1 = {0}, d5 = {1}", d1, d5);

      Console::WriteLine("d1 ~= d5 [true]: {0}\n", RoughlyEquals(d1, d5, window, freq));

      Console::WriteLine("d1 = {0}, d6 = {1}", d1, d6);

      Console::WriteLine("d1 ~= d6 [false]: {0}\n", RoughlyEquals(d1, d6, window, freq));

      Console::WriteLine("d1 = {0}, d7 = {1}", d1, d7);

      Console::WriteLine("d1 ~= d7 [false]: {0}\n", RoughlyEquals(d1, d7, window, freq));

      Console::WriteLine("d1 = {0}, d8 = {1}", d1, d8);

      Console::WriteLine("d1 ~= d8 [true]: {0}\n", RoughlyEquals(d1, d8, window, freq));

      Console::WriteLine("d1 = {0}, d9 = {1}", d1, d9);

      Console::WriteLine("d1 ~= d9 [true]: {0}\n", RoughlyEquals(d1, d9, window, freq));

      return 0;

}

 

Output:

 

 

 

 

C++ .NET formatting - code example demonstrates how to compare roughly equivalent DateTime values

 

The following code example prints out the different format patterns for the en-US culture using the format characters. It also displays the value of the properties associated with the format character.

// testprog.cpp : main project file.

 

#include "stdafx.h"

 

using namespace System;

using namespace System::Globalization;

 

int main()

{

      // Creates and initializes a DateTimeFormatInfo associated with the en-US culture.

      CultureInfo^ MyCI = gcnew CultureInfo("en-US", false);

      DateTimeFormatInfo^ myDTFI = MyCI->DateTimeFormat;

      // Creates a DateTime with the Gregorian date January 3, 2002 (year=2002, month=1, day=3).

      // The Gregorian calendar is the default calendar for the en-US culture.

      DateTime myDT = DateTime(2002,1,3);

      // Displays the format pattern associated with each format character.

      Console::WriteLine("Format  en-US Example");

      Console::WriteLine("char    Value of associated property, if any");

      Console::WriteLine("=============================================\n");

      Console::WriteLine("d {0}", myDT.ToString("d"));

      Console::WriteLine("{0} {1}\n", myDTFI->ShortDatePattern, "(ShortDatePattern)");

      Console::WriteLine("D {0}", myDT.ToString("D"));

      Console::WriteLine(" {0} {1}\n", myDTFI->LongDatePattern, "(LongDatePattern)");

      Console::WriteLine("f {0}\n", myDT.ToString( "f" ) );

      Console::WriteLine("F {0}", myDT.ToString("F"));

      Console::WriteLine(" {0} {1}\n", myDTFI->FullDateTimePattern, "(FullDateTimePattern)");

      Console::WriteLine("g {0}\n", myDT.ToString("g"));

      Console::WriteLine("G {0}\n", myDT.ToString("G"));

      Console::WriteLine("m {0}", myDT.ToString("m"));

      Console::WriteLine(" {0} {1}\n", myDTFI->MonthDayPattern, "(MonthDayPattern)");

      Console::WriteLine("M {0}", myDT.ToString("M" ) );

      Console::WriteLine(" {0} {1}\n", myDTFI->MonthDayPattern, "(MonthDayPattern)");

      Console::WriteLine("r {0}", myDT.ToString("r" ) );

      Console::WriteLine(" {0} {1}\n", myDTFI->RFC1123Pattern, "(RFC1123Pattern)");

      Console::WriteLine("R {0}", myDT.ToString("R"));

      Console::WriteLine(" {0} {1}\n", myDTFI->RFC1123Pattern, "(RFC1123Pattern)");

      Console::WriteLine("s {0}", myDT.ToString("s"));

      Console::WriteLine(" {0} {1}\n", myDTFI->SortableDateTimePattern, "(SortableDateTimePattern)");

      Console::WriteLine("t {0}", myDT.ToString("t"));

      Console::WriteLine(" {0} {1}\n", myDTFI->ShortTimePattern, "(ShortTimePattern)");

      Console::WriteLine("T {0}", myDT.ToString("T"));

      Console::WriteLine(" {0} {1}\n", myDTFI->LongTimePattern, "(LongTimePattern)");

      Console::WriteLine("u {0}", myDT.ToString("u"));

      Console::WriteLine(" {0} {1}\n", myDTFI->UniversalSortableDateTimePattern, "(UniversalSortableDateTimePattern)");

      Console::WriteLine("U {0}\n", myDT.ToString( "U" ) );

      Console::WriteLine("y {0}", myDT.ToString("y"));

      Console::WriteLine(" {0} {1}\n", myDTFI->YearMonthPattern, "(YearMonthPattern)");

      Console::WriteLine("Y {0}", myDT.ToString("Y"));

      Console::WriteLine(" {0} {1}\n", myDTFI->YearMonthPattern, "(YearMonthPattern)");

 

   return 0;

}

 

Output:

Format  en-US Example

char    Value of associated property, if any

=============================================

 

d 1/3/2002

M/d/yyyy (ShortDatePattern)

 

D Thursday, January 03, 2002

 dddd, MMMM dd, yyyy (LongDatePattern)

 

f Thursday, January 03, 2002 12:00 AM

 

F Thursday, January 03, 2002 12:00:00 AM

 dddd, MMMM dd, yyyy h:mm:ss tt (FullDateTimePattern)

 

g 1/3/2002 12:00 AM

 

G 1/3/2002 12:00:00 AM

 

m January 03

 MMMM dd (MonthDayPattern)

 

M January 03

 MMMM dd (MonthDayPattern)

 

r Thu, 03 Jan 2002 00:00:00 GMT

 ddd, dd MMM yyyy HH':'mm':'ss 'GMT' (RFC1123Pattern)

 

R Thu, 03 Jan 2002 00:00:00 GMT

 ddd, dd MMM yyyy HH':'mm':'ss 'GMT' (RFC1123Pattern)

 

s 2002-01-03T00:00:00

 yyyy'-'MM'-'dd'T'HH':'mm':'ss (SortableDateTimePattern)

 

t 12:00 AM

 h:mm tt (ShortTimePattern)

 

T 12:00:00 AM

 h:mm:ss tt (LongTimePattern)

 

u 2002-01-03 00:00:00Z

 yyyy'-'MM'-'dd HH':'mm':'ss'Z' (UniversalSortableDateTimePattern)

 

U Wednesday, January 02, 2002 4:00:00 PM

 

y January, 2002

 MMMM, yyyy (YearMonthPattern)

 

Y January, 2002

 MMMM, yyyy (YearMonthPattern)

 

Press any key to continue . . .

 

 

 

 

 

Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7 | Part 8 | Part 9


< C++ .NET Formatting 4 | Index #1 | Index # 2 | C++ .NET Formatting 6 >