文本转为bytes 文本转为日期_python

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.

对业务数据进行分析通常需要使用Excel中的日期值来回答诸如“我们今天赚了多少钱”或“与上周同一天相比有多少钱?”之类的问题。 当Excel无法将值识别为日期时,这可能会很难。

Unfortunately, this is not unusual, especially when multiple users are typing this information, copying and pasting from other systems and importing from databases.

不幸的是,这并不罕见,尤其是当多个用户键入此信息,从其他系统复制和粘贴以及从数据库导入时。

In this article, we will describe four different scenarios and the solutions to convert the text to date values.

在本文中,我们将描述四种不同的情况以及将文本转换为日期值的解决方案。

(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.

初学者在Excel中键入日期时可能犯的最常见错误之一是使用句号分隔日期,月份和年份。

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不会将其识别为日期值,并将继续将其存储为文本。 但是,您可以使用“查找和替换”工具解决此问题。 通过用斜杠(/)代替句号,Excel将自动将值标识为日期。

Select the columns on which you want to perform the find and replace.

选择要在其上执行查找和替换的列。

文本转为bytes 文本转为日期_python_02

Click Home > Find & Select > Replace—or press Ctrl+H.

单击主页>查找并选择>替换-或按Ctrl + H。

文本转为bytes 文本转为日期_文本转为bytes_03

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.”

在“查找和替换”窗口中,在“查找内容”字段中键入句号(。),在“替换为”字段中键入斜杠(/)。 然后,单击“全部替换”。

文本转为bytes 文本转为日期_大数据_04

All full stops are converted to slashes and Excel recognizes the new format as a date.

所有句号都将转换为斜杠,并且Excel会将新格式识别为日期。

文本转为bytes 文本转为日期_大数据_05

If your spreadsheet data is regularly changing, and you want an automated solution for this scenario, you could use the SUBSTITUTE function.

如果您的电子表格数据有规律地更改,并且您想要针对这种情况的自动化解决方案,则可以使用SUBSTITUTE函数。

=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函数是文本函数,因此无法自行将其转换为日期。 VALUE函数会将文本值转换为数字值。

The results are shown below. The value needs to be formatted as a date.

结果如下所示。 该值需要格式化为日期。

文本转为bytes 文本转为日期_数据库_06

You can do this using the “Number Format” list on the “Home” tab.

您可以使用“主页”选项卡上的“数字格式”列表执行此操作。

文本转为bytes 文本转为日期_python_07

The example here of a full stop delimiter is typical. But you can use the same technique to replace or substitute any delimiter character.

这里以句点定界符为例。 但是您可以使用相同的技术来替换或替换任何定界符。

(Converting the yyyymmdd Format)

If you receive dates in the format shown below, it will require a different approach.

如果您收到以下所示格式的日期,则将需要使用其他方法。

文本转为bytes 文本转为日期_java_08

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.

这种格式在技术上非常标准,因为它消除了有关不同国家/地区如何存储其日期值的任何歧义。 但是,Excel最初不会理解它。

For a quick manual solution, you could use Text to Columns.

对于快速的手动解决方案,您可以使用Text to Columns 。

Select the range of values you need to convert and then click Data > Text to Columns.

选择您需要转换的值范围,然后单击数据>文本到列。

文本转为bytes 文本转为日期_python_09

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.

出现“文本到列”向导。 在前两个步骤中单击“下一步”,以便进入第三步,如下图所示。 选择日期,然后从列表中选择单元格中使用的日期格式。 在此示例中,我们正在处理YMD格式。

文本转为bytes 文本转为日期_数据库_10

If you would like a formula solution, then you could use the Date function to construct the date.

如果需要公式解决方案,则可以使用Date函数构造日期。

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.

这将与文本函数Left,Mid和Right一起使用,以从单元格内容中提取日期的三部分(日,月,年)。

The formula below shows this formula using our sample data.

以下公式使用我们的样本数据显示了此公式。

=DATE(LEFT(A2,4),MID(A2,5,2),RIGHT(A2,2))

文本转为bytes 文本转为日期_文本转为bytes_11

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.

使用这些技术中的任何一种,都可以转换任何八位数字的值。 例如,您可能会以ddmmyyyy格式或mmddyyyyy格式接收日期。

(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.

有时问题不是由定界符引起的,而是仅由于以文本形式存储而具有笨拙的日期结构。

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.

以下是各种结构中的日期列表,但我们都可以将它们识别为日期。 不幸的是,它们已被存储为文本并且需要转换。

文本转为bytes 文本转为日期_java_12

For these scenarios, it is easy to convert using a variety of techniques.

对于这些情况,可以使用多种技术轻松进行转换。

For this article, I wanted to mention two functions to handle these scenarios. They are DATEVALUE and VALUE.

对于本文,我想提到两个函数来处理这些情况。 它们是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函数会将文本转换为日期值(可能已经看到了),而VALUE函数会将文本转换为通用数字值。 它们之间的差异很小。

In the image above, one of the values contains time information as well. And that will be a demonstration of the functions’ minor differences.

在上图中,其中一个值也包含时间信息。 这将演示功能的微小差异。

The DATEVALUE formula below would convert each one to a date value.

下面的DATEVALUE公式会将每个数字转换为日期值。

=DATEVALUE(A2)

文本转为bytes 文本转为日期_大数据_13

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.

请注意,时间是如何从第4行的结果中删除的。此公式严格只返回日期值。 结果仍然需要格式化为日期。

The following formula uses the VALUE function.

以下公式使用VALUE函数。

=VALUE(A2)

文本转为bytes 文本转为日期_java_14

This formula will produce the same results except for in row 4, where the time value is also maintained.

该公式将产生相同的结果,但第4行中的时间值也保持不变。

The results can then be formatted as date and time, or as a date to hide the time value (but not remove).

然后可以将结果格式化为日期和时间,或格式化为隐藏时间值(但不能删除)的日期。

翻译自: https://www.howtogeek.com/415246/how-to-convert-text-to-date-values-in-microsoft-excel/