Excel 日期到字符串的转换

发布于 2024-08-28 15:18:21 字数 240 浏览 7 评论 0原文

在 Excel 工作表的单元格中,我有一个日期值,如下所示:

01/01/2010 14:30:00

我想将该日期转换为文本,并且还希望文本看起来与日期完全相同。因此,01/01/2010 14:30:00 的日期值应类似于 01/01/2010 14:30:00,但在内部它应该是文本。

我怎样才能在 Excel 中做到这一点?

In a cell in Excel sheet I have a Date value like:

01/01/2010 14:30:00

I want to convert that Date to Text and also want the Text to look exactly like Date. So a Date value of 01/01/2010 14:30:00 should look like 01/01/2010 14:30:00 but internally it should be Text.

How can I do that in Excel?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

奶气 2024-09-04 15:18:22

这是另一种选择。使用 Excel 内置的“文本分列”向导。它位于 Excel 2007 的“数据”选项卡下。

如果您选择了一列,则文件类型和分隔符的默认值应该有效,然后它会提示您更改该列的数据格式。选择文本会强制其采用文本格式,以确保它不会存储为日期。

Here's another option. Use Excel's built in 'Text to Columns' wizard. It's found under the Data tab in Excel 2007.

If you have one column selected, the defaults for file type and delimiters should work, then it prompts you to change the data format of the column. Choosing text forces it to text format, to make sure that it's not stored as a date.

温柔戏命师 2024-09-04 15:18:22

在某些情况下,预先使用 ' 字符会起作用,但如果保存到 CSV 并再次加载,这是不可能的。

'01/01/2010 14:30:00

In some contexts using a ' character beforehand will work, but if you save to CSV and load again this is impossible.

'01/01/2010 14:30:00
<逆流佳人身旁 2024-09-04 15:18:22

无法使 TEXT() 公式发挥作用

最简单的解决方案是将粘贴复制到记事本中,然后再复制到 Excel 中,并将列设置为文本,然后再粘贴回去

或者您可以使用类似这样的公式进行相同的操作

=DAY(A2)& ;"/"&月份(A2)&"/"&年份(A2)& " "&小时(B2)&":"&分钟(B2)&":"&秒(B2)

Couldnt get the TEXT() formula to work

Easiest solution was to copy paste into Notepad and back into Excel with the column set to Text before pasting back

Or you can do the same with a formula like this

=DAY(A2)&"/"&MONTH(A2)&"/"&YEAR(A2)& " "&HOUR(B2)&":"&MINUTE(B2)&":"&SECOND(B2)

帝王念 2024-09-04 15:18:22

我不知道这个问题的发表年份;现在可能已经旧了。所以,我希望我的回答能为以后类似问题提供更多的参考。

我不知道是否有人已经给出了与我即将给出的答案类似的答案,这可能会导致 - 我认为 - 是最简单,最直接和最有效的:如果有人已经给出了,我道歉,但我没见过。在这里,我的答案使用 CStr 而不是 TEXT:

假设单元格 A1 包含日期,并使用 VBA 代码:

将 strDate 变暗为字符串

'将A1中包含的值(日期)转换为字符串
strDate = CStr([A1].Value)

此后,您可以使用字符串函数(MID、LEFT、RIGHT、LEN、CONCATENATE (&) 等)将其作为任何普通字符串进行操作

I have no idea about the year of publication of the question; it might be old now. So, I expect my answer to be more of a reference for future similar questions after my post.

I don't know if anybody out there has already given an answer similar to the one I am about to give, which might result -I think- being the simplest, most direct and most effective: If someone has already given it, I apologize, but I haven't seen it. Here, my answer using CStr instead of TEXT:

Asuming cell A1 contains a date, and using VBA code:

Dim strDate As String

'Convert to string the value contained in A1 (a date)
strDate = CStr([A1].Value)

You can, thereafter, manipulate it as any ordinary string using string functions (MID, LEFT, RIGHT, LEN, CONCATENATE (&), etc.)

所谓喜欢 2024-09-04 15:18:22

如果您不使用编程,请执行以下操作
(1)选择列
(2)右键单击并选择设置单元格格式
(3) 选择“自定义”
(4) 在“类型:”下输入 dd/mm/yyyy hh:mm:ss

If you are not using programming then do the following
(1) select the column
(2) right click and select Format Cells
(3) Select "Custom"
(4) Just Under "Type:" type dd/mm/yyyy hh:mm:ss

↙厌世 2024-09-04 15:18:22

在 Excel 2010 中,marg 的答案仅适用于我电子表格中的某些数据(它是进口的)。以下解决方案适用于所有数据。

