Can't format the date properly in VBA, Excel 2012

孤人 提交于 2019-12-01 00:51:36

I know I'm too late, But I think you need to differentiate between months (MM) and minutes (mm)...So try this...

Format(yourDateObj, "yyyy/MM/dd hh:mm:ss");

Hope this Helps..:)

Ould Abba

use the Format() function

Format("17/07/2012", "yyyy-mm-dd")

the result is 2012-07-17

Private Sub testje()

Dim date1, date2, datetime1, datetime2, time1, time2 As Date

date1 = Cells(1, 1)
time1 = Cells(1, 2)
datetime1 = Format(date1, "dd/mm/yyyy") & " " & Format(time1, "hh:mm:ss")

Cells(2, 1) = datetime1

End Sub

Seems to work. .. Otherwise, set the Range format in Excel to Custom with definition "dd/mm/yyyy uu:mm:ss".

Try format ("mm/dd/yyyy") instead.

Within VBA don't Dim as Date if aiming to pass a datetime as part of a query to SQL. Instead try Dim as String.

You should then be able to format the string to the desired format using function suggested here already.

so for example:

Dim date1 As String
date1 = Cells(29, 10)
date1 = Format(Date1, "yyyy-MM-dd hh:mm:ss")

debug.print date1

I assume this is no longer of use to you (posted over a year ago!) but I have just come across the same problem, and this worked for me, maybe it will help someone else.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!