Tabellab

Date, Time and Now

Lesson 1 of 7Reading

Three functions to start with

  • Date โ€” today, with no time part.
  • Time โ€” the current time, with no date part.
  • Now โ€” both together, to the second.
Sub Stamp()
    Dim d As Date

    d = Now

    ' Debug.Print shows dates in your Windows short date format (9/13/2026 on a US PC).
    ' The comments in this course write the value as 13 Sep 2026.
    Debug.Print Date      ' 13 Sep 2026
    Debug.Print Time      ' 14:07:32
    Debug.Print Now       ' 13 Sep 2026 14:07:32
    Debug.Print d + 30    ' 13 Oct 2026 14:07:32
End Sub

Declare dates as Date

Use Dim d As Date, never String. A real Date can be compared with < and >, sorted, and shifted by whole days with plain arithmetic: d + 30 is thirty days later, and endDate - startDate is the number of days between them. Once a date has become text you lose all of that.

A macro you can run right now

This one appends a timestamped line to a log sheet every time it runs, creating the sheet on the first run. Paste it into a standard module โ€” Alt+F11, Insert โ†’ Module โ€” and press F5 two or three times.

Option Explicit

Sub StampRunLog()
    Dim ws As Worksheet
    Dim stampedAt As Date
    Dim startedAt As Double
    Dim projectStart As Date
    Dim daysRunning As Long
    Dim nextRow As Long

    startedAt = Timer
    stampedAt = Now
    projectStart = DateSerial(2026, 1, 1)

    On Error Resume Next
    Set ws = ThisWorkbook.Worksheets("Run Log")
    On Error GoTo 0

    If ws Is Nothing Then
        Set ws = ThisWorkbook.Worksheets.Add( _
            After:=ThisWorkbook.Worksheets(ThisWorkbook.Worksheets.Count))
        ws.Name = "Run Log"
        ws.Range("A1:F1").Value = Array("Day", "Clock", "Full stamp", _
            "Weekday", "Days into project", "Seconds")
        ws.Range("A1:F1").Font.Bold = True
    End If

    daysRunning = DateValue(stampedAt) - projectStart
    nextRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row + 1

    ws.Cells(nextRow, 1).Value = DateValue(stampedAt)
    ws.Cells(nextRow, 2).Value = TimeValue(stampedAt)
    ws.Cells(nextRow, 3).Value = stampedAt
    ws.Cells(nextRow, 4).Value = Format(stampedAt, "dddd")
    ws.Cells(nextRow, 5).Value = daysRunning
    ws.Cells(nextRow, 6).Value = Round(Timer - startedAt, 3)

    ws.Range("A" & nextRow).NumberFormat = "yyyy-mm-dd"
    ws.Range("B" & nextRow).NumberFormat = "hh:mm:ss"
    ws.Range("C" & nextRow).NumberFormat = "yyyy-mm-dd hh:mm:ss"
    ws.Columns("A:F").AutoFit
    ws.Activate
End Sub

Line by line

  • startedAt = Timer gives the seconds since midnight as a Double. It is a stopwatch, not a clock: fine for measuring how long something took, useless across midnight, where it drops back to zero.
  • stampedAt = Now reads the clock once. Every further mention of Now would ask Windows again, so two calls a few lines apart can disagree โ€” and shortly before midnight they disagree about the day. One read, used six times, keeps the row consistent.
  • projectStart = DateSerial(2026, 1, 1) builds a date from three numbers. Do not write #1/1/2026# instead: a VBA date literal is always read as month/day/year whatever your regional settings say, so #3/4/2026# is 4 March and never 3 April.
  • The On Error Resume Next pair asks whether the sheet exists without stopping when it does not; the If ws Is Nothing block creates it and writes headers on the first run only.
  • DateValue(stampedAt) keeps the day and drops the clock, TimeValue(stampedAt) does the opposite. Both hand back a genuine Date, so those columns stay sortable and filterable as dates.
  • daysRunning = DateValue(stampedAt) - projectStart โ€” subtracting two dates gives a plain count of days, so it belongs in a Long, not in a Date.
  • Format(stampedAt, "dddd") returns the weekday name in the Windows display language, not in the language of your code.
  • End(xlUp).Row + 1 finds the first free row, so each run appends instead of overwriting.
  • The three NumberFormat lines change only what you see. Note that a cell format writes minutes as mm after hh, while VBA's Format function wants nn there โ€” two different worlds, and lesson 5 deals with the second one.

