vba mm/dd/yyyy HH:mm:ss 我如何提取日期?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19504582/
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
mm/dd/yyyy HH:mm:ss how can I extract date?
提问by user2898085
How can I extract only the date in excel-vba from this format mm/dd/yyyy HH:mm:ss?
如何从这种格式 mm/dd/yyyy HH:mm:ss 中仅提取 excel-vba 中的日期?
I need to compair two dates in this format but I only want to know if the day is different.
我需要以这种格式比较两个日期,但我只想知道这一天是否不同。
回答by
if cell A1 = 04/16/2013 10:28:12
如果单元格 A1 = 04/16/2013 10:28:12
then put that in cell B2
然后把它放在单元格B2
=DATE(YEAR(A1),MONTH(A1),DAY(A1))
or concatenate components if you need a different order ie.
如果您需要不同的顺序,或者连接组件,即。
for mm/dd/yyyy
为了 mm/dd/yyyy
=MONTH(A1) & "/" & DAY(A1) & "/" & YEAR(A1)
Further to the comparison
进一步的比较
Something like this?
像这样的东西?
Sub Main()
Dim myDate As Date
myDate = Now
Debug.Print myDate
Dim newDate As Date
newDate = DateAdd("d", 2, myDate)
Debug.Print newDate
Select Case DateDiff("d", myDate, newDate)
Case Is > 0
Debug.Print "newDate is newer"
Case 0
Debug.Print "dates are the same"
Case Is < 0
Debug.Print "newDate is older"
End Select
End Sub
Run the macro and check out the Immediate Window
CTRL+G
运行宏并查看Immediate Window
CTRL+G
DateAdd()
adds 2 days to the newDate
DateAdd()
将 2 天添加到 newDate
DateDiff()
returns the difference in days between the two dates
DateDiff()
返回两个日期之间的天数差