Solved

Check short date format of local machine from within VB

Posted on 2001-06-18
10
217 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses
Course of the Month9 days, 13 hours left to enroll

624 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