The trap: the date that is really text

The most expensive habit with dates in VBA is writing the pretty version into the cell. It looks perfect and it is a string. Text sorts alphabetically, so "01 Dec 2026" lands before "02 Jan 2026"; the filter offers a flat list instead of a year-month tree; SUMIFS with a date criterion quietly returns zero; and the next macro that subtracts two of them stops with a type mismatch.

' Wrong: the cell now holds text that merely looks like a date
ws.Range("A1").Value = Format(Now, "dd mmm yyyy")

' Right: store the date, then decide separately how it is displayed
ws.Range("A1").Value = Now
ws.Range("A1").NumberFormat = "dd mmm yyyy"

The rule is one line long: Format is for what a human reads โ€” a message box, a file name, a sheet title. For a cell, put the Date in and let NumberFormat do the presenting. Not sure which one a cell already holds? Debug.Print TypeName(ws.Range("A1").Value) answers Date or String, and settles the argument in a second.

Date is both a function and the name of a data type. That is legal and normal โ€” just never name a variable Date, Time or Now yourself.

How this course goes on

Once dates behave as values instead of text, the rest is arithmetic you can trust. Lesson 2 shows the number hiding under every date and why the time part is a fraction of it, lesson 3 builds dates from parts with DateSerial, and lesson 4 shifts and measures them with DateAdd and DateDiff, including why adding a month is harder than adding thirty days. Lesson 6 takes on the dates that arrive as text from somebody else's system, and lesson 7 covers month ends, quarters and working days.

Who this lesson is for

Almost every workbook that a macro touches has a date in it somewhere: an invoice date, a delivery date, a log of when a report was produced, a deadline that turns red. Dates are also where VBA code breaks most quietly. A macro that works on your PC gives different answers on a colleague's machine in another country, a column of dates refuses to sort, or a report run just before midnight stamps two different days on the same row. This lesson is for anyone who writes or maintains Excel macros and wants dates to behave as reliably as numbers do.

The free lab above contains the macro StampRunLog. Each time you run it, it appends a row to a Run Log sheet: the day, the clock time, the full timestamp, the weekday, the number of days since a project start on 1 January 2026, and how many seconds the macro took. Below we use that log to explain what Date, Time and Now really return, what lands in each cell, and the habits that keep dates sortable, comparable and correct wherever the workbook is opened.

The core idea: a date is a number with a costume

VBA has three functions that read the system clock. Date returns today with no time part, Time returns the current time with no date part, and Now returns both together. All three return a value of the Date data type, and underneath, a Date is a number: the whole part counts days from a fixed starting point, and the fraction is the share of the day that has passed. Noon is 0.5, 06:00 is 0.25, and 14:07:32 is about 0.5886.

Under every Date is a number: whole days plus a fraction of a day 4627712 Sep 20264627813 Sep 20264627914 Sep 20264628015 Sep 2026 Date = 46278 Now = 46278.5886 13 Sep 2026 14:07:32 Time = 0.5886: the share of 24 hours gone by (14:07:32) d + 30 adds 30 whole days ยท endDate - startDate counts the days between
On 13 Sep 2026 at 14:07:32, Date returns 46278 and Now returns 46278.5886. The yellow band is the time part, the fraction that Time returns on its own.

This is why date arithmetic in VBA is so pleasant once your values are real dates. d + 30 is thirty days later, endDate - startDate is the number of days between two dates, and < and > compare them correctly. For dates from March 1900 onwards the number is the same serial number Excel uses in cells, so a VBA Date written into a cell becomes an ordinary Excel date that formulas such as =A2+30 or =NETWORKDAYS(A2,B2) understand. Lesson 2 of this course looks at the counting start and the rare cases where VBA and the worksheet disagree.

The printed form, such as 9/13/2026 or 13.09.2026, is only a costume. Debug.Print and message boxes dress the number in the short-date format of the Windows regional settings, and a cell dresses it in its number format. The value is the same everywhere; only the presentation changes. Keep that separation in mind and most date problems never happen.

Worked example: three entries in the Run Log

Suppose, for example, that you run the lab macro twice on Sunday 13 September 2026 in the afternoon and once more on Monday morning. The table shows what each column holds, the number hiding under each value, and a worksheet formula that would produce the same result from the full timestamp in column C. The seconds in column F depend on your computer, so they are illustrative.

