vba 为什么不将此 CDATE 格式转换为 mm/yyyy?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14270923/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 19:09:05  来源:igfitidea点击:

Why won't this CDATE format to mm/yyyy?

excelexcel-vbavba

提问by brettdj

I want to convert a string "mm/dd/yy hh:mm AM/PM"to a date "mm/yyyy"

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

Why is the following code outputting 11-2-2015?

为什么会输出以下代码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

回答by brettdj

how about

怎么样

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

or in your original format

或以您的原始格式

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

回答by jawaad farooqui

I think lower case mm is for minutes, try MM for month.

我认为小写的 mm 代表分钟,试试 MM 代表一个月。