{"id":67583,"date":"2006-04-07T18:37:00","date_gmt":"2006-04-07T18:37:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2006\/04\/07\/how-can-i-convert-a-number-to-a-date-in-excel\/"},"modified":"2006-04-07T18:37:00","modified_gmt":"2006-04-07T18:37:00","slug":"how-can-i-convert-a-number-to-a-date-in-excel","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-convert-a-number-to-a-date-in-excel\/","title":{"rendered":"How Can I Convert a Number to a Date in Excel?"},"content":{"rendered":"<p><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" height=\"34\" alt=\"Hey, Scripting Guy! Question\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" align=\"left\" border=\"0\"> \n<P>Hey, Scripting Guy! How can I convert a number in Excel to a date; for example, how can I convert 40806 to 4\/8\/2006?<BR><BR>&#8212; JM<\/P><IMG height=\"5\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" border=\"0\"><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" height=\"34\" alt=\"Hey, Scripting Guy! Answer\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" align=\"left\" border=\"0\"><A href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><IMG class=\"farGraphic\" title=\"Script Center\" height=\"288\" alt=\"Script Center\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" width=\"120\" align=\"right\" border=\"0\"><\/A> \n<P>Hey, JM. You probably didn\u2019t know this, but one of the Scripting Guys has a brother who\u2019s an actor. As every actor knows, nepotism is pretty prevalent in the acting world. Well, it\u2019s a good thing the Scripting Guys aren\u2019t actors, because we\u2019d never stoop to such favoritism. (There are other reasons it\u2019s a good thing that the Scripting Guys aren\u2019t actors, but that\u2019s another story.) We randomly select our questions to answer from the hundreds we receive every day. So, little brother, uh, JM, what was it you wanted to know? Oh yes, numbers-to-dates in Excel.<\/P>\n<P>From your email, it sounds like you have a spreadsheet with a column of numbers where the first one or two digits (depending on the month) represent the month, the next two (always two) digits represent the day, and the last two or four (either 06 or 2006) represent the year. Something like this:<\/P><IMG height=\"304\" alt=\"Hey, Scripting Guy!\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/exceldates.jpg\" width=\"362\" border=\"0\"> \n<P><BR>Our first thought was to simply use the <B>FormatDateTime<\/B> method. This method accepts a string and outputs that string in date format:<\/P><PRE class=\"codeSample\">strNew = FormatDateTime(strOld)\n<\/PRE>\n<P>The problem we ran into is that the first date in our table, 40806, became 9\/20\/2011. Why? Well, the \u2026 interesting \u2026 thing about Excel is that when you try to turn a number into a date the program assumes that the number is a serial number representing the number of days that have occurred since Jan. 1, 1900. Turns out that 40806 days after Jan. 1, 1900, is Sept. 20, 2011. Back to the drawing board.<\/P>\n<P>In the end, we decided to treat the number like a string: use the VBScript functions <B>Left<\/B>, <B>Right<\/B>, and <B>Mid<\/B> to take the string apart, and then put everything back together with the appropriate symbols that will convince Excel we want a real date, not a serial number. Here\u2019s what we came up with:<\/P><PRE class=\"codeSample\">Set objExcel = CreateObject(&#8220;Excel.Application&#8221;)\nSet objWorkbook = objExcel.Workbooks.Open(&#8220;C:\\scripts\\test.xls&#8221;)<\/p>\n<p>intRow = 1\nintCol = 1\ni = 0<\/p>\n<p>Do Until objExcel.Cells(intRow,intCol).Value = &#8220;&#8221;\n    strDate = objExcel.Cells(intRow, intCol).Value\n    i = Len(strDate)\n    If i &gt; 6 Then\n        dtYear = Right(strDate, 4)\n        i = i &#8211; 5\n    Else\n        dtYear = Right(strDate, 2)\n        i = i &#8211; 3\n    End If\n    dtDay = Mid(strDate, i, 2)\n    i = i &#8211; 1\n    dtMonth = Left(strDate, i)\n    newDate = dtMonth &amp; &#8220;\/&#8221; &amp; dtDay &amp; &#8220;\/&#8221; &amp; dtYear<\/p>\n<p>    objExcel.Cells(intRow, intCol).Value = newDate\n    intRow = intRow + 1\nLoop<\/p>\n<p>objExcel.Visible = True\n<\/PRE>\n<P>The first thing we do is create an Excel object and open our spreadsheet. We then set up a Do loop to loop through the entire column of dates. The loop will continue until we reach an empty cell in the column; if you don\u2019t have a date in every column, you\u2019ll have to modify this script.<\/P>\n<P>Next, we read in the date from the first cell, which happens to be row 1, column 1:<\/P><PRE class=\"codeSample\">strDate = objExcel.Cells(intRow, intCol).Value\n<\/PRE>\n<P>We then use the Len function to retrieve the number of characters in our date string. Because our string won\u2019t always be the same length we\u2019ll use this number as we read through the string to keep track of how many characters we have left.<\/P>\n<P>In reading our string, we start from the end of the string. Given the format of the dates, the longest a string with a two-digit year can be is six characters, while the shortest a string can be with a four-digit year is seven characters. So we check to see whether the string is greater than six characters long; if it is, that must mean this particular value uses four digits for the year. Consequently, we use the Right function to read the last four characters and save them as the year. If the string is six characters or less, that can only mean one thing: it uses two digits to indicate the year. Therefore, we read in only the last two characters and save those two characters as the year:<\/P><PRE class=\"codeSample\">If i &gt; 6 Then\n    dtYear = Right(strDate, 4)\n    i = i &#8211; 5\nElse\n    dtYear = Right(strDate, 2)\n    i = i &#8211; 3\nEnd If\n<\/PRE>\n<P>As you can see, we also subtract the length of the date plus one from our remaining string length. We use that number as we continue to work backwards through the string to read the day (and yes, these are U.S. settings):<\/P><PRE class=\"codeSample\">dtDay = Mid(strDate, i, 2)\ni = i &#8211; 1\n<\/PRE>\n<P>Here, we\u2019re using the Mid function to say that we want to retrieve two characters starting at position i, which, as we said, is the length of the string minus the length of the year, minus one more because we\u2019re retrieving two characters. In other words, our first string is five characters, so the value of i is 5. After reading the year, the value of i is 5 &#8211; 2 &#8211; 1, or 2. Starting at position 2, we take two characters, two characters which just happen to represent the day. We then subtract 1, and use that position to retrieve the month:<\/P><PRE class=\"codeSample\">dtMonth = Left(strDate, i)\n<\/PRE>\n<P>Here we use the Left function to retrieve the remaining number of characters (in this case, one) and save them as the month. Then we simply put the string together, separating the parts of the date with slashes (\/):<\/P><PRE class=\"codeSample\">newDate = dtMonth &amp; &#8220;\/&#8221; &amp; dtDay &amp; &#8220;\/&#8221; &amp; dtYear\n<\/PRE>\n<P>After that we put the new string into the cell we read the date from, increment the row number, and loop back to start on the next row in the column. The last thing we do is make Excel visible so you can see the results.<\/P>\n<P>So that\u2019s it. Say hi to Mom for us. Um, say hi to <I>your<\/I> Mom for us \u2026.<\/P><BR>\n<DIV>\n<TABLE class=\"\" cellSpacing=\"0\" cellPadding=\"0\" width=\"100%\" border=\"0\">\n<TBODY>\n<TR>\n<TD class=\"\"><A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/resources\/qanda\/apr06\/hey0407.mspx#top\"><IMG height=\"9\" alt=\"Top of page\" src=\"http:\/\/www.microsoft.com\/technet\/mnplibrary\/templates\/MNP2.Common\/images\/arrow_px_up.gif\" width=\"7\" border=\"0\"><\/A><A class=\"topOfPage\" href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/resources\/qanda\/apr06\/hey0407.mspx#top\">Top of page<\/A><\/TD><\/TR><\/TBODY><\/TABLE><\/DIV><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I convert a number in Excel to a date; for example, how can I convert 40806 to 4\/8\/2006?&#8212; JM Hey, JM. You probably didn\u2019t know this, but one of the Scripting Guys has a brother who\u2019s an actor. As every actor knows, nepotism is pretty prevalent in the acting world. [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[48,49,3,5],"class_list":["post-67583","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-microsoft-excel","tag-office","tag-scripting-guy","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! How can I convert a number in Excel to a date; for example, how can I convert 40806 to 4\/8\/2006?&#8212; JM Hey, JM. You probably didn\u2019t know this, but one of the Scripting Guys has a brother who\u2019s an actor. As every actor knows, nepotism is pretty prevalent in the acting world. [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/67583","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=67583"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/67583\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=67583"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=67583"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=67583"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}