DateDiff produces incorrect result when I set the dd value to 12 or less. The format of the date is mm/dd/yyWriting text from Excel 2007 to a .txt file on a Sharepoint SiteExcel VBA: Dates displaying as text even though date format is setVBA date type mismatchExcel form: Input random date in form comes out all screwyExcel VBA Format(<Date>, “mmmmm-yy”) not working as expectedCorrectly calling PMT function and the correct declaration of valuesPrinting from ppt VBA to an Excel spreadsheetExcel 2007: Editing a date value deletes the date formatSave As to Variable File PathsExcel converting all numeric values in Worksheets to Doubles

What formula to chose a nonlinear formula?

Why would company (decision makers) wait for someone to retire, rather than lay them off, when their role is no longer needed?

What color to choose as "danger" if the main color of my app is red

Does a non-singular matrix have a large minor with disjoint rows and columns and full rank?

Why do academics prefer Mac/Linux?

Why does the U.S military use mercenaries?

Why is so much ransomware breakable?

How could it be that 80% of townspeople were farmers during the Edo period in Japan?

Rushed passport - does my reason qualify?

Is it possible to pass a pointer to an operator as an argument like a pointer to a function?

Why did nobody know who the Lord of this region was?

How can I fix the label locations on my tikzcd diagram?

Given 0s on Assignments with suspected and dismissed cheating?

FIFO data structure in pure C

Why can't I share a one use code with anyone else?

What technology would Dwarves need to forge titanium?

Divisor Rich and Poor Numbers

What are the effects of eating many berries from the Goodberry spell per day?

Cannot remove door knob -- totally inaccessible!

Is there an academic word that means "to split hairs over"?

Is Precocious Apprentice enough for Mystic Theurge?

Why use a retrograde orbit?

Usage of the relative pronoun "dont"

Do high-wing aircraft represent more difficult engineering challenges than low-wing aircraft?



DateDiff produces incorrect result when I set the dd value to 12 or less. The format of the date is mm/dd/yy


Writing text from Excel 2007 to a .txt file on a Sharepoint SiteExcel VBA: Dates displaying as text even though date format is setVBA date type mismatchExcel form: Input random date in form comes out all screwyExcel VBA Format(<Date>, “mmmmm-yy”) not working as expectedCorrectly calling PMT function and the correct declaration of valuesPrinting from ppt VBA to an Excel spreadsheetExcel 2007: Editing a date value deletes the date formatSave As to Variable File PathsExcel converting all numeric values in Worksheets to Doubles






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








0















If the "dd" value of the date is "12" or less, on the StartDate variable, then the answer is incorrect.



Eg. When the StartDate is "03/12/19" and the EndDate value is "03/23/19", it yields -5573, which is obviously the incorrect answer.



If the "dd" value of the date is "13" or more, on the StartDate variable, then the answer is correct.



Eg When the StartDate is "03/13/19" and the EndDate value is "03/23/19", it yields 10, which is the correct answer.



I don't quite understand.
BTW the vba code is on a MacOS Excel Mac 2011.



Also, the weird thing is that this exact same workbook code works perfectly on my Win 7 PC Excel 2007 regardless of what value of the "dd" you use.!!!!



Sub DateDiffTest()

Dim StartDate As Date ' Declare variables.
Dim EndDate As Date ' Declare variables.
Dim Msg
StartDate = Format("03/12/19", "mm-dd-yy")
EndDate = Format("03/23/19", "mm-dd-yy")
Msg = "Days from today: " & DateDiff("d", EndDate, StartDate)
MsgBox Msg
End Sub









