Solved

Check short date format of local machine from within VB

Posted on 2001-06-18
10
205 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Window placement 17 75
VB 6.0 printer how to align 6 58
Sorting multiple rows and columns, and count duplicates in Excel 2013 4 69
Advice in Xamarin 21 56
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

920 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now