Now() function with time trim

前端 未结 6 1025
忘了有多久
忘了有多久 2020-12-16 10:23

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

or is there just a function for this idea?

相关标签:
6条回答
  • 2020-12-16 11:01

    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 Date function as tlayton says (to get the current date) you can also cast a date value to a integer to get the date-part from an arbitrary date: Int(myDateValue).

    0 讨论(0)
  • 2020-12-16 11:01
    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...

    0 讨论(0)
  • 2020-12-16 11:09

    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
    
    0 讨论(0)
  • 2020-12-16 11:14

    There is a Date function.

    0 讨论(0)
  • 2020-12-16 11:14

    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

    =format_date(now())
    

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

    0 讨论(0)
  • 2020-12-16 11:26

    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.

    0 讨论(0)
提交回复
热议问题