DateTime projection in C++/CX

C# projects Windows::Foundation::DateTime as System.DateTimeOffset, whereas C++ just exposes the raw structure. This can be rather unnerving if you are trying to port some C# code to C++/CX. All you have to work with is the UniversalTime property which is a long long that MSDN defines as the number of 100-nanosecond intervals prior to or after midnight on January 1, 1601. Even a simple task as displaying a formatted date/time string is difficult as ToString merely returns the fully qualified name of the type.

Fortunately enough, Windows::Globalization::DateTimeFormatting includes the DateTimeFormatter class that can be used to get readable date/time display strings from a DateTime structure. Here’s some sample code that shows how you can use this.

auto dateFormatter = DateTimeFormatter::LongDate;
auto timeFormatter = DateTimeFormatter::LongTime;

for(auto item : feed->Items)
    dateFormatter->Format(item->PublishedDate) + " " +
    timeFormatter->Format(item->PublishedDate) + " - " +  

Obviously, it’d have been heaps better if the compiler gave us easier to use projections, but it’s still not that bad. A common practice is to convert this to standard C++ or ATL/Win32 date structures and formats if you want to do additional date/time processing on the object.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s