share|improve this question
























  • Please take any cell on your excel sheet and put now() and see in what format date is coming ie mm/dd/yyyy or dd/mm/yyyy or dd-mm-yyyy etc . Same format fo date should be used in your code and format pattern. However your code interprets correct values in my Excel 2016 Windows 10 as locals date setting here is dd-mm-yyyy format.

    – skkakkar
    Mar 23 at 16:52












  • I'm not using either dates on my spreadsheet. I use the result to determine what action I will take further on in my macro (which I am not showing in this example). I'm actually just using the sample code to illustrate the problem with not coming up with the right result depending on what "dd" value I use in the DateDiff function. Even using the Format function doesn't seem to make a difference. This is only a snippet of a larger macro that works perfectly on my Win 7 Excel 2007 to highlight the problem I get when I use a "dd" value less than thirteen. Weird!!!

    – Grant Fleury
    Mar 23 at 19:19












  • Essentially, the DateDiff works perfectly on my Win 7 PC Excel 2007 with any value in the "dd" position and only if the value of "dd" is 13 or greater on my Mac Excel 2011.

    – Grant Fleury
    Mar 23 at 19:23











  • Dates are entered as date literals to remove locale from coercion. #12/6/2019#. means 6th December. msgbox FormatDateTime(#12/6/2019#, 1). This is well known and nothing weird about it. This is the underlying COM function that does it docs.microsoft.com/en-us/windows/desktop/api/oleauto/…

    – Noodles
    Mar 24 at 0:54











  • All source code is US English. When you put a date into a string it's not source code and will be interpreted according to the locale.

    – Noodles
    Mar 24 at 0:57

















0















If the "dd" value of the date is "12" or less, on the StartDate variable, then the answer is incorrect.



Eg. When the StartDate is "03/12/19" and the EndDate value is "03/23/19", it yields -5573, which is obviously the incorrect answer.



If the "dd" value of the date is "13" or more, on the StartDate variable, then the answer is correct.



Eg When the StartDate is "03/13/19" and the EndDate value is "03/23/19", it yields 10, which is the correct answer.



I don't quite understand.
BTW the vba code is on a MacOS Excel Mac 2011.



Also, the weird thing is that this exact same workbook code works perfectly on my Win 7 PC Excel 2007 regardless of what value of the "dd" you use.!!!!



Sub DateDiffTest()

Dim StartDate As Date ' Declare variables.
Dim EndDate As Date ' Declare variables.
Dim Msg
StartDate = Format("03/12/19", "mm-dd-yy")
EndDate = Format("03/23/19", "mm-dd-yy")
Msg = "Days from today: " & DateDiff("d", EndDate, StartDate)
MsgBox Msg
End Sub









share|improve this question
























  • Please take any cell on your excel sheet and put now() and see in what format date is coming ie mm/dd/yyyy or dd/mm/yyyy or dd-mm-yyyy etc . Same format fo date should be used in your code and format pattern. However your code interprets correct values in my Excel 2016 Windows 10 as locals date setting here is dd-mm-yyyy format.

    – skkakkar
    Mar 23 at 16:52












  • I'm not using either dates on my spreadsheet. I use the result to determine what action I will take further on in my macro (which I am not showing in this example). I'm actually just using the sample code to illustrate the problem with not coming up with the right result depending on what "dd" value I use in the DateDiff function. Even using the Format function doesn't seem to make a difference. This is only a snippet of a larger macro that works perfectly on my Win 7 Excel 2007 to highlight the problem I get when I use a "dd" value less than thirteen. Weird!!!

    – Grant Fleury
    Mar 23 at 19:19












  • Essentially, the DateDiff works perfectly on my Win 7 PC Excel 2007 with any value in the "dd" position and only if the value of "dd" is 13 or greater on my Mac Excel 2011.

    – Grant Fleury
    Mar 23 at 19:23











  • Dates are entered as date literals to remove locale from coercion. #12/6/2019#. means 6th December. msgbox FormatDateTime(#12/6/2019#, 1). This is well known and nothing weird about it. This is the underlying COM function that does it docs.microsoft.com/en-us/windows/desktop/api/oleauto/…

    – Noodles
    Mar 24 at 0:54











  • All source code is US English. When you put a date into a string it's not source code and will be interpreted according to the locale.

    – Noodles
    Mar 24 at 0:57













0












0








0








If the "dd" value of the date is "12" or less, on the StartDate variable, then the answer is incorrect.



Eg. When the StartDate is "03/12/19" and the EndDate value is "03/23/19", it yields -5573, which is obviously the incorrect answer.



If the "dd" value of the date is "13" or more, on the StartDate variable, then the answer is correct.



Eg When the StartDate is "03/13/19" and the EndDate value is "03/23/19", it yields 10, which is the correct answer.



