目录
YEAR function in Excel
Excel YEAR formula
Using the DATE function
As a serial number representing the date
As a cell reference
As a result of some other formula
As text
How to convert date to year in Excel
Example 1. Extract a year from date using the YEAR function
Example 2. Convert date to month and year in Excel
Example 3. Display a date as a year
How to calculate age from date of birth in Excel
Calculate age from date of birth in years
Calculating exact age from date of birth (in years, month and days)
How to get the day number of the year (1-365)
How to calculate the number of days remaining in the year
Calculating leap years in Excel
Formula 1. Check if February has 29 days
Formula 2. Check if the year has 366 days
首页 软件教程 办公软件 Excel年功能 - 将日期转换为年份

Excel年功能 - 将日期转换为年份

May 08, 2025 am 09:19 AM

This tutorial explains the syntax and uses of the Excel YEAR function and provides formula examples to extract year from date, convert date to month and year, calculate age from the date of birth and determine leap years.

In a few recent posts, we have explored different ways to calculate dates and times in Excel and learned a variety of useful functions such as WEEKDAY, WEEKNUM, MONTH, and DAY. Today, we are going to focus on a bigger time unit and talk about calculating years in your Excel worksheets.

In this tutorial, you will learn:

YEAR function in Excel

The YEAR function in Excel returns a four-digit year corresponding to a given date as an integer from 1900 to 9999.

The syntax of the Excel YEAR function is as simple as it could possibly be:

YEAR(serial_number)

Where serial_number is any valid date of the year you want to find.

Excel YEAR formula

To make a YEAR formula in Excel, you can supply the source date in several ways.

Using the DATE function

The most reliable way to supply a date in Excel is using the DATE function.

For example, the following formula returns the year for 28 April, 2015:

=YEAR(DATE(2015, 4, 28))

As a serial number representing the date

In the internal Excel system, dates are stored as serial numbers beginning with 1 January 1900, which is stored as number 1. For more information on how dates are stored in Excel, please see Excel date format.

The 28 day of April, 2015 is stored as 42122, so you can enter this number directly in the formula:

=YEAR(42122)

Though acceptable, this method is not recommended because date numbering may vary across different systems.

As a cell reference

Assuming you have a valid date in some cell, you can simply refer to that cell. For example:

=YEAR(A1)

As a result of some other formula

For example, you can use the TODAY() function to extract the year from the current date:

=YEAR(TODAY())

As text

In a simple case, the YEAR formula can even understand dates entered as text, like this:

=YEAR("28-Apr-2015")

When using this method, please double check that you enter the date in the format that Excel understands. Also, please remember that Microsoft does not guarantee correct results when a date is supplied as a text value.

The following screenshot demonstrates all of the above YEAR formulas in action, all returning 2015 as you might expect :)

Excel年功能 - 将日期转换为年份

How to convert date to year in Excel

When you work with date information in Excel, your worksheets usually display full dates, including month, day and year. However, for major milestones and important events such as product launches or asset acquisitions, you may want to view only the year without re-entering or modifying the original data. Below, you will find 3 quick ways to do this.

Example 1. Extract a year from date using the YEAR function

In fact, you already know how to use the YEAR function in Excel to convert a date to a year. The screenshot above demonstrates a bunch of formulas, and you can see a few more examples in the screenshot below. Notice that the YEAR function perfectly understands dates in all possible formats:

Excel年功能 - 将日期转换为年份

Example 2. Convert date to month and year in Excel

To convert a given date to year and month, you can use the TEXT function to extract each unit individually, and then concatenate those functions within one formula.

In the TEXT function, you can use different codes for months and years, such as:

  • "mmm" - abbreviated months names, as Jan - Dec.
  • "mmmm" - full month names, as January - December.
  • "yy" - 2-digit years
  • "yyyy" - 4-digit years

To make the output better readable, you can separate the codes with a comma, hyphen or any other character, like in the following Date to Month and Year formulas:

=TEXT(B2, "mmmm") & ", " & TEXT(B2, "yyyy")

Or

=TEXT(B2, "mmm") & "-" & TEXT(B2, "yy")

Where B2 is a cell containing a date.

Excel年功能 - 将日期转换为年份

Example 3. Display a date as a year

