简体   繁体   中英

changing date format from dd-mm-yyyy to yyyy-mm-dd

In my sheet, cells has dates in dd-mm-yyyy format and I am using those date for sql query which accept date format in yyyy-mm-dd format . for that i am converting dates to yyyy-mm-dd format as follows : -

Dim startDate As Date, endDate As Date
startdate = Format(Sheet1.Range("b1").Value, "yyyy-mm-dd")
endDate = Format(Sheet1.Range("c1").Value, "yyyy-mm-dd")

but i get startdate and enddate in dd-mm-yyyy format . why ???? any suggestion ... As i am new to vba , please ignore my silly mistakes if any . thanks in advance ....

You need the new representation of the dates to be sent towards your database. You error is that you are converting a DATE to a STRING format and back into a DATE type.

Try something like this:

Dim startDate As String, endDate As String

startdate = Format(Sheet1.Range("b1").Value, "yyyy-mm-dd")
endDate = Format(Sheet1.Range("c1").Value, "yyyy-mm-dd")
...

(not 100% sure that string is the correct keyword, but I'm sure you got the idea).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM