vba Now() 函数与时间修剪

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

Now() function with time trim

vbams-accessms-access-2007access-vbams-access-2003

提问by Justin

So the function =Now()....is there a way I can use this and only get the date, not the time?

所以这个功能=Now()......有没有办法我可以使用它并且只获取日期而不是时间?

or is there just a function for this idea?

或者这个想法只有一个功能吗?

回答by tlayton

There is a Date function.

有一个日期功能

回答by Dean Harding

Dates in VBA are just floating point numbers, where the integer part represents the date and the fraction part represents the time. So in addition to using the Datefunction as tlayton says (to get the currentdate) you can also cast a date value to a integer to get the date-part from an arbitrary date: Int(myDateValue).

VBA 中的日期只是浮点数,其中整数部分代表日期,小数部分代表时间。因此,除了使用Datetlayton 所说的函数(获取当前日期)之外,您还可以将日期值转换为整数以从任意日期获取日期部分:Int(myDateValue)

回答by Nick

DateValue(CStr(Now()))

That's the best I've found. If you have the date as a string already you can just do:

这是我找到的最好的。如果您已经将日期作为字符串,则可以执行以下操作:

DateValue("12/04/2012 04:56:15")

or

或者

DateValue(*DateStringHere*)

Hope this helps someone...

希望这可以帮助某人...

回答by Berend Nieuwhof

I would prefer to make a function that doesn't work with strings:

我更愿意制作一个不适用于字符串的函数:

'---------------------------------------------------------------------------------------
' Procedure : RemoveTimeFromDate
' Author    : berend.nieuwhof
' Date      : 15-8-2013
' Purpose   : removes the time part of a String and returns the date as a date
'---------------------------------------------------------------------------------------
'
Public Function RemoveTimeFromDate(DateTime As Date) As Date


    Dim dblNumber As Double

    RemoveTimeFromDate = CDate(Floor(CDbl(DateTime)))

End Function

Private Function Floor(ByVal x As Double, Optional ByVal Factor As Double = 1) As Double
    Floor = Int(x / Factor) * Factor
End Function

回答by Rob Gray

You could also use Format$(Now(), "Short Date") or whatever date format you want. Be aware, this function will return the Date as a string, so using Date() is a better approach.

您还可以使用 Format$(Now(), "Short Date") 或您想要的任何日期格式。请注意,此函数会将日期作为字符串返回,因此使用 Date() 是更好的方法。

回答by Karthick Gunasekaran

Paste this function in your Module and use it as like formula

将此函数粘贴到您的模块中并将其用作公式

Public Function format_date(t As String)
    format_date = Format(t, "YYYY-MM-DD")
End Function

for example in Cell A1 apply this formula

例如在单元格 A1 中应用此公式

=format_date(now())

it will return in YYYY-MM-DD format. Change any format (year month date) as your wish.

它将以 YYYY-MM-DD 格式返回。根据需要更改任何格式(年月日)。