I don't quite understand.
BTW the vba code is on a MacOS Excel Mac 2011.



Also, the weird thing is that this exact same workbook code works perfectly on my Win 7 PC Excel 2007 regardless of what value of the "dd" you use.!!!!



Sub DateDiffTest()

Dim StartDate As Date ' Declare variables.
Dim EndDate As Date ' Declare variables.
Dim Msg
StartDate = Format("03/12/19", "mm-dd-yy")
EndDate = Format("03/23/19", "mm-dd-yy")
Msg = "Days from today: " & DateDiff("d", EndDate, StartDate)
MsgBox Msg
End Sub









share|improve this question
















If the "dd" value of the date is "12" or less, on the StartDate variable, then the answer is incorrect.



Eg. When the StartDate is "03/12/19" and the EndDate value is "03/23/19", it yields -5573, which is obviously the incorrect answer.



If the "dd" value of the date is "13" or more, on the StartDate variable, then the answer is correct.



Eg When the StartDate is "03/13/19" and the EndDate value is "03/23/19", it yields 10, which is the correct answer.



I don't quite understand.
BTW the vba code is on a MacOS Excel Mac 2011.



Also, the weird thing is that this exact same workbook code works perfectly on my Win 7 PC Excel 2007 regardless of what value of the "dd" you use.!!!!



Sub DateDiffTest()

Dim StartDate As Date ' Declare variables.
Dim EndDate As Date ' Declare variables.
Dim Msg
StartDate = Format("03/12/19", "mm-dd-yy")
EndDate = Format("03/23/19", "mm-dd-yy")
Msg = "Days from today: " & DateDiff("d", EndDate, StartDate)
MsgBox Msg
End Sub






excel vba






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 25 at 7:24









Pᴇʜ

26.5k63052




26.5k63052










asked Mar 23 at 16:34









Grant FleuryGrant Fleury

32




32












  • Please take any cell on your excel sheet and put now() and see in what format date is coming ie mm/dd/yyyy or dd/mm/yyyy or dd-mm-yyyy etc . Same format fo date should be used in your code and format pattern. However your code interprets correct values in my Excel 2016 Windows 10 as locals date setting here is dd-mm-yyyy format.

    – skkakkar
    Mar 23 at 16:52












  • I'm not using either dates on my spreadsheet. I use the result to determine what action I will take further on in my macro (which I am not showing in this example). I'm actually just using the sample code to illustrate the problem with not coming up with the right result depending on what "dd" value I use in the DateDiff function. Even using the Format function doesn't seem to make a difference. This is only a snippet of a larger macro that works perfectly on my Win 7 Excel 2007 to highlight the problem I get when I use a "dd" value less than thirteen. Weird!!!

    – Grant Fleury
    Mar 23 at 19:19












  • Essentially, the DateDiff works perfectly on my Win 7 PC Excel 2007 with any value in the "dd" position and only if the value of "dd" is 13 or greater on my Mac Excel 2011.

    – Grant Fleury
    Mar 23 at 19:23











  • Dates are entered as date literals to remove locale from coercion. #12/6/2019#. means 6th December. msgbox FormatDateTime(#12/6/2019#, 1). This is well known and nothing weird about it. This is the underlying COM function that does it docs.microsoft.com/en-us/windows/desktop/api/oleauto/…

    – Noodles
    Mar 24 at 0:54











  • All source code is US English. When you put a date into a string it's not source code and will be interpreted according to the locale.

    – Noodles
    Mar 24 at 0:57

















  • Please take any cell on your excel sheet and put now() and see in what format date is coming ie mm/dd/yyyy or dd/mm/yyyy or dd-mm-yyyy etc . Same format fo date should be used in your code and format pattern. However your code interprets correct values in my Excel 2016 Windows 10 as locals date setting here is dd-mm-yyyy format.

    – skkakkar
    Mar 23 at 16:52












  • I'm not using either dates on my spreadsheet. I use the result to determine what action I will take further on in my macro (which I am not showing in this example). I'm actually just using the sample code to illustrate the problem with not coming up with the right result depending on what "dd" value I use in the DateDiff function. Even using the Format function doesn't seem to make a difference. This is only a snippet of a larger macro that works perfectly on my Win 7 Excel 2007 to highlight the problem I get when I use a "dd" value less than thirteen. Weird!!!

    – Grant Fleury
    Mar 23 at 19:19












  • Essentially, the DateDiff works perfectly on my Win 7 PC Excel 2007 with any value in the "dd" position and only if the value of "dd" is 13 or greater on my Mac Excel 2011.

    – Grant Fleury
    Mar 23 at 19:23











  • Dates are entered as date literals to remove locale from coercion. #12/6/2019#. means 6th December. msgbox FormatDateTime(#12/6/2019#, 1). This is well known and nothing weird about it. This is the underlying COM function that does it docs.microsoft.com/en-us/windows/desktop/api/oleauto/…

    – Noodles
    Mar 24 at 0:54











  • All source code is US English. When you put a date into a string it's not source code and will be interpreted according to the locale.

    – Noodles
    Mar 24 at 0:57
















