?
Solved

Conversion of Microsoft DATE to int day, int month, int year?

Posted on 2008-06-24
3
Medium Priority
?
1,427 Views
Last Modified: 2008-06-24
Any system or free code to convert the DATE value to the integer values representing the day, month, year?

Hi experts,

I need to convert the Microsoft data type DATE (typedef double, if I am not mistaken) to the integer values. So far I have found the code by Victor Vogelpoel http://www.codeproject.com/KB/datetime/exceldmy.aspx. Is there any "more official" code by Microsoft or any other proven code?

The value was read from a MS Access database using Microsoft.Jet.OLEDB.4.0 + ADO Recordset, thus getting the VARIANT value of the VT_DATE (the _variant_t v variable inside C++ source, v.date value).

Thanks,
    Petr
/*
    Excel serial date to Day, Month, Year and vise versa
    By Victor Vogelpoel http://www.codeproject.com/KB/datetime/exceldmy.aspx
*/
void ExcelSerialDateToDMY(DATE SerialDate, int &nDay, 
                          int &nMonth, int &nYear)
{
    int nSerialDate = static_cast<int>(SerialDate);
 
    // Excel/Lotus 123 have a bug with 29-02-1900. 1900 is not a
 
    // leap year, but Excel/Lotus 123 think it is...
 
    if (nSerialDate == 60)
    {
        nDay    = 29;
        nMonth    = 2;
        nYear    = 1900;
 
        return;
    }
    else if (nSerialDate < 60)
    {
        // Because of the 29-02-1900 bug, any serial date 
 
        // under 60 is one off... Compensate.
 
        nSerialDate++;
    }
 
    // Modified Julian to DMY calculation with an addition of 2415019
 
    int l = nSerialDate + 68569 + 2415019;
    int n = int(( 4 * l ) / 146097);
            l = l - int(( 146097 * n + 3 ) / 4);
    int i = int(( 4000 * ( l + 1 ) ) / 1461001);
        l = l - int(( 1461 * i ) / 4) + 31;
    int j = int(( 80 * l ) / 2447);
     nDay = l - int(( 2447 * j ) / 80);
        l = int(j / 11);
        nMonth = j + 2 - ( 12 * l );
    nYear = 100 * ( n - 49 ) + i + l;
}

Open in new window

0
Comment
Question by:pepr
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 31

Accepted Solution

by:
Zoppo earned 2000 total points
ID: 21853532
Hi pepr,

there's a function VariantTimeToSystemTime - as arguments it has a double value represanting the DATA and a pointer to an SYSTEMTIME instance which get's filled - the SYSTEMTIME structure has members for year, month, day a.s.o.

Hope that helps,

ZOPPO
0
 
LVL 31

Expert Comment

by:Zoppo
ID: 21853543
You could even use the COleDateTime class if you use MFC. This encapsulates calls to VariantTimeToSystemTime ...
0
 
LVL 29

Author Comment

by:pepr
ID: 21854128
Yes, this is what I have searched for (VariantTimeToSystemTime -- http://msdn.microsoft.com/en-us/library/ms221440(VS.80).aspx).

Thanks!

Petr
0

Featured Post

How Blockchain Is Impacting Every Industry

Blockchain expert Alex Tapscott talks to Acronis VP Frank Jablonski about this revolutionary technology and how it's making inroads into other industries and facets of everyday life.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
Did you know that more than 4 billion data records have been recorded as lost or stolen since 2013? It was a staggering number brought to our attention during last week’s ManageEngine webinar, where attendees received a comprehensive look at the ma…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question