If it does not really matter how the dates are stored in your workbook, you can get Excel to show only the years without changing the original dates. In other words, you can have full dates stored in cells, but only the years displayed.

In this case, no formula is needed. You just open the Format Cells dialog by pressing Ctrl + 1, select the Custom category on the Number tab, and enter one of the below codes in the Type box:

  • yy - to display 2-digit years, as 00 - 99.
  • yyyy - to display 4-digit years, as 1900 - 9999.

Excel年功能 - 将日期转换为年份

Please remember that this method does not change the original date, it only changes the way the date is displayed in your worksheet. If you refer to such cells in your formulas, Microsoft Excel will perform date calculations rather than year calculations.

You can find more details about changing the date format in this tutorial: How to change date format in Excel.

How to calculate age from date of birth in Excel

There are several ways to calculate age form date of birth in Excel - using DATEDIF, YEARFRAC or INT function in combination with TODAY(). The TODAY function supplies the date to calculate age at, ensuring that your formula will always return the correct age.

Calculate age from date of birth in years

The traditional way to calculate a person's age in years is to subtract the birth date from the current date. This approach works fine in everyday life, but an analogous Excel age calculation formula is not perfectly true:

INT((TODAY()-DOB)/365)

Where DOB is the date of birth.

The first part of the formula (TODAY()-B2) calculates the difference is days, and you divide it by 365 to get the number of years. In most cases, the result of this equation is a decimal number, and you have the INT function round it down to the nearest integer.

Assuming the date of birth is in cell B2, the complete formula goes as follows:

=INT((TODAY()-B2)/365)

Excel年功能 - 将日期转换为年份

As mentioned above, this age calculation formula is not always flawless, and here's why. Every 4th year is a leap year that contains 366 days, whereas the formula divides the number of days by 365. So, if someone was born on February 29 and today is February 28, this age formula will make a person one day older.

Dividing by 365.25 instead of 365 is not impeccable either, for example, when calculating the age of a child who hasn't yet lived through a leap year.

Given the above, you'd better save this way of calculating age for normal life, and use one of the following formulas to calculate age from date of birth in Excel.

DATEDIF(DOB, TODAY(), "y") ROUNDDOWN(YEARFRAC(DOB, TODAY(), 1), 0)

The detailed explanation of the above formulas is provided in How to calculate age in Excel. And the following screenshot demonstrates a real-life age calculation formula in action:

=DATEDIF(B2, TODAY(), "y")

Excel年功能 - 将日期转换为年份

Calculating exact age from date of birth (in years, month and days)

To calculate an exact age in years, months and days, write three DATEDIF functions with the following units in the last argument:

  • Y - to calculate the number of complete years.
  • YM - to get the difference between the months, ignoring years.
  • MD - to get the difference between the days, ignoring years and months.

And then, concatenate the 3 DATEDIF functions in a single formula, separate the numbers returned by each function with commas, and define what each number means.

Assuming the date of birth is in cell B2, the complete formula goes as follows:

=DATEDIF(B2,TODAY(),"Y") & " Years, " & DATEDIF(B2,TODAY(),"YM") & " Months, " & DATEDIF(B2,TODAY(),"MD") & " Days"

This age formula may come in very handy, say, for a doctor to display the exact age of patients, or for a personnel officer to know the exact age of all employees:

Excel年功能 - 将日期转换为年份

For more formula examples such as calculating age at a particular date or in a certain year, please check out the following tutorial: How to calculate age in Excel.

How to get the day number of the year (1-365)

This example demonstrates how you can get the number of a certain day in a year, between 1 and 365 (1-366 in leap years) with January 1 considered day 1.

For this, use the YEAR function together with DATE in this way:

=A2-DATE(YEAR(A2), 1, 0)

Where A2 is a cell containing the date.

Excel年功能 - 将日期转换为年份

And now, let's see what the formula actually does. The YEAR function retrieves the year of the date in cell A2, and passes it to the DATE(year, month, day) function, which returns the sequential number that represents a certain date.

So, in our formula, year is extracted from the original date (A2), month is 1 (January) and day is 0. In fact, a zero day forces Excel to return December 31 of the previous year, because we want January 1 to be treated as the 1st day. And then, you subtract the serial number returned by the DATE formula from the original date (which is also stored as a serial number in Excel) and the difference is the day of the year you are looking for. For example, January 5, 2015 is stored as 42009 and December 31, 2014 is 42004, so 42009 - 42004 = 5.

