Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 226
  • Last Modified:

Check short date format of local machine from within VB

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
Al
Asked:
Al
  • 3
  • 3
  • 2
  • +2
1 Solution
 
deightonCommented:
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
 
morgan_peatCommented:
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
 
nutwissCommented:
more to the point, datediff is aware of your format settings, and so any explicit conversion will be unneccesary.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
nutwissCommented:
same with dateadd, datepart etc.....

try to use these functions instead of your preprietary code
0
 
deightonCommented:
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
 
ArunKannanCommented:
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
 
AlAuthor Commented:
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
 
AlAuthor Commented:
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
 
deightonCommented:
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
 
AlAuthor Commented:
Simple but elegant

Cheers
0

Featured Post

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!

  • 3
  • 3
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now