[RESOLVED] Return Format of FileDateTime
I am using the FileDateTime routine to examine the date & time of generation of a file. The reason is so that the program can check whether the file version being examined is more recent than that which had previously been viewed.
The routine works fine, but the Date value returned seems to be in whatever format the local machine is set to eg. dd/mm/yy, mm/dd/yy etc. This is making for difficulties when the program has to work on machines with different regional settings. Without being able to rely upon a consistent format for the date info. returned it is hard to write code to check for latest version.
Is there please a way of ensuring that FileDateTime always returns the date information in a certain format (eg. dd/mm/yy) regardless of the computer's regional settings and date/time settings. I would not wish that the program makes a permanent change to the user's computer settings.
camoore
Wales, UK
Re: Return Format of FileDateTime
if you are comparing dates, then it shouldn't matter what the format is....
-tg
Re: Return Format of FileDateTime
see the section about dates in the vb6 faqs
dates are stored as doubles, just displayed as dates, so it does not matter how it is displayed, to make calculations
Re: Return Format of FileDateTime
Here is the FAQ article you want: Why are my dates not working properly?
The problem is that you are not treating Dates as if they are Dates... you are treating them as Strings instead, which are entirely different.
If you can't solve it based on the FAQ article, show us the code and explain where the issues are.
Re: Return Format of FileDateTime
Thank you for replies. Techgnome : I agree that if I were simply comparing dates the format would be less important. However firstly due to the program's working on machines with different regional settings the same date may appear as a different string (eg. dd/mm/yy and mm/dd/yy) and hence comparison would be difficult and secondly I wished not only to compare dates but to test for which of two was the later or more recent. Hence the desire to ensure a consistent format of the return from the FileDateTime function.
Having read Si's article to which he kindly directed me, I have learnt about the complexities of dates in VB. The solution I developed which will solve my needs is to read the returned Date as VB handles it (ie. days since 30/12/1899 plus decimal parts of a day) by rounding this I got the whole number of days and was then able to put this into a string variable, FILEDAYSTRING, which is an unambiguous value describing the date of generation of a file, regardless of regional / personal computer settings.
Although the value is a bit obscure - eg. 40196 - it enables me now to compare other files of the same name and to determine which is the most recent (because this number will be larger for a more recent file).
Thus the problem is solved, and once again more has been learnt about the workings of VB. Thread RESOLVED, with many thanks.
camoore
Wales, UK
Code:
Dim FILEDATETIMEINFO As String 'these variables will be used to query the
'issue status of the file selected.
Dim FILEDATEDAYS As Date 'to be no. of days since 30/12/1899 that
'the file was generated.
Dim FILEDAYSTRING As String 'to be no. of whole days in FILEDATEDAYS
CHAR = "C:\RAILPROJECT\" & PATHX 'eg. C:\RAILPROJECT\7.125.110.xls
'Now to determine the date and time of generation of the selected .xls file
FILEDATEDAYS = FileDateTime(CHAR) 'file age in days since 30/12/1899
FILEDATEDAYS = Round(FILEDATEDAYS) 'round to nearest day
FILEDAYSTRING = CDbl(FILEDATEDAYS) 'set FILEDAYSTRING string to this no.of days
FILEDATETIMEINFO = FileDateTime(CHAR) 'set this string variable to returned date/
'time info. about the user selected file
Re: [RESOLVED] Return Format of FileDateTime
Erm... you seem to have mis-interpreted everything I'm afraid. There is no reason to avoid using Dates, and they are no more complex than numeric data types such as Integer and Single (you just need to remember that they are Dates rather than Strings).
Items stored in the Date data type (which includes Date variables, and the return value of FileDateTime) do not under any circumstances have a format at all - and therefore have no problem at all with Regional Settings.
The problems come when you don't treat them as Dates, but do things such as using Strings to hold them, which you apparently did before somehow - and are explicitly doing again with FILEDATETIMEINFO. Your code has a similar issue with FILEDAYSTRING (this time with Double rather than Date).
The code you posted should have been just this:
Code:
Dim FILEDATEDAYS As Date 'the date that the file was generated.
CHAR = "C:\RAILPROJECT\" & PATHX 'eg. C:\RAILPROJECT\7.125.110.xls
'Now to determine the date and time of generation of the selected .xls file
FILEDATEDAYS = FileDateTime(CHAR)
I recommend you show us the rest of the code related to this date issue, so that we can check that too.
Re: [RESOLVED] Return Format of FileDateTime
Thank you Si. I do not believe that I have mis-interpreted "everything", since it now seems to be working as I would wish.
In my above listed code, FILEDATETIMEINFO is a String variable being used to "hold" the return from the FileDateTime routine. This was the original problem, since when the date and time elements were parsed out of it into sub-strings the format of the date section depended upon regional and user settings. (Just as your excellent tutorial explains). This has been left in place so that the user sees the date in whatever format he is used to having it presented. That is the only use to which FILEDATATIMEINFO is now put.
For file version I am using the String variable FILEDAYSTRING to store the file age of inception as integer days since 30/12/1899. To arrive at this I firstly store the return from FileDateTime in a Date variable FILEDATEDAYS, then round this, then set FILEDAYSTRING to its value. This seems to work just fine. I end up with a number such as 40054, which is correct for a period of around 110 years. If I want to display it I can do so as a String. If I want to calculate with it I can use Val(String).
Then, regardless of regional or user settings if this file crops up again and is more recent in origin the number will be larger than 40054. That is all I was striving to achieve - a fairly simple means of reading, displaying and dealing with the date of origin of a file so I can tell if another of same name is earlier, later or the same date, to the nearest whole day.
camoore
Wales, UK
Re: [RESOLVED] Return Format of FileDateTime
Your current use of FILEDATETIMEINFO purely as a display to the user is fine (but superfluous). Trying to get information back from it was the mistake you had before.
Your use of FILEDAYSTRING is not safe, as it relies on Regional Settings - because you are storing a non-String value in a String. Using Val or similar will not guarantee that the value you get back from the String is the same as you put in to it.
The problems occur less frequently with numeric data types than they do with Dates (because there are fewer variations, mainly just "1,234.5" and "1.234,5"), but they are definitely still there.
If you want to store a number, you should use a variable with an apt data type (in this case Double). Not only will it reduce problems, but it will also reduce memory usage.
It is also very debatable whether using a number is apt - it gives no benefits, and to me it would make much more sense to use a Date instead so that you can see what the value is (in the Watch window, or via a control or MsgBox, etc) rather than having to decipher it.
My use of the word 'everything' was a little harsh, but not by as much as you think. Here are a few mistakes from the top section of your previous post:
Quote:
I agree that if I were simply comparing dates the format would be less important.
Not just 'less important', but completely irrelevant.
Quote:
However firstly due to the program's working on machines with different regional settings the same date may appear as a different string (eg. dd/mm/yy and mm/dd/yy)
Dates do not appear as Strings, unless you use code to convert them.
The main thrust of the FAQ article is explaining when that happens accidentally - and to persuade you to pay attention to the data types you are using.
Quote:
and hence comparison would be difficult and secondly I wished not only to compare dates but to test for which of two was the later or more recent.
Comparing two Dates is no different to comparing two Integers (or two Doubles, etc).
Comparing items that have different data types to each other is where you need to take care.
Quote:
Hence the desire to ensure a consistent format of the return from the FileDateTime function.
The return value has a data type of Date, and therefore has no format at all.
Re: [RESOLVED] Return Format of FileDateTime
Thank you Si for your further comments.
Your most helpful article did indeed explain a lot about the pitfalls of using a String variable to contain the return of the FileTimeDate function. That was my initial problem because, inter alia, the format in which the Date section appeared varies with the regional / user settings of the PC. It is still useful, not superflous, in my program because it enables a display of Date in the format the user is used to seeing it.
Whatever this format, comparison of two dates for equivalence is straightforward if they are both in String variables and of the same format. In my application the need arose for comparison of two dates, as strings, but possibly generated under different regional / user settings. Hence the post "How to ensure a consistent format of return from FileDateTime". While your article tended to concentrate on how to ENTER dates, it explained to me the manner in which VB6 stores a Date (ddddd.12345678) . That solved my problem, and my program is now working fine simply using the (Rounded) ddddd part (days since 30/12/1899) as a means of comparing two files for later version.
My apparent concentration on obtaining FileDateTime as a consistent-format String was that the information also needed to be added elsewhere into other String variables, Rich Text Boxes and text files.
I note your point about FILEDAYSTRING. In my application, since the date returned from FileDateTime is rounded it will only ever be a whole number with no decimal place or comma separators. Thus I think the code should be OK, and thus far thanks to you it seems to be thus.
There are several potential pitfalls in this date business, to which your article draws good attention. For now my problem is solved, and I know where to go to read up on the topic further.
Thanks Si, thread resolved to my complete satisfaction.
camoore
Wales, UK
Re: [RESOLVED] Return Format of FileDateTime
Quote:
Originally Posted by
camoore
It is still useful, not superflous, in my program because it enables a display of Date in the format the user is used to seeing it.
Displaying the date is not superfluous, using an extra variable for that purpose is - because the user cannot see the variable, so you may as well put the value directly from the Date variable/value to the control (which almost certainly has a String based property for what it displays).
Quote:
Whatever this format, comparison of two dates for equivalence is straightforward if they are both in String variables and of the same format.
..but only for equivalence.
If they are both Date variables then you can just as easily check for equivalence, check which is bigger, etc, etc, etc.
Quote:
In my application the need arose for comparison of two dates, as strings, but possibly generated under different regional / user settings.
Why Strings? :confused:
Quote:
My apparent concentration on obtaining FileDateTime as a consistent-format String was that the information also needed to be added elsewhere into other String variables, Rich Text Boxes and text files.
That is irrelevant to the initial work. You should do the comparisons etc using Date variables (rounded if that is what you particularly want), and only use Strings when needed.
For display purposes it is arguably best to not bother with a String variable (when you assign/add the Date variable to a String based property, it will be displayed using Regional Settings).
For storing the value in a file (assuming your program is what will read it) you should use the Format function to ensure a consistent format (such as ISO: "yyyy-mm-dd hh:nn"), and if needed (not for ISO) re-import it using the kind of code shown at the end of the FAQ article.
Quote:
I note your point about FILEDAYSTRING. In my application, since the date returned from FileDateTime is rounded it will only ever be a whole number with no decimal place or comma separators. Thus I think the code should be OK, and thus far thanks to you it seems to be thus.
The numbers will almost certainly always be over 1000, so (depending on Regional Settings) the digit separators are likely to be used at some point... and if that happens, your program is likely to mis-interpret the values (instead of 42344, it would read 42.344).
Re: [RESOLVED] Return Format of FileDateTime
Why Strings? The way the program works (and I do not say that it could not be done differently or better) is that I need to combine a file name - say xyz.xls and its generation date as a combined character group in a string variable (ListA). eg. xyz.xls/40106 abc.xls/40128 etc. etc. Elsewhere, the progam "reads" this long string, looks at the file names and immediately after them at their generation date. The file name plus generation date is then compared with another list (in a different string variable, ListB) to determine whether a given file in ListA is newer than its corresponding file in ListB. If newer, then an update routine is invoked. These lists may be generated under different regional / user settings - hence the original question about how to ensure a consistent date format. ListA is of source .xls files, ListB is of Access files to which the .xls are converted. I think that to explain more would be specific application detail and not of relevance to this thread.
You have made, thank you, one more important point about how regional settings could affect the string value of the whole number part of a (rounded) Date in that there could appear "." or "," characters. What I will do is parse the resulting string for these characters and, if present, remove them (by Replace routine) such that only a sequence of numerical characters such as 40106 remains.
camoore
Wales, UK
Re: [RESOLVED] Return Format of FileDateTime
There are much better ways than using a String, in terms of memory usage, speed, and safety. These include using a Type (with a String for the file name and Date for the date), or separate variables/arrays.
If you do use a String, it would be safer to use a date in the ISO format (with or without the time, as you prefer) rather than a double. This would also allow an easy comparison of whether one is later than the other.