Please take any cell on your excel sheet and put now() and see in what format date is coming ie mm/dd/yyyy or dd/mm/yyyy or dd-mm-yyyy etc . Same format fo date should be used in your code and format pattern. However your code interprets correct values in my Excel 2016 Windows 10 as locals date setting here is dd-mm-yyyy format.

– skkakkar
Mar 23 at 16:52






Please take any cell on your excel sheet and put now() and see in what format date is coming ie mm/dd/yyyy or dd/mm/yyyy or dd-mm-yyyy etc . Same format fo date should be used in your code and format pattern. However your code interprets correct values in my Excel 2016 Windows 10 as locals date setting here is dd-mm-yyyy format.

– skkakkar
Mar 23 at 16:52














I'm not using either dates on my spreadsheet. I use the result to determine what action I will take further on in my macro (which I am not showing in this example). I'm actually just using the sample code to illustrate the problem with not coming up with the right result depending on what "dd" value I use in the DateDiff function. Even using the Format function doesn't seem to make a difference. This is only a snippet of a larger macro that works perfectly on my Win 7 Excel 2007 to highlight the problem I get when I use a "dd" value less than thirteen. Weird!!!

– Grant Fleury
Mar 23 at 19:19






I'm not using either dates on my spreadsheet. I use the result to determine what action I will take further on in my macro (which I am not showing in this example). I'm actually just using the sample code to illustrate the problem with not coming up with the right result depending on what "dd" value I use in the DateDiff function. Even using the Format function doesn't seem to make a difference. This is only a snippet of a larger macro that works perfectly on my Win 7 Excel 2007 to highlight the problem I get when I use a "dd" value less than thirteen. Weird!!!

– Grant Fleury
Mar 23 at 19:19














Essentially, the DateDiff works perfectly on my Win 7 PC Excel 2007 with any value in the "dd" position and only if the value of "dd" is 13 or greater on my Mac Excel 2011.

– Grant Fleury
Mar 23 at 19:23





Essentially, the DateDiff works perfectly on my Win 7 PC Excel 2007 with any value in the "dd" position and only if the value of "dd" is 13 or greater on my Mac Excel 2011.

– Grant Fleury
Mar 23 at 19:23













Dates are entered as date literals to remove locale from coercion. #12/6/2019#. means 6th December. msgbox FormatDateTime(#12/6/2019#, 1). This is well known and nothing weird about it. This is the underlying COM function that does it docs.microsoft.com/en-us/windows/desktop/api/oleauto/…

– Noodles
Mar 24 at 0:54





Dates are entered as date literals to remove locale from coercion. #12/6/2019#. means 6th December. msgbox FormatDateTime(#12/6/2019#, 1). This is well known and nothing weird about it. This is the underlying COM function that does it docs.microsoft.com/en-us/windows/desktop/api/oleauto/…

– Noodles
Mar 24 at 0:54













All source code is US English. When you put a date into a string it's not source code and will be interpreted according to the locale.

– Noodles
Mar 24 at 0:57





All source code is US English. When you put a date into a string it's not source code and will be interpreted according to the locale.

– Noodles
Mar 24 at 0:57












1 Answer
1






active

oldest

votes


















1














Your problems have to do with undeclared variables, and with VBA being US-Centric in its use of dates and with your Regional settings being inconsistent with what you are doing.



