简体   繁体   English

如何在Excel中将文本转换为日期格式

[英]How to convert text to date format in Excel

I need to sort this column chronologically, however when I import the data, some of the cells are recognized as aa custom format (A2-A4) while others are in a general format (A5-11). 我需要按时间顺序对此列进行排序,但是,当我导入数据时,某些单元格会被识别为自定义格式(A2-A4),而其他单元格则会被视为常规格式(A5-11)。 A2-A4 is recognized as a date, however A5-A11 is being recognized as general text. A2-A4被识别为日期,但是A5-A11被识别为通用文本。 Excel is not recognizing that these should all be dates. Excel无法识别这些都应该是日期。 I have tried formatting them as a date, but this does not work. 我曾尝试将它们格式化为日期,但这不起作用。 I have used text to columns to separate the time from the data, however cells such as A2-A4 leave behind a time of midnight in the cell (eg 2/10/2012 0:00:00) and I still can't get the cells to appear as dates when I use text to columns. 我已经使用文本在列中将时间与数据分开,但是像A2-A4这样的单元格在单元格中留下了午夜的时间(例如2012年2月10日0:00:00),但我仍然无法获得当我使用文本到列时,这些单元格将显示为日期。 I have cleared formatting, I have tried format painter, I have tried =datevalue. 我已经清除了格式设置,尝试了格​​式绘制器,尝试了= datevalue。 I'm not sure what else to do. 我不确定该怎么办。 Also, I noticed that the cells where Excel recognizes the values as dates, the time is listed in 24hr time, where as the others are not. 另外,我注意到Excel会将值识别为日期的单元格,时间以24小时制列出,而其他单元格则没有。

Can someone please help? 有人可以帮忙吗?

text looks like this: 文字看起来像这样:

Column A
2    2/10/2012 20:00
3    1/11/2012 3:00
4    5/12/2013 15:30
5    19/10/2012 2:00 pm
6    18/04/2013 2:00 pm
7    18/10/2013 2:30 pm
8    23/12/2013 9:00 am
9    31/05/2013 1:00 pm
10   16/02/2013 6:00 pm
11   15/02/2013 6:00 pm

I'll assume that, for some reason, adjusting the input data feed so that all the dates follow the convention assumed by Excel is impractical, because that's probably the easiest solution to your problem. 由于某种原因,我将假设调整输入数据源以使所有日期都遵循Excel假定的惯例是不切实际的,因为这可能是解决问题的最简单方法。

For the cells that are stored as text, I assume we know the format of the date that's being read (ie DD/MM/YYYY) and that it'll remain constant. 对于存储为文本的单元格,我假设我们知道读取日期的格式(即DD / MM / YYYY),并且它将保持不变。 For a string like DD/MM/YYYY HH:MM PM , we can then do the following: 对于像DD/MM/YYYY HH:MM PM这样的字符串,我们可以执行以下操作:

(1) The day must be stored in the characters before the first slash, so we can extract it by writing: (1)必须将日期存储在第一个斜杠之前的字符中,因此我们可以通过以下方式提取它:

DAY_FORMULA: =LEFT(A1, FIND("/", A1, 1)-1)

(2) The month must be stored in the characters between the first and the second slash. (2)月份必须存储在第一个和第二个斜杠之间的字符中。 It occupies either one or two characters, so we can write the following: 它占用一个或两个字符,因此我们可以编写以下代码:

MONTH_FORMULA: =MID(A1, FIND("/", A1, 1) + 1, x)

Here, FIND("/", A1, 1) + 1 tells Excel to return the location of the first / : since the month starts after that, we add one. 在这里, FIND("/", A1, 1) + 1告诉Excel返回第一个/的位置,因为该月之后开始,所以我们添加一个。 Now we need to find x , the number of characters to read from that point. 现在我们需要找到x ,即从该点开始读取的字符数。 Since the first / is at FIND("/", A1, 1) and the second is at FIND("/", A1, FIND("/", A1, 1)+1) , then the number of characters between the two slashes is given by: 由于第一个/FIND("/", A1, 1) ,第二个在FIND("/", A1, FIND("/", A1, 1)+1) ,则之间的字符数两个斜杠由:

