Solved

Check short date format of local machine from within VB

Posted on 2001-06-18
10
202 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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
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…
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…

759 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

17 Experts available now in Live!

Get 1:1 Help Now