How to Remove Milliseconds from Timestamps in Google Sheets

Published on

Removing milliseconds from timestamps is an important technique to be aware of in Google Sheets, as it can help troubleshoot formula errors.

For instance, if you used keyboard shortcuts to insert static timestamps in Google Sheets, they might appear like timestamps without milliseconds.

However, if you select those cells and apply DD/MM/YYYY HH:mm:ss.000 in Format > Number > Custom number format, you will reveal the actual underlying values.

As a result, issues may arise when performing lookups or using other criteria that involve matching in the timestamp column.

Your criterion or search key may be a timestamp in the formula, such as DD/MM/YYYY HH:mm:ss, without milliseconds, to find an exact match. However, the search column will contain timestamps with milliseconds.

In this walkthrough, you can learn how to remove milliseconds from timestamps in a column in Google Sheets. For that, we will use both array and non-array formulas.

We will use two types of formulas.

The first formula will simply truncate the milliseconds and return the timestamp without milliseconds. For example, if the timestamp is 13/08/2023 08:47:08.704, the formula will return 13/08/2023 08:47:08.

The second one will round up the seconds. For example, if the timestamp is 13/08/2023 08:47:08.704, the formula will return 13/08/2023 08:47:09. If the milliseconds are less than 500, the formula doesn’t change the seconds’ value; otherwise, it adds 1 to the seconds’ value.

You can choose the formula you prefer.

Removing Milliseconds from Timestamps in Google Sheets

If you want to truncate the milliseconds from timestamps, follow this approach.

Assume you have the timestamp in cell A2. You can use the following formula in any other cell to remove the milliseconds without making any other changes to the timestamp.

=A2-QUERY(A2, "Select millisecond(A) label millisecond(A)''", 0)/86400000

Note: You may need to format the result of this formula and the formulas below to the timestamp format using Format > Number > Date time.

The QUERY function is used to extract the milliseconds part of the timestamp in cell A2.

For instance, if the value is 13/08/2023 08:47:08.704, the formula will return 704. To remove the milliseconds from the timestamp in cell A2, it’s necessary to convert this value into the equivalent fraction of a day.

Therefore, the extracted milliseconds value is divided by 86400000, which represents the number of milliseconds in a day. Subtracting this calculated fraction from the original timestamp results in a timestamp without milliseconds.

In summary, this formula effectively removes the milliseconds from the given timestamp in cell A2.

Can we use this in an array formula to remove milliseconds from a list in Google Sheets?

Absolutely! Here you go!

=ArrayFormula(
     IF(A2:A="", ,
     A2:A-QUERY(A2:A, "Select millisecond(A) label millisecond(A)''", 0)/86400000)
)
Array formula removing milliseconds in Google Sheets

The ArrayFormula is used to apply the subtraction, division, and the IF logical test to the entire column.

The IF logical test ensures that the formula returns a blank in rows where there is no value in the list column (A).

Adjust Timestamps to the Nearest Second based on Milliseconds

Some of you may want to remove the milliseconds but round the seconds part. How do we adjust the timestamps to the nearest second based on milliseconds in Google Sheets?

For the timestamp in cell A2, you can use either of the formulas below:

=DATEVALUE(A2) + TIMEVALUE(TEXT(A2, "HH:mm:ss"))
=DATEVALUE(A2)+TIME(HOUR(A2), MINUTE(A2), SECOND(A2))

In both formulas, the DATEVALUE function returns the date value, while the TIME/TIMEVALUE functions return the time component.

In the first formula, the TEXT function is utilized to format the time as HH:mm:ss. The TIMEVALUE function then converts this time string into a time value. It’s important to note that removing milliseconds effectively rounds the seconds.

In the second formula, we use the HOUR, MINUTE, and SECONDS functions to retrieve the corresponding time components. The TIME function then converts those values into a time, and the SECONDS function is employed to round the seconds.

If the date in cell A2 is 05/10/2023 08:13:56.141, it will return 05/10/2023 08:13:56, truncating the milliseconds as they are below 500.

If the date in cell A2 is 19/09/2023 09:46:14.695, it will return 19/09/2023 09:46:15, rounding up the second.

You can use this in an array formula as well. Here is an example using the timestamps in A2:A:

=ArrayFormula(IFERROR(DATEVALUE(A2:A) + TIMEVALUE(TEXT(A2:A, "HH:mm:ss"))))
=ArrayFormula(IFERROR(DATEVALUE(A2:A)+TIME(HOUR(A2:A), MINUTE(A2:A), SECOND(A2:A))))
Adjust Timestamps to the Nearest Second based on Milliseconds - Google Sheets

Here, ArrayFormula is used to support all the functions in use.

The IFERROR is employed to handle errors returned by DATEVALUE in blank cells. In blank cells, DATEVALUE returns an error, and we convert those errors to blanks using IFERROR. Therefore, the IF logic is not necessary.

Conclusion

We have explored two relatively simple solutions to remove milliseconds from timestamps in Google Sheets.

One truncates the timestamps, and the other removes the milliseconds but rounds the seconds based on the milliseconds.

Here are some resources about milliseconds in Google Sheets.

  1. How to Extract Date From Timestamp in Google Sheets
  2. How to Filter Timestamp in Query in Google Sheets
  3. Extract the Earliest or Latest Record in Each Category Based on Timestamp in Google Sheets
  4. How to Convert a Timestamp to Milliseconds in Google Sheets
  5. Convert Unix Timestamp to Local DateTime and Vice Versa in Google Sheets
  6. Vlookup Date in Timestamp Column in Google Sheets
  7. How to Use Timestamp within IF Logical Function in Google Sheets
  8. How to Insert a Static Timestamp in Google Sheets
Prashanth KV
Prashanth KV
Your Trusted Google Sheets and Excel Guide Prashanth KV brings a wealth of experience in Google Sheets and Excel, cultivated through years of work with multinational corporations in Mumbai and Dubai. As a recognized Google Product Expert in Docs Editors, Prashanth shares his expertise through insightful blogging since 2012. Explore his blog for practical tips and guidance on maximizing your spreadsheet skills.

Lookup Values Under Categories in Excel and Google Sheets

We can use a combination of XLOOKUP and VLOOKUP in both Excel and Google...

Extract Items Under a Category from the Same Column in Excel

In Excel, you can use a combination of the XLOOKUP and DROP functions to...

How to Create a Searchable Table in Excel Using the FILTER Function

Finding specific records, or rows containing the required information, is straightforward in Excel using...

Time Sequences in Excel by Minute, Hour, or Second Increments

Creating time sequences, whether by hour, minute, or second increments in Excel, can be...

More like this

XMATCH Row by Row: Finding Values Across a Range in Google Sheets

Using the BYROW function with XMATCH in Google Sheets allows us to match values...

Limit Formula Expansion to a Specific Row in Google Sheets

In this tutorial, I’ll explain how to limit the expansion of an array formula...

3-D Referencing Structured Data Tables in Google Sheets

When you have several tables within a single sheet—not across multiple sheets in a...

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.