FIND("/", A1, FIND("/", A1, 1)+1) - FIND("/", A1, 1) - 1 . FIND("/", A1, FIND("/", A1, 1)+1) - FIND("/", A1, 1) - 1

The complete MONTH_FORMULA can be written as: 完整的MONTH_FORMULA可以写为:

MONTH_FORMULA: =MID(A1, FIND("/", A1, 1)+1, FIND("/", A1, FIND("/", A1, 1)+1)-FIND("/", A1, 1)-1)

(3) The year is contained in the last four digits after the second slash, so we can write the year as: (3)年号包含在第二个斜杠后的最后四位数字中,因此我们可以将年写为:

YEAR_FORMULA: =MID(A1, FIND("/", A1, FIND("/", A1, 1)+1)+1, 4)

You can then use the standard date formula to force these into a date format: 然后,您可以使用标准日期公式将其强制为日期格式:

=DATE(YEAR_FORMULA, MONTH_FORMULA, DAY_FORMULA

(4) If you want to keep information about the hours, then if no leading blanks exist in the fields (not that this couldn't be circumvented with a =TRIM ), you can use: (4)如果您想保留有关小时的信息,那么如果字段中不存在任何前导空格(不是=TRIM不能避免的),则可以使用:

TIME_FORMULA: =TIMEVALUE(MID(A1, FIND(" ", A1, 1)+1, LEN(A1)))

You can then add the date to the time value to obtain the full date-time: 然后,您可以将日期添加到时间值中以获得完整的日期时间:

=DATE(YEAR_FORMULA, MONTH_FORMULA, DAY_FORMULA) + TIME_FORMULA

You don't want to try to format a string into a date if the date is already properly formatted, so you could also add an =IF statement before performing any of the operations above: 如果日期已被正确格式化,则您不想尝试将字符串格式化为日期,因此您还可以在执行上述任何操作之前添加=IF语句:

=IF(ISTEXT(A1), ADJUST, A1)

The full solution, replacing ADJUST by the steps above, might look something like this: 完整的解决方案,用上述步骤代替ADJUST ,可能看起来像这样:

=IF(ISTEXT(A1), DATE(MID(A1, FIND("/", A1, FIND("/", A1, 1)+1)+1, 4), MID(A1, FIND("/", A1, 1)+1, FIND("/", A1, FIND("/", A1, 1)+1)-FIND("/", A1, 1)-1), LEFT(A1, FIND("/", A1, 1)-1))+TIMEVALUE(MID(A1, FIND(" ", A1, 1)+1, LEN(A1))), A1)

How do you import the data? 您如何导入数据?

If you open the CSV file by double-clicking or via File > Open, then Excel will parse it and you don't get a chance to see the wizard. 如果通过双击或通过“文件”>“打开”打开CSV文件,则Excel将对其进行解析,而您将没有机会看到该向导。 The date will be interpreted according to your regional settings. 日期将根据您的区域设置进行解释。 If the date format of your regional settings and the CSV file don't match, many dates will be wrong. 如果您的区域设置的日期格式和CSV文件不匹配,则许多日期将是错误的。

Instead, open a new file and go via Data > From Text. 而是打开一个新文件,然后通过“数据”>“来自文本”。 Navigate to the CSV file and open it. 导航到CSV文件并打开它。 Then you will get the Import Wizard. 然后,您将获得导入向导。 In Step 3, select the column and set it to Date. 在第3步中,选择列并将其设置为Date。 Then select the order of day, month and year as it appears in the file. 然后选择文件中显示的日,月和年的顺序。

在此处输入图片说明

As @Tim is hinting, the best way to address this may be in the source or in its importation but once in Excel it can still be dealt with by copying your text formatted dates and parsing these with Text to Columns and Fixed width with a break immediately after the year (at 10 ). 正如@Tim所暗示的那样,解决此问题的最佳方法可能是在源代码中或在其导入中,但是一旦在Excel中,仍然可以通过复制文本格式的日期并使用“文本转换为列”和“固定宽度”进行解析来解决。紧接在一年之后(在10 )。 In the process (Step 3 of 3) you can choose to treat the output for the left hand column as Date: MDY. 在此过程(第3步,共3步)中,您可以选择将左侧列的输出视为Date:MDY。 Then add the date and time together and format to suit. 然后将日期和时间加在一起并设置格式以适合。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM