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?
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?
There is a Date function.
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)
.
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
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.
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.