Sub topic 4
Pulling out year, month, day, hour, minute and second
This chapter assumes you can enter formulas, use relative and absolute cell references, and apply number formats from the Format menu. It also assumes you know that Sheets stores a date as a serial number, a count of days since 1899-12-30, with the time of day as a fraction of a day. Extraction functions read that number and return one part of it as an ordinary number.
What extraction functions do
Six functions each return one component of a date-time value:
| Function | Returns | Range |
|---|---|---|
| `YEAR(date)` | The year | 1900 and later in practice |
| `MONTH(date)` | The month number | 1 to 12 |
| `DAY(date)` | The day of the month | 1 to 31 |
| `HOUR(time)` | The hour | 0 to 23 |
| `MINUTE(time)` | The minute | 0 to 59 |
| `SECOND(time)` | The second | 0 to 59 |
Each function returns a plain number, not a date. A cell holding =MONTH(A2) shows 11 for a date in November. It does not show November. If you need the month name, format the original date or use TEXT, which a later chapter covers.
Suppose A2 holds the date-time 2026-11-28 14:35:48. The results are:
| Formula | Result |
|---|---|
| `=YEAR(A2)` | 2026 |
| `=MONTH(A2)` | 11 |
| `=DAY(A2)` | 28 |
| `=HOUR(A2)` | 14 |
| `=MINUTE(A2)` | 35 |
| `=SECOND(A2)` | 48 |
The functions accept any value that Sheets can treat as a date or time. The date functions ignore the time portion, and the time functions ignore the date portion. HOUR applied to a cell that holds only a date returns 0, because a date with no time is midnight.
Using the parts: rebuilding dates
The most common use of extraction is to take a date apart and put it back together with DATE. This lets you change one component while keeping the others.
=DATE(YEAR(A2), MONTH(A2), 1)This returns the first day of the month of the date in A2. DATE builds a real date from three numbers, so the result is a date and you can format it like any other.
You can also move a date to the same day in a different year:
=DATE(YEAR(A2) + 1, MONTH(A2), DAY(A2))This formula has an edge case. For a date of 2028-02-29, adding one year gives DATE(2029, 2, 29). That day does not exist. DATE does not return an error. It rolls the extra days forward and returns 2029-03-01. Whenever you rebuild a date from extracted parts, think about what happens when the day number is too large for the target month.
DATE rolls over out-of-range arguments in both directions. A month of 13 becomes January of the next year. A day of 0 becomes the last day of the previous month. You can use that second behavior on purpose:
=DATE(YEAR(A2), MONTH(A2) + 1, 0)This returns the last day of the month of A2, because day 0 of the next month is the last day of the current one. EOMONTH, covered in a later chapter, does the same job with less work.
Grouping and filtering by part
Extracted parts are useful as keys. If a sheet has one row per transaction, you can add helper columns to group rows by year or month.
| Column | Header | Formula in row 2 |
|---|---|---|
| A | Date | (entered value) |
| B | Year | `=YEAR(A2)` |
| C | Month | `=MONTH(A2)` |
| D | Amount | (entered value) |
With those helper columns you can total one month of one year with SUMIFS:
=SUMIFS(D2:D, B2:B, 2026, C2:C, 11)Always include the year in the key. If you group by month number alone, November 2025 and November 2026 are added into the same total.
A single helper column can combine both parts as a number, which is easier to sort and compare:
=YEAR(A2) * 100 + MONTH(A2)For November 2026 this gives 202611. It sorts in calendar order, and you can compare it with >= and <=.
Extracting time parts
HOUR, MINUTE and SECOND read the fractional part of the serial number. Take 14:35:48. As a fraction of a day this is (14 × 3600 + 35 × 60 + 48) / 86400, or about 0.6082.
You can use the time parts to classify rows. For example, this labels each timestamp in A2 with a shift:
=IFS(HOUR(A2) < 6, "Night", HOUR(A2) < 14, "Morning", HOUR(A2) < 22, "Evening", TRUE, "Night")Hours 0 to 5 are Night, 6 to 13 are Morning, 14 to 21 are Evening, and 22 and 23 fall through to the final TRUE branch and return Night.
To convert a time into a decimal number of hours, combine the three parts:
=HOUR(A2) + MINUTE(A2) / 60 + SECOND(A2) / 3600For 14:35:48 this gives about 14.5967. You can get the same value with =MOD(A2, 1) * 24, which keeps only the fractional part of the serial number and scales it to hours. The MOD version is shorter but depends on the cell holding a real number, and floating-point rounding can leave tiny errors in the last digits.
Time parts are not durations
A common mistake is to use HOUR on a duration. Suppose A2 holds a start time of 08:00 and B2 holds an end time of 21:30 on the next day, as date-times. The difference =B2 - A2 is 37.5 hours, or 1.5625 days. Now =HOUR(B2 - A2) returns 13, not 37. HOUR returns the hour of the day for the fractional part, so it discards whole days.
To get total elapsed hours, multiply the difference by 24:
=(B2 - A2) * 24Use HOUR, MINUTE and SECOND to read the clock time of a timestamp. Use arithmetic to measure elapsed time. The chapter on time of day and durations goes further.
Rounding and fractional seconds
The SECOND function returns whole seconds. It truncates, so a value of 14:35:48.9 returns 48, not 49. If a timestamp comes from a system that records milliseconds, the sheet keeps the fraction in the serial number, but SECOND hides it.
Floating-point storage can cause a related problem. A time typed as 12:00:00 is stored as exactly 0.5. A time computed by adding fractions, such as =TIME(0, 0, 1) * 3600, may land a tiny amount below the intended value. When SECOND truncates that, it can return 59 for a time you expected to show as 0. If a computed time gives a surprising second or minute, round first:
=SECOND(ROUND(A2 * 86400, 0) / 86400)This rounds the value to the nearest whole second before extracting it.
Applying extraction to a whole column
To extract the month from every row without copying the formula down, wrap it in ARRAYFORMULA. Handle blank rows so they do not show 12 for the month of an empty cell. An empty cell counts as 0, which is the date 1899-12-30, so MONTH of an empty cell returns 12.
=ARRAYFORMULA(IF(A2:A = "", "", MONTH(A2:A)))The IF check keeps blank rows empty. Without it, every blank row below your data would display 12.
Common problems
- The result is a date, not a number. Sheets sometimes applies a date format to the result cell because the formula references a date. If
=YEAR(A2)shows1905-07-06instead of2026, the cell has a date format applied. Change it with Format, then Number, then Automatic or Number. - The result is
#VALUE!. The argument is text that Sheets cannot read as a date. Check whether the cell is left-aligned, which usually means text, and convert it withDATEVALUE. - The month is right but the year is 1899 or 1900. The source cell holds a time with no date. Times alone are stored with a date part of 0.
- Two-digit years give odd results. An entry such as
11/28/26depends on how the spreadsheet locale reads two-digit years. Enter four-digit years or build the date withDATE.
How the pieces connect
Exercises
Exercise
Cell A2 holds the date 2026-11-28. Write a formula that returns the first day of the following month as a real date, and say what it returns.
Show answer
Use =DATE(YEAR(A2), MONTH(A2) + 1, 1). It returns 2026-12-01. The month argument of 12 is valid here. If A2 were in December, the month argument would be 13, and DATE would roll it over to January of the next year.
Exercise
Cell A2 holds a start timestamp of 2026-11-27 20:00 and B2 holds an end timestamp of 2026-11-29 02:30. A colleague writes =HOUR(B2 - A2) and gets 6. What is the correct number of elapsed hours, and which formula gives it?
Show answer
The elapsed time is 30.5 hours. Use =(B2 - A2) * 24. HOUR(B2 - A2) returns only the hour within the final partial day, so it discards the 24 whole hours (one whole day). The difference is 1.2708 days, and HOUR reads only the 0.2708 fraction, which is 6 hours.
Exercise
A sheet has dates in A2:A and amounts in D2:D. Write a formula that totals the amounts for November 2026 without helper columns.
Show answer
Use =SUMPRODUCT((YEAR(A2:A) = 2026) (MONTH(A2:A) = 11) D2:D). Blank rows in A give year 1899, so they do not match, and blank cells in D count as 0. If the range has text headers or other text in D, wrap D2:D in N() or restrict the range to numeric rows.