Log in

View Full Version : Some Date Notes and Tests



DocAElstein
11-23-2021, 10:40 PM
Some date notes related to this thread
https://eileenslounge.com/viewtopic.php?f=30&t=37384
These notes here are also in preparation for a possible detailed Blog on VBA dates


Sub MessinMitdates() ( see here : )
A macro messing about with date functions, including to get at the implied system short date format, sShortDate
Rem 1 sShortDate ( https://i.postimg.cc/x8mr56rZ/System-short-date-format.jpg https://i.postimg.cc/bYSjqwGR/System-short-date-format.jpg )
Use of DateSerial( year , month , day ) function
Crude implifiedcation technique
The arguments that this takes are quite clear. It is less obvious is that returns a date in the system short date. The first thing I do in the macro is give the function these arguments , DateSerial(9, 3, 4)
I am hoping that the given output can be similarly interpreted in different excel versions. The number I chose are based on some experimenting.
There are ' explaining notes in the macro and some more here in particular to document important variables which can be used to build the string up again later.
I don’t expect the macro to work in all circumstances. For example its assuming you use sensible numbers in a typical short date format.
The string that is given back by DateSerial(9, 3, 4) , ( in variable TestDateSerial ) , is stepped through. The position of changes in type is used to determine lengths of the numbers so we can represent them in the typical notation, - for example dd represents that a day may be shown from 01 to 31 , whereas d would mean you want to show days from 1 to 31 , so that you show double number characters only when you have to from 10 to 31
The month numbers will be handled in a similar way. The macro would catch if you use y yy yyy or yyyy , but I am expecting yy or yyyy - I have not experimented thoroughly what may result from a y or yyy
Purpose of the macro is to determine the number of characters that have been chosen for the three quantities, day , month and year , given in numbers, and also to give the actual characters chosen for the two separation bits. Typically these separation bits would be the same and would be something like a single one of these . , : / - In other words I am looking to give an output of this type of form dd/mm/yyyy or mm-d-yyyy etc.. etc… , but my macro would also catch a form like yyyyCrapddSwillmm
( You cannot use any possible character combination for the separators as there may be conflicts with character used for specific things, such as the d m and y , but it seems you can use quite a few – that yyyyCrapddSwillmm for example is perfectly valid )


Summary of the final string defining variables
Cnt1 – character count in the first returned number bit
Sep1 – the actual character string of the first separator
Pos2 – The position of the start of the middle number bit
Cnt2 – the character count in the middle number bit
Sep2 – the actual character string of the second separator
Pos3 – The position of the start of the last number bit
Cnt3 - the character count of the last number section


Some ideas and conclusions so far:
DateSerial( year , month , day ) function
Its fairly clear what this takes as arguments in terms of whether it’s a day , month or year. If you use numbers for those arguments , for example, like ( 2021, 1, 2 ) then there is never any ambiguity and it sees the date of 2nd January, 2021. The output within VBA is also fairly predictable. It appears to come out in exactly the form that you specified in your sShortdate in the registry. What gets put in a cell is a different story. The macro gives some examples. It appears to be very unpredictable

/ and similar single characters
The / and possibly to a lesser extent some other characters of the sort often used to separate things, may encourage Excel to think about making something look different in a cell to the form you give it. You need to be careful if you use this in anything related to dates, and using the “escape” character, \ , may help get you out of difficulty ( http://www.eileenslounge.com/viewtopic.php?p=289607#p289607 )

System short date , sShortDate
Possibly by design, or possibly by accident due to the last point, you will always get returned by a macro recording the code line like .._
.NumberFormat = "m/d/yyyy"
-… regardless of what your specified system short date is.
This suggests that .NumberFormat = "m/d/yyyy" is really saying/ meaning use system short date format. The results you get partially support this. But not fully: once again you may gat inconsistent results using it.

Using enclosing hash pair # # , in place of the usual 2 quote , “ “ , pair when giving a value in VBA
Usually in VBA we supply a text string in quotes like, = "My text"
However you can also supply a text string in a pair of #s , but not in an un restricted form. I don’t know yet what exactly all the accepted variations are, but initially it appears that should be in some predominantly number form that Excel may be able to recognise as a date or time. The date that Excel tries to see, would appear to be the English American. So for example, if I try to give this , that is to say I type exactly this .._
= #12___- __ 1_.2021 #
… then VBA will not error but the compiler will immediately change that, as I hit Enter , to
#12/1/2021#
It would appear that this is then taken as 1st December, 2021

DocAElstein
02-25-2025, 11:16 PM
Coding and perhaps some more notes later for this main forum post
https://eileenslounge.com/viewtopic.php?f=27&t=41986

Option Explicit
Sub DatesAgain1() ' https://www.excelfox.com/forum/showthread.php/2772-Some-Date-Notes-and-Tests?p=24985&viewfull=1#post24985
Dim Rng As Range
Set Rng = Range("I1:I4")
Rng.Clear
Rem 1
1 Let Rng.NumberFormat = "dd\/mm\/yyyy" ' https://i.postimg.cc/nz3P5GGF/UCDeF.jpg

Rng.Clear
Rem 2 ' https://i.postimg.cc/mDLn85Mp/Do-Date-manually.jpg
2 Let Rng.NumberFormat = "m/d/yyyy" ' https://i.postimg.cc/fyFgWWP1/Computer-HKEY-CURRENT-USER-Control-Panel-International.jpg
' https://i.postimg.cc/VNBT98Sb/Date-format-appears.jpg
Rng.Clear
Rem 3
Let Rng.NumberFormat = "dd\/mm\/yyyy"
Let Rng.Item(1) = "12/1/2025" ' 01/12/2025
Let Rng.Item(2) = "13/1/2025" ' 13/1/2025
Rem 4
Dim StrDteSer As String
Let StrDteSer = DateSerial(Year:=2025, Month:=1, Day:=13) ' ' https://i.postimg.cc/Gp1Vs2pC/How-VBA-sees-it-Hover.jpg
Let Rng.Item(3) = StrDteSer ' 13/01/2025
Let StrDteSer = DateSerial(Year:=2025, Month:=1, Day:=12)
Let Rng.Item(4) = StrDteSer ' 12/01/2025
' https://i.postimg.cc/6qTmN4Md/All-4-the-same-UCDe-F.jpg
Stop
End Sub

https://www.youtube.com/channel/UCnxwq2aGJRbjOo_MO54oaHA (https://www.youtube.com/channel/UCnxwq2aGJRbjOo_MO54oaHA)
https://eileenslounge.com/viewtopic.php?p=316441#p316441 (https://eileenslounge.com/viewtopic.php?p=316441#p316441)
https://eileenslounge.com/viewtopic.php?p=324736#p324736 (https://eileenslounge.com/viewtopic.php?p=324736#p324736)
https://eileenslounge.com/viewtopic.php?p=324990#p324990 (https://eileenslounge.com/viewtopic.php?p=324990#p324990)
https://eileenslounge.com/viewtopic.php?f=27&t=41937&p=325485#p325485 (https://eileenslounge.com/viewtopic.php?f=27&t=41937&p=325485#p325485)
https://eileenslounge.com/viewtopic.php?p=325609#p325609 (https://eileenslounge.com/viewtopic.php?p=325609#p325609)
https://eileenslounge.com/viewtopic.php?p=325610#p325610 (https://eileenslounge.com/viewtopic.php?p=325610#p325610)
https://www.youtube.com/channel/UCnxwq2aGJRbjOo_MO54oaHA (https://www.youtube.com/channel/UCnxwq2aGJRbjOo_MO54oaHA)

DocAElstein
03-02-2025, 07:29 PM
Some coding and notes for these Posts
https://eileenslounge.com/viewtopic.php?p=325516#p325516



Sub MoreFightingWithDates() ' https://eileenslounge.com/viewtopic.php?p=325516#p325516
Dim Ws2 As Worksheet, Rng As Range: Set Ws2 = Sheet2: Ws2.Activate
' Ws2.Range("B1:B20").Insert Shift:=xlToRight, CopyOrigin:=xlLeft ' https://www.excelfox.com/forum/showthread.php/2221-VBA-Range-Insert-Method-Code-line-makes-a-space-to-put-new-range-in

Rem 0 I routinely check what sShortDate in registry before doing any date experiments.
Let Ws2.Range("B1") = Pubics.GetMySystemsShortDate ' dd.MM.yyyy
Rem 1 Safe - Start with a defined underlining cell type, and use = DateSerial(Year:=2025, Month:=2, Day:=28)
Ws2.Range("B2").Clear
Let Ws2.Range("B2").NumberFormat = "m/d/yyyy": Let Ws2.Range("B2").Interior.ColorIndex = 20
Let Ws2.Range("B2") = DateSerial(Year:=2025, Month:=2, Day:=28)

Rem 2 Look in variables after they are given an unambiguous date
Dim vTemp As Variant, dTemp As Date, sTemp As String, lTemp As Long
Let vTemp = DateSerial(Year:=2025, Month:=2, Day:=28) ' 28.02.2025
Debug.Print vTemp
Let dTemp = DateSerial(Year:=2025, Month:=2, Day:=28) ' 28.02.2025
Debug.Print dTemp
Let sTemp = DateSerial(Year:=2025, Month:=2, Day:=28) ' Hmm.... Immediate window 28.02.2025 , Hover "28.02.2025"
Debug.Print sTemp
Let lTemp = DateSerial(Year:=2025, Month:=2, Day:=28) ' 45716
Debug.Print lTemp
'_2b) Put in cells with undelying cell format of Text
Ws2.Range("B3:B7").Clear
Let Ws2.Range("B3:B7").NumberFormat = "@": Let Ws2.Range("B3:B7").Interior.ColorIndex = 15
Let Ws2.Range("B3") = vTemp ' In cell 2/28/2025
Let Ws2.Range("B4") = dTemp ' In cell 2/28/2025
Let Ws2.Range("B5") = sTemp ' In cell 28.02.2025
Let Ws2.Range("B6") = lTemp ' In cell 45716
Let Ws2.Range("B7") = DateSerial(Year:=2025, Month:=2, Day:=28) ' In cell 2/28/2025

'_2c) Put in cells with undelying cell format of General
Ws2.Range("B13:B17").Clear
Let Ws2.Range("B13") = vTemp ' In cell 28.02.2025
Let Ws2.Range("B14") = dTemp ' In cell 28.02.2025
Let Ws2.Range("B15") = sTemp ' In cell 28.02.2025
Let Ws2.Range("B16") = lTemp ' In cell 45716
Let Ws2.Range("B17") = DateSerial(Year:=2025, Month:=2, Day:=28) ' In cell 28.02.2025

'_-

Rem 3 Make a copy of the results to the right, in Text so that it stays as it is, and use normal cell formatting to try and represent close to what it actually looked like
Ws2.Range("C1:C20").Insert Shift:=xlToRight, CopyOrigin:=xlLeft
Let Ws2.Range("C1:C20").NumberFormat = "@"
For Each Rng In Ws2.Range("B1:B17")
If Rng.NumberFormat = "m/d/yyyy" Then
Let Rng.Resize(1, 2).Interior.ColorIndex = 20 ' light blue to indicate underlying Date type
Let Rng.Offset(0, 1).Value2 = Rng.Text
Let Rng.Offset(0, 1).HorizontalAlignment = xlRight
ElseIf Rng.NumberFormat = "@" Then
Let Rng.Resize(1, 2).Interior.ColorIndex = 15 ' light grey to indicate underlying Text type
Let Rng.Offset(0, 1).Value2 = Rng.Text
If Not Rng.Offset(0, 1) = "" And IsNumeric(Rng.Offset(0, 1)) Then Let Rng.Offset(0, 1) = 1 * Rng.Offset(0, 1).Value
Let Rng.Offset(0, 1).HorizontalAlignment = xlLeft
ElseIf Rng.NumberFormat = "General" Then
If IsNumeric(Rng.Value2) And InStr(1, Rng.Value2, ",", vbBinaryCompare) = 0 And InStr(1, Rng.Value2, ".", vbBinaryCompare) = 0 Then ' Text put into a cell is aligned to the left border of the cell while numbers are aligned to the right
Let Rng.Offset(0, 1).Value2 = Rng.Text
If Not Rng.Offset(0, 1) = "" Then Let Rng.Offset(0, 1) = 1 * Rng.Offset(0, 1).Value
Let Rng.Offset(0, 1).HorizontalAlignment = xlRight '
Else
Let Rng.Offset(0, 1).Value2 = Rng.Text
Let Rng.Offset(0, 1).HorizontalAlignment = xlLeft
End If
Else
Let Rng.Offset(0, 1).Value2 = Rng.Text
End If
Next Rng

Stop ' This will help by debuging to hover over or ? Debug.Print in the Ctrl+G Immediate window, as well as causing the VB Editor to open automatically.
End Sub

A Function needed is in the next post

DocAElstein
03-02-2025, 07:38 PM
Function required in coding in last posts.


Option Explicit
'Public Sub Get2ShortDate()
Public Function GetMySystemsShortDate() As String
Rem 1 Attempt to get the sShortDate from registry via DateSerial( ) implification way
On Error GoTo Bed
Dim TestDateSerial As String
Let TestDateSerial = DateSerial(9, 3, 4) ' DateSerial(year, month, day) https://docs.microsoft.com/en-us/office/vba/language/reference/user-interface-help/dateserial-function
' By experimenting, it appears that for the day and month, if the sShortDate in the registry needs more than one character then a zero is added to that retuned by DateSerial(9, 3, 4) For the year any missing characters seem to be relplaced by a 0 2 or 1 - For this reason I don't use 1 or 2 for the day or month becuse if i do , there may be some wrong determination below: We use the number to determine if we have a day or month or year
If InStr(1, TestDateSerial, "9", vbBinaryCompare) = 0 Then MsgBox Prompt:="You have no Year in your short date format": Exit Function
If InStr(1, TestDateSerial, "3", vbBinaryCompare) = 0 Then MsgBox Prompt:="You have no Month number in your short date format": Exit Function
If InStr(1, TestDateSerial, "4", vbBinaryCompare) = 0 Then MsgBox Prompt:="You have no Day number in your short date format": Exit Function
Dim C As Variant ' This is each character. It can be a number or letter, so I used Variant but string would work also
Dim Cnt As Long: Let Cnt = 1
Let C = Mid(TestDateSerial, Cnt, 1) ' ========= start of returned string
Do While IsNumeric(C) = True
Let Cnt = Cnt + 1 ' we are counting through the characters, (numbers), from the start, while we have a number. So we are counting the first number section
Let C = Mid(TestDateSerial, Cnt, 1)
Loop ' While IsNumeric(C) = True
Dim Cnt1 As Long: Let Cnt1 = Cnt - 1 ' this will be the count of the characters ( numbers ) in the first number section
' =============================================
Dim Sep1 As String
Do While IsNumeric(C) = False
Let Sep1 = Sep1 & C ' In the first seperator
Let Cnt = Cnt + 1 ' we are counting through the character in the thing used as the first seperator. (These should not be numbers)
Let C = Mid(TestDateSerial, Cnt, 1)
Loop ' While IsNumeric(C) = False
Dim DMY As String ' At this point we have reached the end of the first seperator,
' the next lines search the numbers in the first number section to determine if they are a day or month or year
If InStr(1, Left(TestDateSerial, Cnt - 1), "4", vbBinaryCompare) <> 0 Then: Let DMY = "d" ' we are actually looking at the first number and first seperator, but never ming
If InStr(1, Left(TestDateSerial, Cnt - 1), "3", vbBinaryCompare) <> 0 Then Let DMY = "M"
If InStr(1, Left(TestDateSerial, Cnt - 1), "9", vbBinaryCompare) <> 0 Then Let DMY = "y"
Dim sShortDate As String ' the next line gives us a character string where the fist numbers are replaced by characters representing if they are a day or month or year
Let sShortDate = Evaluate("=REPT(""" & DMY & """," & Cnt1 & ")") & Sep1 ' ======= make first bit of output ==
' ===== xx & sep1 ******************************************
Dim Pos2 As Long ' ================================================== ==========================================
Let Pos2 = Len(sShortDate) + 1 ' This should be the position where the second number section starts
Do While IsNumeric(C) = True
Let Cnt = Cnt + 1 ' we are counting the characters ( numbers ) in the second ( middle ) character ( number ) section
Let C = Mid(TestDateSerial, Cnt, 1)
Loop
Dim Cnt2 As Long: Let Cnt2 = Cnt - Pos2 ' this will be the count of the characters ( numbers ) in the second ( middle ) number section
' ========================== at first character in second seperator ========================================
Dim Sep2 As String
Do While IsNumeric(C) = False
Let Sep2 = Sep2 & C '
Let Cnt = Cnt + 1 ' we are counting through the characters making up the second seperator
Let C = Mid(TestDateSerial, Cnt, 1)
Loop ' While IsNumeric(C) = False
' the Cnt C counting stops at the start of the last number section
' the next lines search the numbers in the second ( middle) number section to determine if they are a day or month or year
' at this point we have got the second seperator string, Sep2 and are at the start of the last number ===
If InStr(1, Mid(TestDateSerial, Pos2, Cnt2), "4", vbBinaryCompare) <> 0 Then: Let DMY = "d" ' we are looking in the middle number section
If InStr(1, Mid(TestDateSerial, Pos2, Cnt2), "3", vbBinaryCompare) <> 0 Then Let DMY = "M"
If InStr(1, Mid(TestDateSerial, Pos2, Cnt2), "9", vbBinaryCompare) <> 0 Then Let DMY = "y"
Let sShortDate = sShortDate & Evaluate("=REPT(""" & DMY & """," & Cnt2 & ")") & Sep2 ' this adds to the sShortDate string the characters to indicate the type ( day month or year ) of the middle section numbers and the second seperator
' ==== xx & Sep1 & yy & Sep2 *******************************************
Dim Pos3 As Long: Let Pos3 = Len(sShortDate) + 1 ' this should be the start position of the last number section
Dim Cnt3 As Long '
Let Cnt3 = Len(TestDateSerial) - Pos3 + 1 ' this should be the number of characters ( numbers ) in the last number section
' we don't bother to loop through the last number section
' the next lines search the numbers in the third ( last ) number section to determine if they are a day or month or year
If InStr(1, Mid(TestDateSerial, Pos3, Cnt3), "4", vbBinaryCompare) <> 0 Then: Let DMY = "d" ' we look from the start of the last number section, Pos3 , for a length of its count of its characters , Cnt3
If InStr(1, Mid(TestDateSerial, Pos3, Cnt3), "3", vbBinaryCompare) <> 0 Then Let DMY = "M"
If InStr(1, Mid(TestDateSerial, Pos3, Cnt3), "9", vbBinaryCompare) <> 0 Then Let DMY = "y"
Let sShortDate = sShortDate & Evaluate("=REPT(""" & DMY & """," & Cnt3 & ")") ' finally we add the characters representing the characters to indicate the type ( day month or year ) of the last section
' ===== xx & Sep1 & yy & Sep2 & zzz ************************************************** ***********************
' ================================================== ================================================== =========
GoTo Rem2
Bed: ' Error handling code section for if the above coding errored
Let sShortDate = "Error getting sShortDate"
On Error GoTo -1
Rem2: ' Rem 2 Some other computer and user info
On Error Resume Next ' In case info below is somehow protected
Dim UsrNme As String: Let UsrNme = Application.UserName: Let UsrNme = Environ("username")
Dim CmprNme As String: Let CmprNme = Environ("computername")
Dim WOS As String: Let WOS = Environ("OS")
'Dim VersXl As String: Let VersXl = " ( " & ExcelVersion & " ) "
On Error GoTo 0

'Debug.Print sShortDate
Let GetMySystemsShortDate = sShortDate
End Function

DocAElstein
03-02-2025, 07:38 PM
_.____

DocAElstein
03-26-2025, 02:56 AM
Some notes in support of these threads
https://eileenslounge.com/viewtopic.php?f=27&t=42065
https://eileenslounge.com/viewtopic.php?f=27&t=42024

_..____ later