I suspect your Regional short date settings are YMD.



And what you think is dd, VBA thinks is mm.



So when the value is <= 12 it converts it to YMD
StartDate --> 2003-Dec-19



When it is >12, it cannot be a month, so VBA interprets it as a day, and converts it as a US-Centric date. So EndDate --> 2019-Mar-23



You should declare all variables, and also, when coding, use unambiguous dates.



eg:



Option Explicit
Sub DateDiffTest()
'OK to use US-centric MDY here
Const StartDate As Date = #3/12/2019#
Const EndDate As Date = #3/23/2019#
Dim Msg As String

Msg = "Days from today: " & DateDiff("d", EndDate, StartDate)
MsgBox Msg
End Sub


Another method of unambiguously declaring the dates:



Option Explicit
Sub DateDiffTest()
Dim StartDate As Date
Dim EndDate As Date
Dim Msg As String

StartDate = "2019-Mar-12"
EndDate = "2019-Mar-23"

Msg = "Days from today: " & DateDiff("d", EndDate, StartDate)
MsgBox Msg
End Sub


By the way, I don't understand why MS makes NOT requiring variable declaration the default. Select Tools/Options/Editor and check Require Variable Declaration. This will place Option Explicit at the start of any new module. To correct this module, enter it manually at the beginning.






share|improve this answer

























  • Thank you for your excellent insight. However, the StartDate variable is getting it's value from a file that I read in that is in the format "mm/dd/yy". Therefore I don't have any way of changing that value into the format that you are suggesting. Or is there any easy little piece of code that you can suggest to convert it into the format you use in your example #3/12/2019#.

    – Grant Fleury
    Mar 23 at 19:59












  • It depends on how you are reading in the file, type of file, etc. That's a different issue, but usually easily solved.

    – Ron Rosenfeld
    Mar 23 at 20:09











  • Thank you Ron Rosenfeld. I did however look at my short date settings on my Mac and changed them to match the format of the dates I'm reading in, and it now seems to work correctly! Thank you so much for that excellent suggestion!!

    – Grant Fleury
    Mar 23 at 20:43












  • @GrantFleury Usually, if you Import (instead of opening) a text/csv file, you can specify the incoming date format, and Excel will then properly interpret the dates. By the way, If my answer satisfies your problem, I would appreciate it if you could mark it as accepted. You can read What should I do when someone answers my question for more information.

    – Ron Rosenfeld
    Mar 24 at 11:02











  • If you A = split("12/31/2019", "/") you will get a three element array with x(0) = 12. y=CDate(x(1) & "/" & x(0) & "/" & x(2))

    – Noodles
    Mar 24 at 21:30












Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55315940%2fdatediff-produces-incorrect-result-when-i-set-the-dd-value-to-12-or-less-the-fo%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














Your problems have to do with undeclared variables, and with VBA being US-Centric in its use of dates and with your Regional settings being inconsistent with what you are doing.



I suspect your Regional short date settings are YMD.



And what you think is dd, VBA thinks is mm.



So when the value is <= 12 it converts it to YMD
StartDate --> 2003-Dec-19



When it is >12, it cannot be a month, so VBA interprets it as a day, and converts it as a US-Centric date. So EndDate --> 2019-Mar-23



You should declare all variables, and also, when coding, use unambiguous dates.



eg:



Option Explicit
Sub DateDiffTest()
'OK to use US-centric MDY here
Const StartDate As Date = #3/12/2019#
Const EndDate As Date = #3/23/2019#
Dim Msg As String

Msg = "Days from today: " & DateDiff("d", EndDate, StartDate)
MsgBox Msg
End Sub


Another method of unambiguously declaring the dates:



Option Explicit
Sub DateDiffTest()
Dim StartDate As Date
Dim EndDate As Date
Dim Msg As String

StartDate = "2019-Mar-12"
EndDate = "2019-Mar-23"

Msg = "Days from today: " & DateDiff("d", EndDate, StartDate)
MsgBox Msg
End Sub


By the way, I don't understand why MS makes NOT requiring variable declaration the default. Select Tools/Options/Editor and check Require Variable Declaration. This will place Option Explicit at the start of any new module. To correct this module, enter it manually at the beginning.






