gpt4 book ai didi

excel - 为什么这个 CDATE 不能格式化为 mm/yyyy?

转载 作者:行者123 更新时间:2023-12-03 00:32:05 24 4
gpt4 key购买 nike

我想将字符串“mm/dd/yy hh:mm AM/PM”转换为日期“mm/yyyy”

为什么下面的代码输出11-2-2015

Sub Test()

Dim yourStringDate As String
Dim yourDateVariable As Date

yourStringDate = "11/2/15 12:00 AM"
yourDateVariable = Format(CDate(yourStringDate), "mm/yyyy")
MsgBox yourDateVariable

End Sub

最佳答案

怎么样

Dim yourStringDate As Date
yourStringDate = DateValue("11/2/15 12:00 AM")
MsgBox Format(yourStringDate, "mm/yyyy")

或采用您的原始格式

Dim yourStringDate As String
Dim yourDateVariable As Date
yourStringDate = "11/2/15 12:00 AM"
yourDateVariable = CDate(yourStringDate)
MsgBox Format(yourDateVariable, "mm/yyyy")

关于excel - 为什么这个 CDATE 不能格式化为 mm/yyyy?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14270923/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com