86

I am using DateTime.Now in my Excel Macro to show the current timestamp.

It shows timestamp in "dd-MM-yyyy hh:mm:ss" format.

Instead, how can I get the timestamp in "yyyy-MM-dd hh:mm:ss" format?

10 Answers 10

125

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

0
43

DateTime.Now returns a value of data type Date. Date variables display dates according to the short date format and time format set on your computer.

They may be formatted as a string for display in any valid date format by the Format function as mentioned in aother answers

Format(DateTime.Now, "yyyy-MM-dd hh:mm:ss")
0
7
Format(Now(), "yyyy-MM-dd hh:mm:ss")
4

If some users of the code have different language settings format might not work. Thus I use the following code that gives the time stamp in format "yyymmdd hhMMss" regardless of language.

Function TimeStamp()
Dim iNow
Dim d(1 To 6)
Dim i As Integer


iNow = Now
d(1) = Year(iNow)
d(2) = Month(iNow)
d(3) = Day(iNow)
d(4) = Hour(iNow)
d(5) = Minute(iNow)
d(6) = Second(iNow)

For i = 1 To 6
    If d(i) < 10 Then TimeStamp = TimeStamp & "0"
    TimeStamp = TimeStamp & d(i)
    If i = 3 Then TimeStamp = TimeStamp & " "
Next i

End Function
3

this worked best for me:

        Cells(partcount + 5, "N").Value = Date + Time
        Cells(partcount + 5, "N").NumberFormat = "mm/dd/yy hh:mm:ss AM/PM"
1

Copy and paste this format yyyy-mm-dd hh:MM:ss in format cells by clicking customs category under Type

0

Timestamp in saving workbook path, the ":" needs to be changed. I used ":" -> "." which implies that I need to add the extension back "xlsx".

wb(x).SaveAs ThisWorkbook.Path & "\" & unique(x) & " - " & Format(Now(), "mm-dd-yy, hh.mm.ss") & ".xlsx"
0

You can use the Format function below:

$(get-date -f yyyyMMddhhmmss) 
-1

It can work as easy as this, choose the location you want, in this case I choose D3

Sheets("Put your Sheet's name here").Range("D3") = Now

Example, my sheet is called Sources

Sheets("Sources").Range("D3") = Now
-2

Use the Format function.

Format(Date, "yyyy-mm-dd hh:MM:ss")
1
  • 1
    Don't work! Date contain only the calendar date but the hour, minutes and seconds are fixed to 0:00:00 ! You have to use Now() instead..
    – A.Sommerh
    May 20, 2014 at 14:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.