share|improve this answer

























  • Thank you for your excellent insight. However, the StartDate variable is getting it's value from a file that I read in that is in the format "mm/dd/yy". Therefore I don't have any way of changing that value into the format that you are suggesting. Or is there any easy little piece of code that you can suggest to convert it into the format you use in your example #3/12/2019#.

    – Grant Fleury
    Mar 23 at 19:59












  • It depends on how you are reading in the file, type of file, etc. That's a different issue, but usually easily solved.

    – Ron Rosenfeld
    Mar 23 at 20:09











  • Thank you Ron Rosenfeld. I did however look at my short date settings on my Mac and changed them to match the format of the dates I'm reading in, and it now seems to work correctly! Thank you so much for that excellent suggestion!!

    – Grant Fleury
    Mar 23 at 20:43












  • @GrantFleury Usually, if you Import (instead of opening) a text/csv file, you can specify the incoming date format, and Excel will then properly interpret the dates. By the way, If my answer satisfies your problem, I would appreciate it if you could mark it as accepted. You can read What should I do when someone answers my question for more information.

    – Ron Rosenfeld
    Mar 24 at 11:02











  • If you A = split("12/31/2019", "/") you will get a three element array with x(0) = 12. y=CDate(x(1) & "/" & x(0) & "/" & x(2))

    – Noodles
    Mar 24 at 21:30
















1














Your problems have to do with undeclared variables, and with VBA being US-Centric in its use of dates and with your Regional settings being inconsistent with what you are doing.



I suspect your Regional short date settings are YMD.



And what you think is dd, VBA thinks is mm.



So when the value is <= 12 it converts it to YMD
StartDate --> 2003-Dec-19



When it is >12, it cannot be a month, so VBA interprets it as a day, and converts it as a US-Centric date. So EndDate --> 2019-Mar-23



You should declare all variables, and also, when coding, use unambiguous dates.



eg:



Option Explicit
Sub DateDiffTest()
'OK to use US-centric MDY here
Const StartDate As Date = #3/12/2019#
Const EndDate As Date = #3/23/2019#
Dim Msg As String

Msg = "Days from today: " & DateDiff("d", EndDate, StartDate)
MsgBox Msg
End Sub


Another method of unambiguously declaring the dates:



Option Explicit
Sub DateDiffTest()
Dim StartDate As Date
Dim EndDate As Date
Dim Msg As String

StartDate = "2019-Mar-12"
EndDate = "2019-Mar-23"

Msg = "Days from today: " & DateDiff("d", EndDate, StartDate)
MsgBox Msg
End Sub


By the way, I don't understand why MS makes NOT requiring variable declaration the default. Select Tools/Options/Editor and check Require Variable Declaration. This will place Option Explicit at the start of any new module. To correct this module, enter it manually at the beginning.






share|improve this answer

























  • Thank you for your excellent insight. However, the StartDate variable is getting it's value from a file that I read in that is in the format "mm/dd/yy". Therefore I don't have any way of changing that value into the format that you are suggesting. Or is there any easy little piece of code that you can suggest to convert it into the format you use in your example #3/12/2019#.

    – Grant Fleury
    Mar 23 at 19:59












  • It depends on how you are reading in the file, type of file, etc. That's a different issue, but usually easily solved.

    – Ron Rosenfeld
    Mar 23 at 20:09











  • Thank you Ron Rosenfeld. I did however look at my short date settings on my Mac and changed them to match the format of the dates I'm reading in, and it now seems to work correctly! Thank you so much for that excellent suggestion!!

    – Grant Fleury
    Mar 23 at 20:43












  • @GrantFleury Usually, if you Import (instead of opening) a text/csv file, you can specify the incoming date format, and Excel will then properly interpret the dates. By the way, If my answer satisfies your problem, I would appreciate it if you could mark it as accepted. You can read What should I do when someone answers my question for more information.

    – Ron Rosenfeld
    Mar 24 at 11:02











  • If you A = split("12/31/2019", "/") you will get a three element array with x(0) = 12. y=CDate(x(1) & "/" & x(0) & "/" & x(2))

    – Noodles
    Mar 24 at 21:30














1












1








1







