我有一个非常简单的问题(但我现在已经坚持了一段时间)。有谁知道如何在变量中将日期值从dd / m / yyyy变为dd / mm / yyyy?
dim lastdaylastmonth as date
lastdaylastmonth = DateSerial(Year(Date), Month(Date), 0)
因此,截至目前,此代码将返回上个月的最后一天,因此将于2015年5月31日返回。为了格式化,并沿着代码向下拉出MID()以取出月份字符串" 05",我需要将日期转换为dd / mm / yyyy或31/05/2015。有谁知道这个简单的解决方案?我试过了:
lastdaylastmonth = format(DateSerial(Year(Date), Month(Date), 0), "dd/mm/yyyy")
它仍然返回相同的值!那里有英雄吗? :d
答案 0 :(得分:2)
使用字符串
ResponseBuilder::send()
答案 1 :(得分:0)
见:
lastdaylastmonth = DateSerial(Year(Date), Month(Date), 0)
?lastdaylastmonth
31.05.2015
? format(lastdaylastmonth,"dd.mm.yyyy")
31.05.2015
? format(lastdaylastmonth,"mm")
05
最后一个输出是一个字符串,可以在你的代码中使用。
答案 2 :(得分:0)
我更喜欢这种方法,因为我必须处理世界各地的许多不同的Excel版本。使用格式为" mm / dd / yyyy"只会在美国工作(或者英语是定义的语言)。在其他委员会中,以下代码仍然有效。
Dim lastdaylastmonth As Date
'Last day of last month
lastdaylastmonth = Date - Day(Date) - 1
'Formatting it US-style regardless of international Excel or Windows settings
Debug.Print Right("0" & Day(lastdaylastmonth), 2) & "/" & Right("0" & Month(lastdaylastmonth), 2) & "/" & Year(lastdaylastmonth)