If the concept of day 0 does not seem right to you, you can use the following formula instead:

=A2-DATE(YEAR(A2), 1, 1)+1

How to calculate the number of days remaining in the year

To compute the number of days remaining in the year, we are going to use the DATE and YEAR functions again. The formula is based on the same approach as Example 3 above, so you are unlikely to have any difficulties with understanding its logic:

=DATE(YEAR(A2),12,31)-A2

Excel年功能 - 将日期转换为年份

If you want to know how many days remain till the end of the year based on the current date, you use the Excel TODAY() function, as follows:

=DATE(2015, 12, 31)-TODAY()

Where 2015 is the current year.

Calculating leap years in Excel

As you know, nearly every 4th year has an extra day on February 29 and is called a leap year. In Microsoft Excel sheets, you can determine whether a certain date belongs to a leap year or a common year in a variety of ways. I'm going to demonstrate just a couple of formulas, which in my opinion are easiest to understand.

Formula 1. Check if February has 29 days

This is a very obvious test. Since February has 29 days in leap years, we calculate the number of days in month 2 of a given year and compare it with number 29. For example:

=DAY(DATE(2015,3,1)-1)=29

In this formula, the DATE(2015,3,1) function returns the 1st day of March in the year 2015, from which we subtract 1. The DAY function extracts the day number from this date, and we compare that number with 29. If the numbers match, the formula returns TRUE, FALSE otherwise.

If you already have a list of dates in your Excel worksheet and you want to know which ones are leap years, then incorporate the YEAR function in the formula to extract a year from a date:

=DAY(DATE(YEAR(A2),3,1)-1)=29

Where A2 is a cell containing the date.

The results returned by the formula are as follows:

Excel年功能 - 将日期转换为年份

Alternatively, you can use the EOMONTH function to return the last day in February, and compare that number with 29:

=DAY(EOMONTH(DATE(YEAR(A2),2,1),0))=29

To make the formula more user-friendly, employ the IF function and have it return, say, "Leap year" and "Common year" instead of TRUE and FALSE:

=IF(DAY(DATE(YEAR(A2),3,1)-1)=29, "Leap year", "Common year")

=IF(DAY(EOMONTH(DATE(YEAR(A2),2,1),0))=29, "Leap year", "Common year")

Excel年功能 - 将日期转换为年份

Formula 2. Check if the year has 366 days

This is another obvious test that hardly requires any explanation. We use one DATE function to return 1-Jan of the next year, another DATE function to get 1-Jan of this year, subtract the latter from the former and check if the difference equals to 366:

=DATE(2016,1,1) - DATE(2015,1,1)=366

To calculate a year based on a date entered in some cell, you use the Excel YEAR function exactly in the same way as we did in the previous example:

=DATE(YEAR(A2)+1,1,1) - DATE(YEAR(A2),1,1)=366

Where A2 is a cell containing the date.

And naturally, you can enclose the above DATE / YEAR formula in the IF function for it to return something more meaningful than the Boolean values of TRUE and FALSE:

=IF(DATE(YEAR(A2)+1,1,1) - DATE(YEAR(A2),1,1)=366, "Leap year", "Non-leap year")

Excel年功能 - 将日期转换为年份

As already mentioned, these are not the only possible ways to calculate leap years in Excel. If you are curious to know other solutions, you can check the method suggested by Microsoft. As usual, Microsoft guys are not looking for easy ways, are they?

Hopefully, this article has helped you figure out year calculations in Excel. I thank you for reading and look forward to seeing you next week.

以上是Excel年功能 - 将日期转换为年份的详细内容。更多信息请关注PHP中文网其他相关文章!

本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

<🎜>:泡泡胶模拟器无穷大 - 如何获取和使用皇家钥匙
3 周前 By 尊渡假赌尊渡假赌尊渡假赌
北端:融合系统,解释
3 周前 By 尊渡假赌尊渡假赌尊渡假赌
Mandragora:巫婆树的耳语 - 如何解锁抓钩
3 周前 By 尊渡假赌尊渡假赌尊渡假赌

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

热门话题

