Solved

Check short date format of local machine from within VB

Posted on 2001-06-18
10
209 Views
Last Modified: 2008-01-09
Part of my work involves comparing dates which I do using the following code.

Option Explicit
Dim Date1 As String, Date2 As String
Dim Value1 As Long, Value2 As Long
Private Sub Form_Load()
Show
Date1 = "23/04/01"
Date2 = "01/06/00"
Value1 = CLng(DateValue(Date1))
Value2 = CLng(DateValue(Date2))
Print Value1, Value2
End Sub

This will only work if the local machine uses dd/mm/yy date format.  How can I check the date format of the machine and change it to this format if necessary.

I was thinking of using an API call, but I have never done this and am not sure how to proceed.

Thanks

Alistair
0
Comment
Question by:Al
  • 3
  • 3
  • 2
  • +2
10 Comments
 
LVL 18

Expert Comment

by:deighton
ID: 6201441
Option Explicit
Dim Date1 As String, Date2 As String
Dim Value1 As Long, Value2 As Long
Private Sub Form_Load()
Show
Date1 = dateserial(2001,4,23)
Date2 = dateserial(2001,6,01)
Value1 = CLng(DateValue(Date1))
Value2 = CLng(DateValue(Date2))
Print Value1, Value2
End Sub

'if the dates are user inputs, then get the values using left,mid & right

0
 
LVL 1

Expert Comment

by:morgan_peat
ID: 6201489
Try using the DateDiff function instead.
This will be loads quicker than using intermediate variables, CLng's etc....

eg:
DateDiff("d", "01/06/00", "23/4/01")
returns 326 - 326 days between the 2 dates.
0
 
LVL 4

Expert Comment

by:nutwiss
ID: 6201508
more to the point, datediff is aware of your format settings, and so any explicit conversion will be unneccesary.
0
Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

 
LVL 4

Expert Comment

by:nutwiss
ID: 6201516
same with dateadd, datepart etc.....

try to use these functions instead of your preprietary code
0
 
LVL 18

Expert Comment

by:deighton
ID: 6201541
datediff will confuse "01/06/00" with 6/1/00 if the date settings are changed from dd/mm/yy to mm/dd/yy - but not in the case of 23/04/01 - leading to problems.

if you force input to be dd/mm/yy then construct the date as follows

dim datX as date

sDate = "01/06/01"  'example user input date

datX = DateSerial(right(sdate,2),mid(sdate,4,2),left(sdate,2))
'then always the 1st of July.
0
 

Expert Comment

by:ArunKannan
ID: 6201542
Hi,
The above Solutions provided by NutWiss will work. But if you want to use API's to do that, then there are API's to Get the Current Format of your Machine and to change the current settings of your machine.

The API GetDateFormat will give the Current Date Format in your system. Then, SetLocaleInfo sets the date format in your machine.

But it's not good programming sense to change the settings of your machine. So use datediff as given by NutWiss.


Arun
0
 

Author Comment

by:Al
ID: 6201543
Thanks for your answers guys.  However I don't think I made my question clear enough.  The dates I start off with are downloaded from a main database in dd/mm/yy format and it is these dates I want to compare whether the local machine is dd/mm/yy or any other format.

If the machine is m/d/yy format VB will take 01/06/00 as the 6th Januay 2000.  If it is dd/mm/yy VB will take it as the 1st June 2000.
0
 

Author Comment

by:Al
ID: 6201546
Thanks for your answers guys.  However I don't think I made my question clear enough.  The dates I start off with are downloaded from a main database in dd/mm/yy format and it is these dates I want to compare whether the local machine is dd/mm/yy or any other format.

If the machine is m/d/yy format VB will take 01/06/00 as the 6th Januay 2000.  If it is dd/mm/yy VB will take it as the 1st June 2000.
0
 
LVL 18

Accepted Solution

by:
deighton earned 100 total points
ID: 6201564
Make the file dates into a VB date variable using my formula

dim datX as date

datX = DateSerial(right(sdate,2),mid(sdate,4,2),left(sdate,2))


then you can still do

Value1 = CLng(datX)


You know the data format is always dd/mm/yy so you can always build the correct date in VB.  

- trust me, it is the way to go.  
0
 

Author Comment

by:Al
ID: 6201570
Simple but elegant

Cheers
0

Featured Post

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

832 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question