Sub change()
    toText Selection
End Sub

Sub toText(target As range)
Dim cell As range
Dim txt As String
    For Each cell In target
        txt = cell.text
        cell.NumberFormat = "@"
        cell.Value2 = txt
    Next cell
End Sub

In Excel 2010, marg's answer only worked for some of the data I had in my spreadsheet (it was imported). The following solution worked on all data.

Sub change()
    toText Selection
End Sub

Sub toText(target As range)
Dim cell As range
Dim txt As String
    For Each cell In target
        txt = cell.text
        cell.NumberFormat = "@"
        cell.Value2 = txt
    Next cell
End Sub
一瞬间的火花 2024-09-04 15:18:22

由于文本已本地化,当您尝试在不同的文化中共享文件时,它会中断。 ÅÅÅÅ-MM-DD 在瑞典可能效果很好,但在美国、德国或以色列就会变成狗屎。
合理的解决方案是英语在所有地方都被接受,但事实并非如此。

基本上不要使用文本来格式化日期。
以下是如何创建 ISO 格式的日期。 TEXT 用于确保领先,

=YEAR(A1)&"-"&TEXT(MONTH(A1);"00")&"-"&TEXT(DAY(A1);"00")

如果您想要向后、向侧面或其他任何方式,只需更改它即可。
https://www.reddit.com/r/ISO8601/comments/enhlp6 /logic_of_the_ different_date_time_systems_with/

As Text is localized it will break when trying you try to share your files over diffrent cultures. ÅÅÅÅ-MM-DD might work perfectly in sweden, is US, Germany or israel it will turn to shit.
The reasonable solution would be that english was accepted everywhere, but it's not.

Basically DON'T EVER use text as intended to format dates.
Here is how to create the date in ISO format. TEXT is used to ensure leading

=YEAR(A1)&"-"&TEXT(MONTH(A1);"00")&"-"&TEXT(DAY(A1);"00")

If you want it backwards, sideways or whatever, just change it.
https://www.reddit.com/r/ISO8601/comments/enhlp6/logic_of_the_different_date_time_systems_with/

感性 2024-09-04 15:18:22

所选答案对我不起作用,因为 Excel 仍未将文本转换为日期。这是我的解决方案。

假设在第一列 A 中,您有 2016/03/25 21:20:00 类型的数据,但存储为文本。然后在 B 列中写入 =DATEVALUE(A1),在 C 列中写入 =TIMEVALUE(A1)

然后在 D 列中执行 =B1+C1 添加日期和时间的数字格式。

最后,通过右键单击 E 列并选择“选择性粘贴”-> 将 D 中的值复制到 E 列中。粘贴为值。

突出显示 E 列中的数值并将数据类型更改为日期 - 我更喜欢使用 YYYY-MM-DD HH:MM:SS 形式的自定义日期。

The selected answer did not work for me as Excel was still not converting the text to date. Here is my solution.

Say that in the first column, A, you have data of the type 2016/03/25 21:20:00 but is stored as text. Then in column B write =DATEVALUE(A1) and in column C write =TIMEVALUE(A1).

Then in column D do =B1+C1 to add the numerical formats of the date and time.

Finally, copy the values from D into column E by right clicking in column E and select Paste Special -> Paste as Values.

Highlight the numerical values in column E and change the data type to date - I prefer using a custom date of the form YYYY-MM-DD HH:MM:SS.

多像笑话 2024-09-04 15:18:21
=TEXT(A1,"DD/MM/YYYY hh:mm:ss")

(24 小时时间)

=TEXT(A1,"DD/MM/YYYY hh:mm:ss AM/PM")

(标准时间)

=TEXT(A1,"DD/MM/YYYY hh:mm:ss")

(24 hour time)

=TEXT(A1,"DD/MM/YYYY hh:mm:ss AM/PM")

(standard time)

你没皮卡萌 2024-09-04 15:18:21

这是一种 VBA 方法:

Sub change()
    toText Sheets(1).Range("A1:F20")
End Sub

Sub toText(target As Range)
Dim cell As Range
    For Each cell In target
        cell.Value = cell.Text
        cell.NumberFormat = "@"
    Next cell
End Sub

如果您正在寻找无需编程的解决方案,则应将问题移至超级用户。

Here is a VBA approach:

Sub change()
    toText Sheets(1).Range("A1:F20")
End Sub

Sub toText(target As Range)
Dim cell As Range
    For Each cell In target
        cell.Value = cell.Text
        cell.NumberFormat = "@"
    Next cell
End Sub

If you are looking for a solution without programming, the Question should be moved to SuperUser.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文