简体   繁体   中英

Convert date and time to ISO yyyy-mm-dd in Excel

In a csv file which I have opened in Excel date is formated like "2014-08-28 09:03:19.789".

How can the above be converted to just yyyy-mm-dd (2014-08-28) in Excel?

I have tried to set the column to yyyy-mm-dd but it does not change.

What version of Excel are you using? (I'm not sure if that matters.)

If you tried just typing yyyy-mm-dd in the format selector box that will not work.

在此处输入图片说明

You need to open the custom format dialog and type in yyyy-mm-dd as a new format there.

在此处输入图片说明 在此处输入图片说明

Use the Text to Columns wizard either on the worksheet cell, or by Importing the file using the Data Ribbon ► Get External Data tab ► From text.

For that column, select Space as the delimiter and, at Step 3, YMD for the format. When you are finished, custom format the date column and the time columns as you wish.

If you need the entire date/time stamp in the same cell, add them together in a third column, and format as needed.

This solution should work regardless of your system settings and is based on your date time value really being a string once its been imported into excel. This solution will assume your date is in A1

First strip out the year:

=left(A1,4)

Then strip out the month:

=mid(A1,6,2)

Then strip out the day:

=trim(mid(A1,9,2))

note if you know you have a leading zero in from of single digit days then y ou can drop the trim function.

Now you date can be convert to an excel format date using the date function and substituting in the information pull out above.

=Date(left(A1,4),mid(A1,6,2),trim(mid(A1,9,2)))

If your cell is not formatted correctly you will probably get an integer like 420303 or something to that effect Its the number of days since January 0, 1900 on a windows OS, something like since 1904 on a Mac.

So for the time you repeat the process, but I am going to suggest you try option A) first and if it does not work for you try option B)

Option A) for time shortcut

If you time also show trailing 0 ie 9.000 for seconds then you might get away with:

=Timevalue(right(A1,12))

Now if that does not work for you we can adjust the 12 to be:

LEN(A1)-Find(" ",A1)

Now if option A and using time value does not work for you then we need to go in and rip out the hours minutes and seconds and essentially do the same process that was done for the date as described above.

Option B) ripped by units

To get the hours use:

=MID(A1,FIND(" ",A1)+1,2)

To get the minutes use:

=MID(A1,FIND(":",A1)+1,2)

To get seconds use

=RIGHT(A1,LEN(A1)-FIND(".",A1)+3)

This assumes you have a leading 0 for single digit seconds 0-9.

Now you can take all that and drop it into the TIME formula to get the following

=TIME(MID(A1,FIND(" ",A1)+1,2),MID(A1,FIND(":",A1)+1,2),RIGHT(A1,LEN(A1)-FIND(".",A1)+3))

Now since days at the integers and time is saved as the decimal value, we can combine the date and time formula with a + and you should now have the whole thing ready to have formatting applied to it.

=Date(left(A1,4),mid(A1,6,2),trim(mid(A1,9,2)))+TIME(MID(A1,FIND(" ",A1)+1,2),MID(A1,FIND(":",A1)+1,2),RIGHT(A1,LEN(A1)-FIND(".",A1)+3))    

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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