Java教程
1666
14
CakePHP 教程
1425
52
Laravel 教程
1325
25
PHP教程
1273
29
C# 教程
1252
24
如果您不在Excel中重命名桌子,那么今天是开始的一天 如果您不在Excel中重命名桌子,那么今天是开始的一天 Apr 15, 2025 am 12:58 AM

快速链接 为什么应该在 Excel 中命名表格 如何在 Excel 中命名表格 Excel 表格命名规则和技巧 默认情况下,Excel 中的表格命名为 Table1、Table2、Table3,依此类推。但是,您不必坚持使用这些标签。事实上,如果您不这样做会更好!在本快速指南中,我将解释为什么您应该始终重命名 Excel 中的表格,并向您展示如何操作。 为什么应该在 Excel 中命名表格 虽然养成在 Excel 中命名表格的习惯可能需要一些时间(如果您通常不这样做的话),但以下原因说明了今

如何更改Excel表样式并删除表格格式 如何更改Excel表样式并删除表格格式 Apr 19, 2025 am 11:45 AM

本教程向您展示了如何在保留所有表功能的同时快速应用,修改和删除Excel表样式。 想让您的Excel桌子看起来完全想要吗?继续阅读! 创建Excel表之后,第一步是通常

Excel匹配功能与公式示例 Excel匹配功能与公式示例 Apr 15, 2025 am 11:21 AM

本教程解释了如何在公式示例中使用excel中的匹配函数。它还显示了如何通过使用Vlookup和匹配的动态公式来改善查找公式。 在Microsoft Excel中,有许多不同的查找/参考

Excel:比较两个单元格中的字符串以进行匹配(对病例不敏感或精确) Excel:比较两个单元格中的字符串以进行匹配(对病例不敏感或精确) Apr 16, 2025 am 11:26 AM

该教程显示了如何比较Excel中的文本字符串,以了解不敏感和确切的匹配。您将学习许多公式,以通过其值,字符串长度或特定字符的出现数量来比较两个单元格

如何使所有人都可以访问Excel电子表格 如何使所有人都可以访问Excel电子表格 Apr 18, 2025 am 01:06 AM

提升Excel表格的可访问性:实用指南 创建Microsoft Excel工作簿时,务必采取必要的步骤,确保每个人都能访问它,尤其是在您计划与他人共享工作簿的情况下。本指南将分享一些实用技巧,帮助您实现这一目标。 使用描述性工作表名称 提高Excel工作簿可访问性的一种方法是更改工作表的名称。默认情况下,Excel工作表命名为Sheet1、Sheet2、Sheet3等等,这种非描述性的编号系统在您点击“ ”添加新工作表时会继续下去。 更改工作表名称使其更准确地描述工作表内容具有多重好处: 提

不要忽略Microsoft Excel中F4的功能 不要忽略Microsoft Excel中F4的功能 Apr 24, 2025 am 06:07 AM

Excel高手必备:F4键的妙用,提升效率的秘密武器! 本文将揭秘Windows系统下Microsoft Excel中F4键的强大功能,助你快速掌握这个提升生产力的快捷键。 一、公式引用类型切换 Excel中的引用类型包括相对引用、绝对引用和混合引用。F4键可以方便地在这些类型之间切换,尤其在创建公式时非常实用。 假设你需要计算七种产品的价格,并加上20%的税。 在单元格E2中,你可能输入以下公式: =SUM(D2 (D2*A2)) 按Enter键后,即可计算出包含20%税的价格。 但是,如

如何使用Excel的汇总函数来完善计算 如何使用Excel的汇总函数来完善计算 Apr 12, 2025 am 12:54 AM

快速链接汇总语法

为什么您应该始终在Excel中重命名工作表 为什么您应该始终在Excel中重命名工作表 Apr 17, 2025 am 12:56 AM

提升Excel工作效率:高效命名工作表指南 本文将指导您如何有效命名Excel工作表,提升工作效率并增强可访问性。 清晰的工作表名称能显着改善导航、组织和跨表引用。 为什么要重命名Excel工作表? 使用默认的“Sheet1”、“Sheet2”等名称效率低下,尤其在包含多个工作表的文件中。 更清晰的名称,例如“仪表盘”、“销售额”和“预测”,能让您和其他人一目了然地了解工作簿内容,并快速找到所需工作表。 使用描述性名称(例如“Dashboard”、“Sales”、“Forecast”)可以

See all articles