13,248,562 members (77,975 online)
alternative version

#### Stats

351.6K views
30 bookmarked
Posted 12 Aug 2002

# Excel serial date to Day, Month, Year and vise versa

, 12 Aug 2002
 Rate this:
A speedy algorithm to convert Excel numbers to DMY and vise versa.

## Introduction

For a little project of mine, I needed to convert a serial date number to day/month/year (DMY) and vise versa. The serial number came from date field in a converted Paradox database and seemed to be exactly the same as the serial date in Microsoft Excel, hence the article name. After some googling I learned that the Excel serial date is related to Julian date, and found a speedy algorithm to convert these numbers to DMY and vise versa.

### Excel Serial Date Number

Now what is an Excel serial date number? 37477 is a serial date number and is the number of days since 1-1-1900. 37477 actually translates to Aug 9, 2002, the date of writing this article.

The number of days since 1-1-1900 isn't that hard to calculate of course, once you know the leap years. Microsoft Excel however contains a bug with its date calculation: it considers 29-02-1900 as a valid date, but 1900 isn't a leap year! 29-02-1900 is not a valid date!

According to the stories, Microsoft decided to duplicate this date bug from Lotus 123, which was the dominating spreadsheet application at the time Excel was being written. This allowed Excel to open Lotus 123 spreadsheets without date problems.

(Note that a serial date number may also contain a fraction that denotes the time. It's actually the percentage of 24 hours, but that's not for this article.)

### Mac Excel Serial Date number

Excel on the Apple Mac uses a slightly different Serial Date number base: the number of days since 1-1-1904. That's why Excel also features a "1904 Date System" checkbox in its Options (Calculation tab). I won't go into this futher.

## Translating Serial Date Number to DMY

First of all, credit where credit is due. I got the basic algorithm from http://serendipity.magnet.ch/hermetic/cal_stud/jdn.htm. It's about converting a Julian date to DMY using integer calculations. I won't bore you with the theory (following the above hyperlink), but the original Julian has a different base date.

With an addition of a certain number, the algorithm comes close to the Excel serial date. And when the 29-02-1900 issue is handled, we've got an Excel serial date to Day, Month, Year calculation!

```void ExcelSerialDateToDMY(int nSerialDate, int &nDay,
int &nMonth, int &nYear)
{
// 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;
}```

## DMY to Excel Serial Date

Calculate an Excel serial date from Day, Month, Year. The function assumes that the day, month and year are valid date numbers.

```int DMYToExcelSerialDate(int nDay, int nMonth, int nYear)
{
// Excel/Lotus 123 have a bug with 29-02-1900. 1900 is not a
// leap year, but Excel/Lotus 123 think it is...
if (nDay == 29 && nMonth == 02 && nYear==1900)
return 60;

// DMY to Modified Julian calculatie with an extra substraction of 2415019.
long nSerialDate =
int(( 1461 * ( nYear + 4800 + int(( nMonth - 14 ) / 12) ) ) / 4) +
int(( 367 * ( nMonth - 2 - 12 * ( ( nMonth - 14 ) / 12 ) ) ) / 12) -
int(( 3 * ( int(( nYear + 4900 + int(( nMonth - 14 ) / 12) ) / 100) ) ) / 4) +
nDay - 2415019 - 32075;

if (nSerialDate < 60)
{
// Because of the 29-02-1900 bug, any serial date
// under 60 is one off... Compensate.
nSerialDate--;
}

return (int)nSerialDate;
}```

## About the Author

 Software Developer (Senior) Netherlands
Victor is consulting in The Netherlands.

His interests include Windows and web application development using .NET technologies and even some Apache/PHP/MySQL...

