如何在Microsoft Excel中将文本转换为日期值
Analysis of business data often requires working with date values in Excel to answer questions such as “how much money did we make today” or “how does this compare to the same day last week?” And that can be hard when Excel doesn’t recognize the values as dates.
對(duì)業(yè)務(wù)數(shù)據(jù)進(jìn)行分析通常需要使用Excel中的日期值來回答諸如“我們今天賺了多少錢”或“與上周同一天相比有多少錢?”之類的問題。 當(dāng)Excel無法將值識(shí)別為日期時(shí),這可能會(huì)很難。
Unfortunately, this is not unusual, especially when multiple users are typing this information, copying and pasting from other systems and importing from databases.
不幸的是,這并不罕見,尤其是當(dāng)多個(gè)用戶鍵入此信息,從其他系統(tǒng)復(fù)制和粘貼以及從數(shù)據(jù)庫導(dǎo)入時(shí)。
In this article, we will describe four different scenarios and the solutions to convert the text to date values.
在本文中,我們將描述四種不同的情況以及將文本轉(zhuǎn)換為日期值的解決方案。
包含句號(hào)/日期的日期 (Dates that Contain a Full Stop/Period)
Probably one of the most common mistakes beginners make when typing dates into Excel is doing so with the full stop character to separate the day, month, and year.
初學(xué)者在Excel中鍵入日期時(shí)可能犯的最常見錯(cuò)誤之一是使用句號(hào)分隔日期,月份和年份。
Excel will not recognize this as a date value and will go ahead and store it as text. However, you can solve this problem with the Find and Replace tool. By replacing the full stops with slashes (/), Excel will automatically identify the values as dates.
Excel不會(huì)將其識(shí)別為日期值,并將繼續(xù)將其存儲(chǔ)為文本。 但是,您可以使用“查找和替換”工具解決此問題。 通過用斜杠(/)代替句號(hào),Excel將自動(dòng)將值標(biāo)識(shí)為日期。
Select the columns on which you want to perform the find and replace.
選擇要在其上執(zhí)行查找和替換的列。
Click Home > Find & Select > Replace—or press Ctrl+H.
單擊主頁>查找并選擇>替換-或按Ctrl + H。
In the Find and Replace window, type a full stop (.) in the “Find what” field and a slash (/) in the “Replace with” field. Then, click “Replace All.”
在“查找和替換”窗口中,在“查找內(nèi)容”字段中鍵入句號(hào)(。),在“替換為”字段中鍵入斜杠(/)。 然后,單擊“全部替換”。
All full stops are converted to slashes and Excel recognizes the new format as a date.
所有句號(hào)都將轉(zhuǎn)換為斜杠,并且Excel會(huì)將新格式識(shí)別為日期。
If your spreadsheet data is regularly changing, and you want an automated solution for this scenario, you could use the SUBSTITUTE function.
如果您的電子表格數(shù)據(jù)有規(guī)律地更改,并且您想要針對(duì)這種情況的自動(dòng)化解決方案,則可以使用SUBSTITUTE函數(shù)。
=VALUE(SUBSTITUTE(A2,".","/"))The SUBSTITUTE function is a text function, so cannot convert it to a date on its own. The VALUE function will convert the text value to a numeric value.
SUBSTITUTE函數(shù)是文本函數(shù),因此無法自行將其轉(zhuǎn)換為日期。 VALUE函數(shù)會(huì)將文本值轉(zhuǎn)換為數(shù)字值。
The results are shown below. The value needs to be formatted as a date.
結(jié)果如下所示。 該值需要格式化為日期。
You can do this using the “Number Format” list on the “Home” tab.
您可以使用“主頁”選項(xiàng)卡上的“數(shù)字格式”列表執(zhí)行此操作。
The example here of a full stop delimiter is typical. But you can use the same technique to replace or substitute any delimiter character.
這里以句點(diǎn)定界符為例。 但是您可以使用相同的技術(shù)來替換或替換任何定界符。
轉(zhuǎn)換yyyymmdd格式 (Converting the yyyymmdd Format)
If you receive dates in the format shown below, it will require a different approach.
如果您收到以下所示格式的日期,則將需要使用其他方法。
This format is quite standard in technology as it removes any ambiguity about how different countries store their date values.?However, Excel will not initially understand it.
這種格式在技術(shù)上非常標(biāo)準(zhǔn),因?yàn)樗擞嘘P(guān)不同國(guó)家/地區(qū)如何存儲(chǔ)其日期值的任何歧義。 但是,Excel最初不會(huì)理解它。
For a quick manual solution, you could use Text to Columns.
對(duì)于快速的手動(dòng)解決方案,您可以使用Text to Columns 。
Select the range of values you need to convert and then click Data > Text to Columns.
選擇您需要轉(zhuǎn)換的值范圍,然后單擊數(shù)據(jù)>文本到列。
The Text to Columns wizard appears. Click “Next” on the first two steps so that you are at step three, as shown in the image below.?Choose Date and then select the date format being used in the cells from the list. In this example, we are dealing with a YMD format.
出現(xiàn)“文本到列”向?qū)А?在前兩個(gè)步驟中單擊“下一步”,以便進(jìn)入第三步,如下圖所示。 選擇日期,然后從列表中選擇單元格中使用的日期格式。 在此示例中,我們正在處理YMD格式。
If you would like a formula solution, then you could use the Date function to construct the date.
如果需要公式解決方案,則可以使用Date函數(shù)構(gòu)造日期。
This would be used alongside the text functions Left, Mid and Right to extract the three parts of a date (day, month, year) from the cell contents.
這將與文本函數(shù)Left,Mid和Right一起使用,以從單元格內(nèi)容中提取日期的三部分(日,月,年)。
The formula below shows this formula using our sample data.
以下公式使用我們的樣本數(shù)據(jù)顯示了此公式。
=DATE(LEFT(A2,4),MID(A2,5,2),RIGHT(A2,2))Using either of these techniques, you can convert any eight-digit number value. For example, you might receive the date in a ddmmyyyy format or a mmddyyyy format.
使用這些技術(shù)中的任何一種,都可以轉(zhuǎn)換任何八位數(shù)字的值。 例如,您可能會(huì)以ddmmyyyy格式或mmddyyyyy格式接收日期。
DATEVALUE和VALUE函數(shù) (DATEVALUE and VALUE Functions)
Sometimes the problem is not caused by a delimiter character but has an awkward date structure simply because it is stored as text.
有時(shí)問題不是由定界符引起的,而是僅由于以文本形式存儲(chǔ)而具有笨拙的日期結(jié)構(gòu)。
Below is a list of dates in a variety of structures, but they are all recognizable to us as a date. Unfortunately, they have been stored as text and need converting.
以下是各種結(jié)構(gòu)中的日期列表,但我們都可以將它們識(shí)別為日期。 不幸的是,它們已被存儲(chǔ)為文本并且需要轉(zhuǎn)換。
For these scenarios, it is easy to convert using a variety of techniques.
對(duì)于這些情況,可以使用多種技術(shù)輕松進(jìn)行轉(zhuǎn)換。
For this article, I wanted to mention two functions to handle these scenarios. They are DATEVALUE and VALUE.
對(duì)于本文,我想提到兩個(gè)函數(shù)來處理這些情況。 它們是DATEVALUE和VALUE。
The DATEVALUE function will convert text into a date value (probably saw that coming), while the VALUE function will convert text into a generic number value. The differences between them are minimal.
DATEVALUE函數(shù)會(huì)將文本轉(zhuǎn)換為日期值(可能已經(jīng)看到了),而VALUE函數(shù)會(huì)將文本轉(zhuǎn)換為通用數(shù)字值。 它們之間的差異很小。
In the image above, one of the values contains time information as well. And that will be a demonstration of the functions’ minor differences.
在上圖中,其中一個(gè)值也包含時(shí)間信息。 這將演示功能的微小差異。
The DATEVALUE formula below would convert each one to a date value.
下面的DATEVALUE公式會(huì)將每個(gè)數(shù)字轉(zhuǎn)換為日期值。
=DATEVALUE(A2)Notice how the time was removed from the result in row 4. This formula strictly returns just the date value.?The result will still need to be formatted as a date.
請(qǐng)注意,時(shí)間是如何從第4行的結(jié)果中刪除的。此公式嚴(yán)格只返回日期值。 結(jié)果仍然需要格式化為日期。
The following formula uses the VALUE function.
以下公式使用VALUE函數(shù)。
=VALUE(A2)This formula will produce the same results except for in row 4, where the time value is also maintained.
該公式將產(chǎn)生相同的結(jié)果,但第4行中的時(shí)間值也保持不變。
The results can then be formatted as date and time, or as a date to hide the time value (but not remove).
然后可以將結(jié)果格式化為日期和時(shí)間,或格式化為隱藏時(shí)間值(但不能刪除)的日期。
翻譯自: https://www.howtogeek.com/415246/how-to-convert-text-to-date-values-in-microsoft-excel/
總結(jié)
以上是生活随笔為你收集整理的如何在Microsoft Excel中将文本转换为日期值的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Microsoft Desktop Pl
- 下一篇: 看着手机会让您晕眩吗? 禁用动画