vba 如何从日期时间格式 12/10/2012 12:18:58 PM 中提取小时?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15539977/
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-08 15:13:14  来源:igfitidea点击:

How to extract Hours from the date time format 12/10/2012 12:18:58 PM?

excelvbadateformat

提问by user2193697

How do I extract the hours from the date time format 12/10/2012 12:18:58 PM?

如何从日期时间格式中提取小时数12/10/2012 12:18:58 PM

I found this Hour(Format(d1, "mm/dd/yyyy HH:mm:ss HH24"))on another Stack Overflow question, but not working for me.

Hour(Format(d1, "mm/dd/yyyy HH:mm:ss HH24"))在另一个 Stack Overflow 问题上发现了这个,但对我不起作用。

回答by Ali Anwar

This should work

这应该工作

=TEXT(D1, "hh")

=TEXT(D1, "hh")

and format as time

并格式化为时间

回答by chris neilsen

If D1contains a Date/Time as Date Time Serial Numberor as a string recognisable as a date timethen use =Hour(D1)

如果D1包含日期/时间作为Date Time Serial Number或作为可识别的字符串,date time则使用=Hour(D1)

Eg D1= the string 21/03/2013 18:45:00=Hour(D1)= 18

例如D1= 字符串21/03/2013 18:45:00=Hour(D1)=18

Eg D1= 41354.781250then =Hour(D1)= 18
Note: D1may contain a value but be formatted to display as a date/time. Check the cell number format.

例如D1= 41354.781250then =Hour(D1)=18
注意:D1可能包含一个值但被格式化以显示为日期/时间。检查单元格编号格式。

To do this in VBA try this

要在 VBA 中做到这一点,试试这个

Sub demo()
    Dim rng As Range
    Dim Hr As Long

    Set rng = Range("D1")

    Hr = Hour(rng.Value)

    Debug.Print Hr
End Sub

View the result in the Immediate Window

查看结果在 Immediate Window

回答by Lingasamy Sakthivel

Try with:

尝试:

Hour(format(now(), "yyyy-MM-dd hh:mm:ss"))

回答by sk8asd123

if "12/10/2012 12:18:58 PM" is in cell A1:

如果“12/10/2012 12:18:58 PM”在单元格 A1 中:

=TIME(HOUR(a1),MINUTE(a1),SECOND(a1))

After the formula is entered, you can right click on "format cells" and select the time format you want to see. I've verified this formula in Excel 2013.

输入公式后,您可以右键单击“格式化单元格”,然后选择您要查看的时间格式。我已经在 Excel 2013 中验证了这个公式。

回答by Kasnady

Try this http://www.vb6.us/tutorials/date-time-functions-visual-basicit really work in vba

试试这个http://www.vb6.us/tutorials/date-time-functions-visual-basic它真的适用于 vba

Or just use Nowif you want the date and time by now

或者Now如果你现在想要日期和时间就使用