日期格式 - 将 dd/m/yyyy 变成 dd/mm/yyyy
Date formatting - making dd/m/yyyy into dd/mm/yyyy
我有一个非常简单的问题(但我已经坚持了一段时间)。有谁知道如何将变量中的日期值从 dd/m/yyyy 变为 dd/mm/yyyy?
dim lastdaylastmonth as date
lastdaylastmonth = DateSerial(Year(Date), Month(Date), 0)
因此,截至目前,此代码将 return 上个月的最后一天,即 2015 年 5 月 31 日。为了格式化,并沿着代码向下使用 MID() 以提取月份字符串“05”,我需要将日期转换为 dd/mm/yyyy 或 31/05/2015。有谁知道这个的简单解决方案?我试过:
lastdaylastmonth = format(DateSerial(Year(Date), Month(Date), 0), "dd/mm/yyyy")
它仍然 return 是相同的值!有大侠在吗? :D
看到这个:
lastdaylastmonth = DateSerial(Year(Date), Month(Date), 0)
?lastdaylastmonth
31.05.2015
? format(lastdaylastmonth,"dd.mm.yyyy")
31.05.2015
? format(lastdaylastmonth,"mm")
05
最后输出的是一个字符串,可以在您的代码中使用。
使用字符串
Sub dural()
Dim lastdaylastmonth As String
lastdaylastmonth = Format(DateSerial(Year(Date), Month(Date), 0), "dd/mm/yyyy")
MsgBox lastdaylastmonth
End Sub
我更喜欢这种方法,因为我必须处理世界各地许多不同的 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)
我有一个非常简单的问题(但我已经坚持了一段时间)。有谁知道如何将变量中的日期值从 dd/m/yyyy 变为 dd/mm/yyyy?
dim lastdaylastmonth as date
lastdaylastmonth = DateSerial(Year(Date), Month(Date), 0)
因此,截至目前,此代码将 return 上个月的最后一天,即 2015 年 5 月 31 日。为了格式化,并沿着代码向下使用 MID() 以提取月份字符串“05”,我需要将日期转换为 dd/mm/yyyy 或 31/05/2015。有谁知道这个的简单解决方案?我试过:
lastdaylastmonth = format(DateSerial(Year(Date), Month(Date), 0), "dd/mm/yyyy")
它仍然 return 是相同的值!有大侠在吗? :D
看到这个:
lastdaylastmonth = DateSerial(Year(Date), Month(Date), 0)
?lastdaylastmonth
31.05.2015
? format(lastdaylastmonth,"dd.mm.yyyy")
31.05.2015
? format(lastdaylastmonth,"mm")
05
最后输出的是一个字符串,可以在您的代码中使用。
使用字符串
Sub dural()
Dim lastdaylastmonth As String
lastdaylastmonth = Format(DateSerial(Year(Date), Month(Date), 0), "dd/mm/yyyy")
MsgBox lastdaylastmonth
End Sub
我更喜欢这种方法,因为我必须处理世界各地许多不同的 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)