ColumnWritten by the macroRun 1Run 2Run 3Worksheet equivalent (row 2)
A DayDateValue(stampedAt)2026-09-13 (46278)2026-09-13 (46278)2026-09-14 (46279)=INT(C2)
B ClockTimeValue(stampedAt)14:07:32 (0.5886)14:09:05 (0.5896)08:30:10 (0.3543)=MOD(C2,1)
C Full stampstampedAt (one read of Now)46278.588646278.589646279.3543=NOW() (but it keeps changing)
D WeekdayFormat(stampedAt, "dddd")SundaySundayMonday=TEXT(C2,"dddd")
E Days into projectDateValue(stampedAt) - projectStart255255256=INT(C2)-DATE(2026,1,1)
F SecondsRound(Timer - startedAt, 3)for example 0.0160.0080.012none

A few things stand out. Columns A, B and C are real Date values, so you can sort the log, filter it by month with the date filter tree, or sum the hours between two stamps. Column D is deliberately text: a weekday name is a label for people, and it is written in the display language of Windows, so an English PC writes Sunday and a German one Sonntag. Column E is a plain count of days, which is why the lab stores it in a Long. Had it been declared As Date, the cell would show 255 days after the counting start, a meaningless date in September 1900.

Notice also that =NOW() is not a substitute for the macro. Worksheet NOW and TODAY are volatile: they recalculate every time the workbook does, so a log built with them would show the current time on every row. A macro writes a value once and it stays. In the worksheet you can do the same by hand with Ctrl+; for today's date and Ctrl+Shift+; for the current time, both entered as fixed values.

Read the clock once

The most important line in the lab is one of the shortest: stampedAt = Now. Every mention of Now asks Windows for the time again. If a macro wrote Now into three cells on three lines, the values would differ by fractions of a second, and if it ran at 23:59:59 the last cell could fall on the next day. Reading the clock once into a variable and using that variable everywhere guarantees that every column of a row describes the same moment.

Dim stampedAt As Date
stampedAt = Now
ws.Cells(nextRow, 1).Value = DateValue(stampedAt) ' day only
ws.Cells(nextRow, 2).Value = TimeValue(stampedAt) ' clock only
ws.Cells(nextRow, 3).Value = stampedAt ' both

The same rule applies to Date in longer macros. A report that runs across midnight and calls Date at the start and again at the end can label its header with one day and its footer with the next. Capture runDate = Date at the top and pass that variable around.

Timer is a different tool. It returns the seconds since midnight as a number with a fractional part, which makes it a handy stopwatch: store it at the start, subtract at the end, and you know how long the code took. It is not a clock, because it falls back to zero at midnight; a measurement that crosses midnight comes out negative. For timing a macro that is rarely a problem, but never use Timer to record when something happened.

Whose clock is it?

Now, Date and Time read the clock of the computer that runs the macro, in its local time zone, and the value carries no time zone with it. For a log kept on one PC that is exactly what you want. It becomes a question as soon as a workbook is shared: a colleague in another office who runs the same macro stamps his local time, and nothing in the cell says so. Daylight saving time adds a second wrinkle. On the night the clocks go back, one hour of local time happens twice, so two stamps can look identical although they are an hour apart, and on the night they go forward one hour of local time never happens at all.

For most business logs this is harmless, but it is worth deciding consciously. If people in several time zones write to the same sheet, add a column with the user name (Application.UserName) and, if needed, a note of the office or time zone, so that nobody later reads a nine-hour gap as a slow process. And if the system clock of a PC is wrong, every stamp it writes is wrong too: VBA has no independent source of time, it simply trusts Windows.

How to run the lab macro and make it your own, step by step

  1. Save the workbook as an Excel Macro-Enabled Workbook (.xlsm) with File โ†’ Save As.
  2. Press Alt+F11, choose Insert โ†’ Module, and paste the full StampRunLog code from the lab.
  3. Click inside the procedure and press F5 two or three times, a few seconds apart. Switch back to Excel with Alt+F11 or the taskbar: the Run Log sheet has one row per run.
  4. Click a cell in column A and look at the formula bar, then press Ctrl+Shift+~ to apply the General format temporarily (or change it via Home โ†’ Number Format โ†’ General) and see the serial number underneath. Undo with Ctrl+Z.
  5. In the editor, open the Immediate window with Ctrl+G and type ?CDbl(Now). You get today's serial number with its fraction, the same idea as in the diagram above.
  6. Type ?TypeName(Worksheets("Run Log").Range("A2").Value). The answer Date confirms that the cell holds a real date. Try the same on column D and you get String.
  7. Change projectStart = DateSerial(2026, 1, 1) to the start date of a real project of yours, and column E counts the days since then on every run.
  8. To log something useful, call StampRunLog as the last line of another macro, for example a monthly import, so every run of that macro leaves a timestamped trace.
