Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Date Time settings on users machine

Status
Not open for further replies.

kiloez

Programmer
Sep 5, 2003
8
US
Does anyone know how to get the Date Time settings on a user's machine? I need to get the D/T setting in order to pass the correct format to a routine so that it will run correctly where ever in the world (US, Eur, AP) the app is being used.
 
I'm using XL but the VBA code I'm looking for should work in any app that supports VBA. While I'm not sure, I think there is an API function that returns the Regional Settings on a user's machine. I haven't found it yet.
 
Is there something wrong with the "DATE" and "FORMAT" functions?

My VB4's InfoView indicates that BOTH are available. I'm pretty certain that 'date'/'date$' AND 'format()' are intrinsic thoughout VBAs, since vb4 was supposedly the basis from which VBAs were refined and further developed.

example:
dim TellMeDateInUSFormat as string
TellMeDateInUSFormat = format(DATE$, 'mmmm dd, yyyy')
debug.print "US DATE FORMAT:" & TellMeDateInUSFormat

--MiggyD
 
Just realized: the month's spelling may differ from country to country.

you can code the FORMAT to use numbers instead of spelling out the month -- or -- DIM xxx As DATE (if the info is being seen by other nations) then use the Format to display it in their setting like: format(MyDate, "dd-mmm-yy').

--MiggyD
 
If you want to show dates in the user's D/T format, just use the FORMAT Function in VBA and use the formats "Short Date", "Long Date", "Short Time" and "Long Time".
Code:
Sub Date_Time_System()
MsgBox Now
MsgBox Format(Now, "Short Date") ' eg. 9/11/2003
MsgBox Format(Now, "Long Date") ' eg. September 11, 2003
MsgBox Format(Now, "Short Time") ' 24 Hour Clock (14:30)
MsgBox Format(Now, "Long Time") ' AM/PM Clock (2:30 PM) or 24 hr. w/ sec.
End Sub
End Sub
These Formats will return the system set D/T formats from the current PC.

I hope this helps!



Peace! [peace]

Mike

Never say Never!!!
Nothing is impossible!!!
 
Not exactly MiggyD,

You are SETTING the format to "dd-mmm-yyyy", which may not be the Date Format of the current machine. Using "Short Date" will return a date format that the user is used to seeing because it is the actual format for all of their applications (except for the ones that have their own D/T settings option).

;-)


Peace! [peace]

Mike

Never say Never!!!
Nothing is impossible!!!
 
Yep, you're right.[thumbsup] I set it that way because of what Kiloez said:

"...D/T setting in order to pass the correct format to a routine so that it will run correctly where ever..."


Without knowing how the "routine" is setup, my conclusion was kiloez has already hard coded the format.

How is the routine set up? I don't know. Is the date/time to be stripped FIRST to save time because it's being used often thoughout the program(*)? or is it simply used once, in which case it should be passed as a whole(**)?

* Dim MyDAY as integer, MyMonth as integer
MyDAY = val(format(date$, "DD"))
MyMonth = val(format(date$, "MM"))
call MyRoutine (MyDAy, MyMonth)
SUB MyRoutine (usrDAY as integer, usrMONTH as integer)

** call MyRoutine (now)
SUB MyRoutine (usrDayTime as date)


Either way, I am hopefull that Kiloez will take the time to look at the help file's examples of the Format command and let us know if anything here helped, other than looking for an API.
 
If you are in excel - you can test international settings:

[tt]With Application
Select Case .International(xlDateOrder)
Case 0
MsgBox "Date order: " & "MDY"
Case 1
MsgBox "Date order: " & "DMY"
Case 2
MsgBox "Date order: " & "YMD"
End Select
MsgBox "Date separator: " & .International(xlDateSeparator)
MsgBox "Year code: " & .International(xlYearCode)
MsgBox "Currency: " & .International(xlCurrencyCode)
' etc.
End With[/tt]

combo
 
kiloez,

Now that I see you are online. I was wondering if you have found a solution to the problem???



Peace! [peace]

Mike

Never say Never!!!
Nothing is impossible!!!
 
Hello again, I thank everyone for their help. Here's the lowdown on what I've come to. I realize that there are a number of different ways to reformat a date and had actually many of them, all with success. I had written separate "sub" routines and got the reformatted dates without a problem. My problem was declaring the return value of a function I was using "as Date". When doing this no matter how I tried to reformat the date the function would return the system date, which in London returned dd/mm/yyyy. Passing this to a Sybase BD which is looking for mm/dd/yyyy failed. Well guess what? After changing 'Function DateChng(x as Date) as Date' to 'Function DateChng(x as Date)', everything went just fine. Any further comments are welcomed.
 
I didn't realize it was a function. For some reason, I was under the impression that it was a submodule; and that's why I wrote it that way on my last post above.

I'm glad to see you read it. Good luck with the rest of your coding.
--MiggyD
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top