Your problems have to do with undeclared variables, and with VBA being US-Centric in its use of dates and with your Regional settings being inconsistent with what you are doing.



I suspect your Regional short date settings are YMD.



And what you think is dd, VBA thinks is mm.



So when the value is <= 12 it converts it to YMD
StartDate --> 2003-Dec-19



When it is >12, it cannot be a month, so VBA interprets it as a day, and converts it as a US-Centric date. So EndDate --> 2019-Mar-23



You should declare all variables, and also, when coding, use unambiguous dates.



eg:



Option Explicit
Sub DateDiffTest()
'OK to use US-centric MDY here
Const StartDate As Date = #3/12/2019#
Const EndDate As Date = #3/23/2019#
Dim Msg As String

Msg = "Days from today: " & DateDiff("d", EndDate, StartDate)
MsgBox Msg
End Sub


Another method of unambiguously declaring the dates:



Option Explicit
Sub DateDiffTest()
Dim StartDate As Date
Dim EndDate As Date
Dim Msg As String

StartDate = "2019-Mar-12"
EndDate = "2019-Mar-23"

Msg = "Days from today: " & DateDiff("d", EndDate, StartDate)
MsgBox Msg
End Sub


By the way, I don't understand why MS makes NOT requiring variable declaration the default. Select Tools/Options/Editor and check Require Variable Declaration. This will place Option Explicit at the start of any new module. To correct this module, enter it manually at the beginning.






share|improve this answer















Your problems have to do with undeclared variables, and with VBA being US-Centric in its use of dates and with your Regional settings being inconsistent with what you are doing.



I suspect your Regional short date settings are YMD.



And what you think is dd, VBA thinks is mm.



So when the value is <= 12 it converts it to YMD
StartDate --> 2003-Dec-19



When it is >12, it cannot be a month, so VBA interprets it as a day, and converts it as a US-Centric date. So EndDate --> 2019-Mar-23



You should declare all variables, and also, when coding, use unambiguous dates.



eg:



Option Explicit
Sub DateDiffTest()
'OK to use US-centric MDY here
Const StartDate As Date = #3/12/2019#
Const EndDate As Date = #3/23/2019#
Dim Msg As String

Msg = "Days from today: " & DateDiff("d", EndDate, StartDate)
MsgBox Msg
End Sub


Another method of unambiguously declaring the dates:



Option Explicit
Sub DateDiffTest()
Dim StartDate As Date
Dim EndDate As Date
Dim Msg As String

StartDate = "2019-Mar-12"
EndDate = "2019-Mar-23"

Msg = "Days from today: " & DateDiff("d", EndDate, StartDate)
MsgBox Msg
End Sub


By the way, I don't understand why MS makes NOT requiring variable declaration the default. Select Tools/Options/Editor and check Require Variable Declaration. This will place Option Explicit at the start of any new module. To correct this module, enter it manually at the beginning.







share|improve this answer














share|improve this answer



share|improve this answer








edited Mar 23 at 19:32

























answered Mar 23 at 19:26









Ron RosenfeldRon Rosenfeld

24.7k41641