Assign the macro to a button (Insert โ†’ Shapes, draw a shape, right-click โ†’ Assign Macro) if colleagues need to stamp a log without opening the editor.

Store dates, format for people

The lab closes with the trap that costs the most time: writing a nicely formatted date into a cell. Format(Now, "dd mmm yyyy") returns a String. Put that into a cell and it looks like a date, but Excel stores text, and text behaves differently in every way that matters. The diagram shows the first symptom you are likely to see, a sort that makes no sense.

The same four dates, sorted A to Z Format(...) written to the cell: text Date + NumberFormat: a value "01 Dec 2026"02 Jan 2026"02 Jan 2026"28 Feb 2026"15 Mar 2026"15 Mar 2026"28 Feb 2026"01 Dec 2026 โ‰  TypeName(cell.Value) = "String" TypeName(cell.Value) = "Date" sorted character by character sorted by the number underneath
Sorted ascending, text dates line up by their first characters, so 01 Dec comes before 02 Jan. Real dates sort by the number underneath and come out in calendar order.

The other symptoms are just as annoying: the AutoFilter shows a flat list of strings instead of grouping by year and month, SUMIFS with a date criterion returns zero, and the next macro that subtracts two of these cells stops with a type mismatch. The fix is to keep value and presentation separate:

ws.Range("A1").Value = Now
ws.Range("A1").NumberFormat = "dd mmm yyyy"

Use Format where a human reads the result and nothing calculates with it afterwards: a message box, a sheet name, a file name such as "Report_" & Format(Date, "yyyy-mm-dd") & ".xlsx". The year-month-day pattern in file names has a bonus: those names sort in date order in File Explorer.

Illustration for the Tabellab course Dates and Times in Excel VBA
Dates & Times: build, compare, shift and format dates reliably, including the text ones that arrived from somebody else's system.

Common mistakes and how to spot them

  • Dates declared as String. Dim d As String loses comparison and arithmetic. Declare As Date. If you suspect a cell holds text, ?TypeName(cell.Value) in the Immediate window settles it, and the worksheet =ISNUMBER(A2) returns FALSE for a text date.
  • Calling Now several times for one record. Values drift apart and can straddle midnight. Read the clock once into a variable.
  • Date literals typed the local way. A literal such as #3/4/2026# is always read as month/day/year, so it is 4 March, never 3 April, whatever your regional settings. Use DateSerial(2026, 4, 3) and there is nothing to misread.
  • A day count stored in a Date variable. The cell then shows a date in 1900 instead of a number. Use Long for whole days and Double for fractions of days.
  • Naming a variable Date, Time or Now. It hides the built-in function inside that procedure and produces confusing results. Pick names like runDate or stampedAt.
  • Using Timer as a clock. It resets at midnight. Use it only to measure durations.
  • Cell formats and Format patterns mixed up. The two use similar letters but are separate systems; lesson 5 lists the codes that differ, including the minutes.

Quick reference

VBAWorksheetWhat it gives you
Date=TODAY()Today, no time part
Time=MOD(NOW(),1)Current time, no date part
Now=NOW()Date and time together
DateValue(d) / TimeValue(d)=INT(C2) / =MOD(C2,1)Day part / time part of a timestamp
DateSerial(2026, 1, 1)=DATE(2026,1,1)A date built from year, month and day
Format(d, "dddd")=TEXT(C2,"dddd")Weekday name as text
TimernoneSeconds since midnight, for timing code
TypeName(cell.Value)=ISNUMBER(A2)Tells a real date from text
Range.NumberFormat = "yyyy-mm-dd"Ctrl+1 โ†’ Number โ†’ CustomChanges the display only
Ctrl+; ยท Ctrl+Shift+;Type today's date / current time as fixed values

