Click here to Skip to main content
13,249,512 members (37,622 online)
Click here to Skip to main content
Add your own
alternative version


17 bookmarked
Posted 9 Feb 2007

SystemTime to VariantTime with Milliseconds

, 9 Feb 2007
Rate this:
Please Sign up or sign in to vote.
Enhanced system time / variant time conversion with milliseconds


When you use the Microsoft Automation functions VariantTimeToSystemTime to convert variant time to system time and the SystemTimeToVariantTime to convert system time to variant time, the milliseconds value appears as zero or is ignored. This is known issue documented in MSDN knowledge base, under ID Q297463. However many times ignoring milliseconds is not a option. Two simple functions wrapping the original API functions can be used to convert SystemTime to VariantTime and vice versa without loosing the millisecond information.

The Sample application is just to demonstrate the use of these functions. You can type in the SystemTime entries and click convert to see the equivalent VariantTime or you can type in a VariantTime value and select conversion Type for reverse conversion and convert it back to SystemTime. Clicking on the 'GetCurrentTime' button will populate SystemTime structure from the current time on your system. The application also does some basic validations on the range of input you can specify.

Using the code

Two functions which SystemTimeToVariantTimeWithMilliseconds and VariantTimeToSystemTimeWithMilliseconds can replace the Microsoft Automation functions and can return the result in a similar to the original functions without losing the millisecond information.

a) SystemTimeToVariantTimeWithMilliseconds takes systemtime as the input and passes the information without millisecond information to the Microsoft Automation function. The result would be the converted variant time without milliseconds. The reason for not passing the millisecond information to the Microsoft Automation function is that the function automatically rounds off the value to the nearest second, which we don't want. We then add the variant portion for the millisecond.

A variant time is stored as an 8-byte real value (double), representing a date between January 1, 1753 and December 31, 2078, inclusive. The value 2.0 represents January 1, 1900; 3.0 represents January 2, 1900, and so on. Adding 1 to the value increments the date by a day. The fractional part of the value represents the time of day. Therefore, 2.5 represents noon on January 1, 1900; 3.25 represents 6:00 A.M. on January 2, 1900, and so on. So, 0.5 represents 12 hours i.e. 12*60*60 seconds, hence 1 second = 0.5/(12*60*60) = .0000115740740740

BOOL CSysTimeConversionDlg::SystemTimeToVariantTimeWithMilliseconds 
                 (/*input*/ SYSTEMTIME st, /*output*/double *dVariantTime)
    BOOL retVal = TRUE;

    WORD wMilliSeconds = st.wMilliseconds; // save the milli second 
                                           // information
    st.wMilliseconds = 0; // pass 0 milliseconds to the function and get  
                          // the converted value without milliseconds
    double dWithoutms;
    retVal = SystemTimeToVariantTime(&st, &dWithoutms) ;

    // manually convert the millisecond information into variant 
    // fraction and add it to system converted value
    double OneMilliSecond = ONETHOUSANDMILLISECONDS/1000 ;
    *dVariantTime = dWithoutms + (OneMilliSecond * wMilliSeconds);

    return retVal;

b) VariantTimeToSystemTimeWithMilliseconds takes in the variant time and calculates each component of SYSTEMTIME individually down to the milliseconds and thus will have the precious millisecond information. We start with using the Microsoft Automation function VariantTimeToSystemTime to give us the variant time from system time. We delete 0.5 seconds from the original variant time so that we remove the rounding off problem with VariantTimeToSystemTime function. We then calculate each component of the systemtime from the fraction until we obtain the millisecond information. Once we have the millisecond information, we then add 0.5 second to compensate for our earlier adjustment.

BOOL CSysTimeConversionDlg::VariantTimeToSystemTimeWithMilliseconds 
                  (/*input*/ double dVariantTime, /*output*/SYSTEMTIME *st)
    BOOL retVal = TRUE;

    double halfsecond = ONETHOUSANDMILLISECONDS / 2.0; 
    // ONETHOUSANDMILLISECONDS is equal to 0.0000115740740740

    retVal = VariantTimeToSystemTime(dVariantTime - halfsecond, st); 
    // this takes care of rounding problem with 
    // VariantTimetoSystemTime function
    if (retVal == FALSE)
        return retVal;

    double fraction = dVariantTime - (int) dVariantTime; 
    // extracts the fraction part

    double hours; 
    hours = fraction = (fraction - (int)fraction) * 24;

    double minutes;
    minutes = (hours - (int)hours) * 60;

    double seconds;
    seconds = (minutes - (int)minutes) * 60;

    double milliseconds;
    milliseconds = (seconds - (int)seconds) * 1000;

    milliseconds = milliseconds + 0.5; // rounding off millisecond to the 
                                       // nearest millisecond 
    if (milliseconds < 1.0 || milliseconds > 999.0) //Fractional 
                          // calculations may yield in results like
        milliseconds = 0; // 0.00001 or 999.9999 which should actually 
                          // be zero (slightly above or below limits 
                          // are actually zero)

    if (milliseconds) 
        st->wMilliseconds = (WORD) milliseconds;
    else  // if there is 0 milliseconds, then we don't have the problem !!
        retVal = VariantTimeToSystemTime(dVariantTime, st); // 

    return retVal;


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Web Developer
United States United States
I started programming some 10 years back, working on on a 386 PC writing some embedded systems code, but soon moved on into application programming. I have worked on Microsoft environment since the beginning of MSDOS and Windows 3.1. I have had several years of hands on experience with C++, Win32, MFC, ATL COM, Visual Basic and C#
I work IBM Global Services and currently living in Lake Forest California.

Occupation : Software design and development

Other than my work, my special areas of interest is Computer hardware, photography, music and movies.

You may also be interested in...

Comments and Discussions

GeneralMy vote of 5 Pin
Member 68306924-Oct-14 7:43
memberMember 68306924-Oct-14 7:43 
GeneralMy vote of 5 Pin
Siriwat.Suananpornpanit15-Jun-11 21:14
groupSiriwat.Suananpornpanit15-Jun-11 21:14 
GeneralRounding problem when miiliseconds is 999 Pin
sbt@ismobile14-Oct-09 22:59
membersbt@ismobile14-Oct-09 22:59 
Nice work! However when milliseconds is 999 there seem to be a rounding problem when converting from systemtime to varianttime and back again. E.g. 12:00:00:999 -> <varianttime> ->12:00:01:000. Is this possible to fix?
GeneralRe: Rounding problem when miiliseconds is 999 Pin
skarapanahalli21-Nov-11 3:22
memberskarapanahalli21-Nov-11 3:22 
GeneralBasically and practically error-prone conversion Pin
ehaerim11-Jan-14 14:34
memberehaerim11-Jan-14 14:34 
GeneralRe: Basically and practically error-prone conversion Pin
mgampi7-Jul-14 0:20
membermgampi7-Jul-14 0:20 
QuestionRange of Dates? Pin
MichP15-Feb-07 6:26
memberMichP15-Feb-07 6:26 
AnswerRe: Range of Dates? Pin
skydvr16-Feb-07 11:14
memberskydvr16-Feb-07 11:14 
GeneralRe: Range of Dates? Pin
skarapanahalli20-Feb-07 16:07
memberskarapanahalli20-Feb-07 16:07 

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
Web02 | 2.8.171114.1 | Last Updated 9 Feb 2007
Article Copyright 2007 by skarapanahalli
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid