简体   繁体   English

将日期格式从dd-mm-yyyy更改为yyyy-mm-dd

[英]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 . 在我的工作表中,单元格具有dd-mm-yyyy格式的日期,我正在使用这些日期进行sql查询,这些日期接受yyyy-mm-dd格式的日期格式。 for that i am converting dates to yyyy-mm-dd format as follows : - 为此,我将日期转换为yyyy-mm-dd格式,如下所示:-

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 . 但是我以dd-mm-yyyy格式获取开始日期和结束日期。 why ???? 为什么? any suggestion ... As i am new to vba , please ignore my silly mistakes if any . 任何建议...由于我是vba的新手,请忽略我的愚蠢错误(如有)。 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. 您的错误是您将DATE转换为STRING格式,然后又转换回DATE类型。

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). (不能100%地确定该string是正确的关键字,但是我确定您有这个主意)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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