Real-world uses

  • Audit trail for recurring macros. Call a logging routine at the end of every import or month-end macro so you can answer "when was this last updated, and by which run?" without guessing.
  • Stamping edits. In a shared tracker, a Worksheet_Change event can write Now into a "last changed" column next to the edited row, as a value that does not recalculate.
  • Ageing reports. Days between an invoice date and Date are a simple subtraction once both are real dates, which is the basis for 0โ€“30, 31โ€“60 and 61โ€“90 day buckets in accounts receivable.
  • Time-stamped file names. Saving exports as "Sales_" & Format(Now, "yyyy-mm-dd_hhnn") & ".csv" gives each run a unique name that sorts chronologically.
  • Measuring macro speed. The Timer pattern from the lab shows whether a change to your code really made it faster, in milliseconds rather than gut feeling.

What you will build in the rest of this course

This first lesson is free. The six lessons below make up the rest of the course, available as a one-time purchase of $9.90, and each one comes with code you can run straight away.

Dates Are Really Numbers. The Double under every Date, stripping the time off a timestamp, where the counting starts, and the difference between Value and Value2 when you read dates from cells.

Building Dates with DateSerial. Three numbers in, one date out, and why it happily accepts impossible values like month 13 or day 0. That behaviour gives you the classic month-end trick and safe rebuilding of dates from parts.

DateAdd and DateDiff. Shifting a date by months, quarters or weeks, why adding a month is cleverer than adding thirty days, and measuring gaps in the unit you actually need.

Formatting Dates for People. The Format codes worth memorising, why minutes are nn in VBA, and a firm rule for when text is the right output and when it is not.

Parsing Dates That Arrived as Text. CSV files, web exports and other people's systems deliver dates as strings. You will see when CDate can be trusted, when it guesses wrongly, and how to take the text apart yourself so day and month never swap.

Month Ends and Working Days. Month, quarter and year boundaries without special cases, weekday logic, and borrowing Excel's WORKDAY and NETWORKDAYS from VBA for due dates and service-level calculations.

Summary

Date, Time and Now read the clock, and all three return a Date: a number whose whole part counts days and whose fraction is the time of day. Declare dates As Date, read Now once per record, keep day counts in a Long, build fixed dates with DateSerial rather than literals, and write real dates into cells with a NumberFormat instead of formatted text. Now scroll back up to the lab, run StampRunLog a few times, and check with TypeName that columns A to C hold dates and column D holds text.

Frequently asked questions

What is the difference between Date, Time and Now in VBA?

Date returns today without a time, Time returns the current time without a date, and Now returns both. All three return the Date data type, so they can be compared and used in arithmetic.

How do I insert a timestamp with VBA that does not change?

Assign Now to the cell value, for example Range("A2").Value = Now, and set a NumberFormat such as "yyyy-mm-dd hh:mm:ss". Unlike the worksheet NOW() function, a value written by a macro does not recalculate.

How are dates stored in Excel VBA?

As a number: the whole part counts days and the fraction is the time of day, so noon is 0.5. For dates from March 1900 onwards this matches the serial numbers Excel uses in cells.

Why does my date column sort in the wrong order?

The cells most likely hold text, often because a macro wrote Format(...) into them. Text sorts character by character, so write the Date value itself and set the cell NumberFormat for display.

How do I get only the date part of Now in VBA?

Use Date if you want today, or DateValue(stampedAt) to strip the time from a timestamp you already have. Int(stampedAt) also works for dates after 1899 because it removes the fraction.

Is #3/4/2026# the 3rd of April or the 4th of March in VBA?

It is always 4 March 2026, because VBA date literals are read as month/day/year regardless of regional settings. DateSerial(2026, 4, 3) avoids the ambiguity completely.

Keep going: Dates & Times

This free lab is lesson 1 of 7. Unlock the other 6 hands-on labs, the final exam and your certificate for a one-time โ‚ฌ9.90.

  1. Dates Are Really Numbers
  2. Building Dates with DateSerial
  3. DateAdd and DateDiff
  4. Formatting Dates for People
  5. Parsing Dates That Arrived as Text
  6. Month Ends and Working Days

Buy course โ€” โ‚ฌ9.90 See the full course

Knowledge check

A macro stamps three cells with Now on three separate lines, and happens to run at 23:59:59. What can go wrong?

Continues in this course
  • Dates Are Really Numbers
  • Building Dates with DateSerial
  • DateAdd and DateDiff
  • Formatting Dates for People
  • Parsing Dates That Arrived as Text
  • Month Ends and Working Days

Buy the course to unlock the remaining 6 lessons, the exam and the certificate.

Buy course โ€” โ‚ฌ9.90