## Comments and Discussions

 First PrevNext
 DateTime.ToOADate() Dietmar Schoder29-Jul-14 6:27 Dietmar Schoder 29-Jul-14 6:27
 seriel no to DMY rbsshetty2-Dec-12 2:39 rbsshetty 2-Dec-12 2:39
 Alternative JamesHoward97224-Jan-12 23:33 JamesHoward972 24-Jan-12 23:33
 Different Alg Memonic15-Dec-11 8:02 Memonic 15-Dec-11 8:02
 Re: Different Alg Victor Vogelpoel15-Dec-11 10:01 Victor Vogelpoel 15-Dec-11 10:01
 Thanks. sriest10-Jul-09 11:01 sriest 10-Jul-09 11:01
 Hi. Thanks for the invaluable contribution. I have found a bug.... Member 402179718-Nov-08 7:57 Member 4021797 18-Nov-08 7:57
 Error in Excel Serial Date to DMY ?? Harry Adams17-Sep-08 18:33 Harry Adams 17-Sep-08 18:33
 Ported to PHP and fixed Lubosdz9-Aug-07 1:19 Lubosdz 9-Aug-07 1:19
 Works great for date, what about for time? TariAbay29-Jul-07 21:19 TariAbay 29-Jul-07 21:19
 Re: Works great for date, what about for time? Member 22281364-Aug-10 17:26 Member 2228136 4-Aug-10 17:26
 YEAR gtown717-May-07 5:20 gtown7 17-May-07 5:20
 Going off after Febuvary... vijaicmo12-Mar-07 1:23 vijaicmo 12-Mar-07 1:23
 Hey Victor, First of all thank you for ur post. Very helpful article. I am trying to convert your fucntion DMY to Excel Serial Date to PHP and I am newbe in PHP. function DMYToExcelSerialDate(\$nDay, \$nMonth, \$nYear) { // Excel/Lotus 123 have a bug with 29-02-1900. 1900 is not a // leap year, but Excel/Lotus 123 think it is... if (\$nDay == 29 && \$nMonth == 02 && \$nYear==1900) return 60; // DMY to Modified Julian calculate with an extra substraction of 2415019. \$nSerialDate = ((1461 * (\$nYear + 4800 + (\$nMonth - 14) / 12) / 4) + ( 367 * (\$nMonth - 2 - 12 * ( (\$nMonth - 14) / 12) ) / 12) - ( 3 * (\$nYear + 4900 + (\$nMonth - 14) / 12) / 100) / 4)+ \$nDay - 2415019 - 32075; if (\$nSerialDate <= 60) { // Because of the 29-02-1900 bug, any serial date // under 60 is one off... Compensate. \$nSerialDate--; } return \$nSerialDate; } I have put some constants for date values (1st of every month in a year) like from 1/1/2006 (DD,MM,YYYY), 1/2/2006, 1/3/2006.... 1/12/2006(DD/MM/YYYY). But when I am calling this function for convertion its giving the serial numbers. 39083 39114 39144 39175 39205 39235 39266 39296 39327 39357 39388 39418 Which represents in Excel... 1/1/2007 1/2/2007 3/3/2007 3/4/2007 3/5/2007 2/6/2007 3/7/2007 2/8/2007 2/9/2007 2/10/2007 2/11/2007 2/12/2007 This means its going out after februvary. Could u plz help with this. Thanks in advance. Vijai
 But how can I know if the cell value is a Date? stanley guan7-Oct-05 17:51 stanley guan 7-Oct-05 17:51
 Re: But how can I know if the cell value is a Date? Victor Vogelpoel7-Oct-05 23:56 Victor Vogelpoel 7-Oct-05 23:56
 Re: But how can I know if the cell value is a Date? Suunil21-Aug-09 3:21 Suunil 21-Aug-09 3:21
 Re: But how can I know if the cell value is a Date? Victor Vogelpoel24-Aug-09 0:51 Victor Vogelpoel 24-Aug-09 0:51
 Number off by 183 Anonymous29-Aug-05 5:33 Anonymous 29-Aug-05 5:33
 Re: Number off by 183 Anonymous29-Aug-05 7:03 Anonymous 29-Aug-05 7:03
 DMYToExcelSerialDate doesn't work for 28th Feb 1900 crusby10-Feb-05 0:09 crusby 10-Feb-05 0:09
 Re: DMYToExcelSerialDate doesn't work for 28th Feb 1900 Victor Vogelpoel10-Feb-05 11:10 Victor Vogelpoel 10-Feb-05 11:10
 doesn't work in months input > 12 Anonymous24-Jun-04 13:23 Anonymous 24-Jun-04 13:23
 Re: doesn't work in months input > 12 Victor Vogelpoel24-Jun-04 23:54 Victor Vogelpoel 24-Jun-04 23:54
 Thanks - Would like some clarifications jocompto4-May-04 10:11 jocompto 4-May-04 10:11
 Re: Thanks - Would like some clarifications Victor Vogelpoel4-May-04 11:29 Victor Vogelpoel 4-May-04 11:29
 Last Visit: 31-Dec-99 19:00     Last Update: 18-Nov-17 7:51 Refresh 12 Next »

General    News    Suggestion    Question    Bug    Answer    Joke    Praise    Rant    Admin

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