Click here to Skip to main content
13,097,012 members (38,645 online)
Click here to Skip to main content
Add your own
alternative version


9 bookmarked
Posted 10 Dec 2013

Exchange Date Time between Native C++ and C#

, 13 Dec 2013
Rate this:
Please Sign up or sign in to vote.
How to exchange date time between C# and C++


A coworker asked me an interesting question. Here is his question:

We need to pass a Date (and potentially time) between C++ and C#. What way do you think would be best for this/what is built in C++? Since time (potentially milliseconds) is a component to the date, I don’t want to pass a string, nor do I want to decompose the date time into elements (i.e. month, day, hour, year, minutes, etc.).
Just wondering if there is a common convention between C# and C++ we could use.
OK. Well I didn't know the answer right away. After some digging and searching around, here is my solution.


We know that raw data types like integer type (32-bit or 64-bit) are the same across C++ and C# and their date time are nothing but different formats of timestamps, so it's possible to use an integer number to finish up the job.

Using the Code

What is a .NET DateTime? Here is what from MSDN:

Represents an instant in time, typically expressed as a date and time of day.
How does it get constructed? There are many constructors to create a DateTime object. For the purpose of exchanging data between C# and C++, the following constructor is crucial.
DateTime(Int64) initializes a new instance of the DateTime structure to a specified number of ticks.
// This example demonstrates the DateTime(Int64) constructor. 
using System;
using System.Globalization;

class Sample 
    public static void Main() 
        // Instead of using the implicit, default "G" date and time format string, we 
        // use a custom format string that aligns the results and inserts leading zeroes. 
        string format = "{0}) The {1} date and time is {2:MM/dd/yyyy hh:mm:ss tt}";

        // Create a DateTime for the maximum date and time using ticks.
        DateTime dt1 = new DateTime(DateTime.MaxValue.Ticks);

        // Create a DateTime for the minimum date and time using ticks.
        DateTime dt2 = new DateTime(DateTime.MinValue.Ticks);

        // Create a custom DateTime for 7/28/1979 at 10:35:05 PM using a  
        // calendar based on the "en-US" culture, and ticks. 
        long ticks = new DateTime(1979, 07, 28, 22, 35, 5, 
            new CultureInfo("en-US", false).Calendar).Ticks;
        DateTime dt3 = new DateTime(ticks);

        Console.WriteLine(format, 1, "maximum", dt1);
        Console.WriteLine(format, 2, "minimum", dt2);
        Console.WriteLine(format, 3, "custom ", dt3);
        Console.WriteLine("\nThe custom date and time is created from {0:N0} ticks.", ticks);
This example produces the following results:

1) The maximum date and time is 12/31/9999 11:59:59 PM
2) The minimum date and time is 01/01/0001 12:00:00 AM
3) The custom  date and time is 07/28/1979 10:35:05 PM

The custom date and time is created from 624,376,461,050,000,000 ticks.

The number (624,376,461,050,000,000) will be passed to C++ to construct a C structure. The following snippet is taken from here. Thanks!

// Convert C# DateTime to C++ date time
    // 624376461050000000 (dec)  07/28/1979 10:35:05 PM
    unsigned __int64 DTFromCDull = 0x8AA3B154F003280; 
   // Thursday, January 01, 1970 12:00:00 AM  
   unsigned __int64 UnixEpoch = 0x089f7ff5f7b58000;  

   time_t t;
   struct tm *tm;

   printf("%s \n",asctime(tm));
Output: Sat Jul 28 22:35:05 1979  

How do we convert C++ ticks back to C#?

// Assuming tick is passed in by C++ which is:
// time_t ticks = time(0);
long timeNow = ticks * 10000000 + 0x089f7ff5f7b58000;
DateTime dt = new DateTime(timeNow); 

How do you deal with the milliseconds within C# DateTime? The answer is do exactly what .NET does.

C# source code for DateTime can be found from Microsoft website or search within this website:[^].

C++ code for converting ticks into C# DateTime milliseconds:

__int64 tick = 0x8AA3B154F003280;
int millisecond = ((ticks & 0x3FFFFFFFFFFFFFFF) / 10000) % 1000

If you want to convert between different timestamps, the following references will be intensively helpful.

It's quite possible I might have missed something, please let me know. Many thanks to Jeff for inspiring me to write this up.



This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

dragonfly lee
United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralThanks for the write-up. +5 Pin
Ryan S White12-Mar-15 19:06
professionalRyan S White12-Mar-15 19:06 
GeneralRe: Thanks for the write-up. +5 Pin
dragonfly lee11-Apr-15 4:04
memberdragonfly lee11-Apr-15 4:04 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.170813.1 | Last Updated 13 Dec 2013
Article Copyright 2013 by dragonfly lee
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid