Excel VBA - Date in different language settings

Using Excel 2010, the user selects a date in a cell.  Code converts the date to a string to use in the query.  This is the string format: "CONVERT(DATETIME, '2013-06-30 00:00:00', 102) ".   If the user has a language setting other than English the query doesn't work because the date doesn't convert to the string in the format shown.  Having the user change language settings is not an option so how can I get the date into a format that will work in the query?  


Public Sub GetDates()
    Worksheets("Select Date Range").Select
    dStartDate = Selection.Value
    dEndDate = Selection.Value
    ' convert date to format needed for queries
    ' CONVERT(DATETIME, '2013-06-30 11:59:59', 102)
    cStartDate = "CONVERT(DATETIME, '" & dStartDate & " 00:00:00', 102)"
    cEndDate = "CONVERT(DATETIME, '" & dEndDate & " 00:00:00', 102)"
End Sub
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Shaun KlineLead Software EngineerCommented:
You can use the VBA format function to put the date in the desired format: Microsoft Help: VBA Format Function.

The format you should need is "yyyy-mm-dd".
try this

cStartDate = Format(dStartDate ,"yyyy-mm-dd hh:mm:ss")
cEndDate = Format(dEndDate ,"yyyy-mm-dd hh:mm:ss")

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.