24.7k41641












  • Thank you for your excellent insight. However, the StartDate variable is getting it's value from a file that I read in that is in the format "mm/dd/yy". Therefore I don't have any way of changing that value into the format that you are suggesting. Or is there any easy little piece of code that you can suggest to convert it into the format you use in your example #3/12/2019#.

    – Grant Fleury
    Mar 23 at 19:59












  • It depends on how you are reading in the file, type of file, etc. That's a different issue, but usually easily solved.

    – Ron Rosenfeld
    Mar 23 at 20:09











  • Thank you Ron Rosenfeld. I did however look at my short date settings on my Mac and changed them to match the format of the dates I'm reading in, and it now seems to work correctly! Thank you so much for that excellent suggestion!!

    – Grant Fleury
    Mar 23 at 20:43












  • @GrantFleury Usually, if you Import (instead of opening) a text/csv file, you can specify the incoming date format, and Excel will then properly interpret the dates. By the way, If my answer satisfies your problem, I would appreciate it if you could mark it as accepted. You can read What should I do when someone answers my question for more information.

    – Ron Rosenfeld
    Mar 24 at 11:02











  • If you A = split("12/31/2019", "/") you will get a three element array with x(0) = 12. y=CDate(x(1) & "/" & x(0) & "/" & x(2))

    – Noodles
    Mar 24 at 21:30


















  • Thank you for your excellent insight. However, the StartDate variable is getting it's value from a file that I read in that is in the format "mm/dd/yy". Therefore I don't have any way of changing that value into the format that you are suggesting. Or is there any easy little piece of code that you can suggest to convert it into the format you use in your example #3/12/2019#.

    – Grant Fleury
    Mar 23 at 19:59












  • It depends on how you are reading in the file, type of file, etc. That's a different issue, but usually easily solved.

    – Ron Rosenfeld
    Mar 23 at 20:09











  • Thank you Ron Rosenfeld. I did however look at my short date settings on my Mac and changed them to match the format of the dates I'm reading in, and it now seems to work correctly! Thank you so much for that excellent suggestion!!

    – Grant Fleury
    Mar 23 at 20:43












  • @GrantFleury Usually, if you Import (instead of opening) a text/csv file, you can specify the incoming date format, and Excel will then properly interpret the dates. By the way, If my answer satisfies your problem, I would appreciate it if you could mark it as accepted. You can read What should I do when someone answers my question for more information.

    – Ron Rosenfeld
    Mar 24 at 11:02











  • If you A = split("12/31/2019", "/") you will get a three element array with x(0) = 12. y=CDate(x(1) & "/" & x(0) & "/" & x(2))

    – Noodles
    Mar 24 at 21:30

















Thank you for your excellent insight. However, the StartDate variable is getting it's value from a file that I read in that is in the format "mm/dd/yy". Therefore I don't have any way of changing that value into the format that you are suggesting. Or is there any easy little piece of code that you can suggest to convert it into the format you use in your example #3/12/2019#.

– Grant Fleury
Mar 23 at 19:59






Thank you for your excellent insight. However, the StartDate variable is getting it's value from a file that I read in that is in the format "mm/dd/yy". Therefore I don't have any way of changing that value into the format that you are suggesting. Or is there any easy little piece of code that you can suggest to convert it into the format you use in your example #3/12/2019#.

– Grant Fleury
Mar 23 at 19:59














It depends on how you are reading in the file, type of file, etc. That's a different issue, but usually easily solved.

– Ron Rosenfeld
Mar 23 at 20:09





It depends on how you are reading in the file, type of file, etc. That's a different issue, but usually easily solved.

– Ron Rosenfeld
Mar 23 at 20:09













Thank you Ron Rosenfeld. I did however look at my short date settings on my Mac and changed them to match the format of the dates I'm reading in, and it now seems to work correctly! Thank you so much for that excellent suggestion!!

– Grant Fleury
Mar 23 at 20:43






Thank you Ron Rosenfeld. I did however look at my short date settings on my Mac and changed them to match the format of the dates I'm reading in, and it now seems to work correctly! Thank you so much for that excellent suggestion!!

– Grant Fleury
Mar 23 at 20:43














@GrantFleury Usually, if you Import (instead of opening) a text/csv file, you can specify the incoming date format, and Excel will then properly interpret the dates. By the way, If my answer satisfies your problem, I would appreciate it if you could mark it as accepted. You can read What should I do when someone answers my question for more information.

– Ron Rosenfeld
Mar 24 at 11:02





@GrantFleury Usually, if you Import (instead of opening) a text/csv file, you can specify the incoming date format, and Excel will then properly interpret the dates. By the way, If my answer satisfies your problem, I would appreciate it if you could mark it as accepted. You can read What should I do when someone answers my question for more information.

– Ron Rosenfeld
Mar 24 at 11:02













If you A = split("12/31/2019", "/") you will get a three element array with x(0) = 12. y=CDate(x(1) & "/" & x(0) & "/" & x(2))

– Noodles
Mar 24 at 21:30






If you A = split("12/31/2019", "/") you will get a three element array with x(0) = 12. y=CDate(x(1) & "/" & x(0) & "/" & x(2))

– Noodles
Mar 24 at 21:30




















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55315940%2fdatediff-produces-incorrect-result-when-i-set-the-dd-value-to-12-or-less-the-fo%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript