Excel Date Conversion from yyyymmdd to mm/dd/yyyy - excel

I have been searching for about an hour on how to do this in Excel.
I have an Excel file that was created from an old system and I am pulling information from a SQL Server Database, I will be inputting the information back into the SQL Server Database and would like the Dates to match.
I have tried Creating a Custom Format, but I am unsure if I even did it Correctly. I found several places where they want to go the other way mm/dd/yyyy to yyyymmdd but they have not been helpful.
I am unfamiliar with using VBA in any Microsoft Products otherwise I am sure that this would be a simple Task.
I have two separate columns that need to be changed.
How do I Format the entire column from (float)yyyymmdd to a (Date)mm/dd/yyyy

You can convert the value to a date using a formula like this, next to the cell:
=DATE(LEFT(A1,4),MID(A1,5,2),RIGHT(A1,2))
Where A1 is the field you need to convert.
Alternatively, you could use this code in VBA:
Sub ConvertYYYYMMDDToDate()
Dim c As Range
For Each c In Selection.Cells
c.Value = DateSerial(Left(c.Value, 4), Mid(c.Value, 5, 2), Right(c.Value, 2))
'Following line added only to enforce the format.
c.NumberFormat = "mm/dd/yyyy"
Next
End Sub
Just highlight any cells you want fixed and run the code.
Note as RJohnson mentioned in the comments, this code will error if one of your selected cells is empty. You can add a condition on c.value to skip the update if it is blank.

Do you have ROWS of data (horizontal) as you stated or COLUMNS (vertical)?
If it's the latter you can use "Text to columns" functionality to convert a whole column "in situ" - to do that:
Select column > Data > Text to columns > Next > Next > Choose "Date" under "column data format" and "YMD" from dropdown > Finish
....otherwise you can convert with a formula by using
=TEXT(A1,"0000-00-00")+0
and format in required date format

Here is a bare bones version:
Let's say that you have a date in Cell A1 in the format you described. For example: 19760210.
Then this formula will give you the date you want:
=DATE(LEFT(A1,4),MID(A1,5,2),RIGHT(A1,2)).
On my system (Excel 2010) it works with strings or floats.

for converting dd/mm/yyyy to mm/dd/yyyy
=DATE(RIGHT(a1,4),MID(a1,4,2),LEFT(a1,2))

Found another (manual) answer which worked well for me
Select the column.
Choose Data tab
Text to Columns - opens new box
(choose Delimited), Next
(uncheck all boxes, use "none" for text qualifier), Next
use the ymd option from the Date dropdown.
Click Finish

Related

excel 2013 convert column data format from 'yyyymmdd hhmm' to yyyy-mm-dd

How can i convert in excel 2013 column data format from 'yyyymmdd hhmm' to yyyy-mm-dd
Click on any cell in the column and run this macro:
Sub DateFormat()
With ActiveCell.EntireColumn.Cells
.NumberFormat = "yyyy-mm-dd"
End With
End Sub
Firstly, click on the top of the column to select all of the cells within that column, then right click on a random cell within that column and click 'Format Cells', from here a box will show up.
The box will have all different format rules, and by default it should be on the 'Number' tab within the box, from here select 'Date' and then you will be able to select the appropriate type of date format from here that you wish to have.
For your case you should see one corresponding to your yyyy-mm--dd format
If it is text-to-text: =LEFT(A1,4)&"-"&MID(A1,5,2)&"-"&MID(A1,7,2) (split and rejoin the string)
If it is date-to-text: =TEXT(A1,"yyyy-mm-dd")
If it is text-to-date, make a date with: =DATE(LEFT(A1,4),MID(A1,5,2),MID(A1,7,2)) (split the string and give the parts to the DATE function) ...
... to show date type cells in a different format, use methods above (if you don't have yyyy-mm-dd in your "Date" options, you can type it in under "Custom")

Dates Formatting in Excel

I have a column with many thousands of rows. The column is meant to be date and is of the format dd/mm/yyyy
But, when I try to do formulas based on the dates, something is clearly amiss.
For example, if you try to apply autofilter on the dates, some of them are grouped as a year with the expandable boxes while others appear as their own items.
For each record I tried a formula to parse it apart.=DATE(RIGHT(A2,4),MID(A2,4,2),LEFT(A2,2))
That did not help.
I also selected the column and switched it from general to date format
I really don't know how to ask the question any clearer. I can tell you that with a date of the format 1/11/2013 when I run =year(right(A1,4)) I get 1903 instead of 2013. When I run =date(right(A1,4),mid(A1,3,2),left(A1,2)) the formula returns 2/10/3192
It's very simple why your formulas doesn't work corectly. When yor're using somehting like this: RIGHT(A2,4), your value from A2 translates to 41579 for 1/11/2013 (Excel stores all dates as integers and all times as decimal fractions. You can read more here). Next formula should work well:
=DATE(RIGHT(TEXT(A2,"dd/mm/yyyy"),4),MID(TEXT(A2,"dd/mm/yyyy"),4,2),LEFT(TEXT(A2,"dd/mm/yyyy"),2))
Btw, if you'd like to get correct format for dates, you can add formula in some empty column (but before set date format for this column):
=A2*1
and drag it down. Then copy values from this temp column and paste them using "Paste special->Values" in colunm A (where should be date format as well)
Or you can use this simple macro:
Sub test()
With Range("A2:A100")
.NumberFormat = "dd/mm/yyyy"
.Value = .Value
End With
End Sub

Excel formatting for date is not working when exporting as XML

I have created an Excel that includes a mapping file. What I am trying to do is have users enter the data in here and press ctrl + E (I created a macro) and it would export it out as XML. Here's the issue: When line 1 gets exported, it keeps its date and time format in XML (Example: In XML, time will show up as 12/01/2013). But anything entered under that line will not show up in the format I want. Instead, it will show up as 1345464 for date.
This thing has been very annoying because I cannot figure out what I can do to format the whole Excel document/column so it will show the date and time as text instead of weird number.
Someone please help me out.
In VBA, use the Format function
date = Format(date, "mm/dd/yyyy")
Then change the format of the cell you store it in:
Cells(row, col).NumberFormat = "#"
(substitute your cell's coordinates for row and col)
I'm no expert but I spent a lot of time trying to figure this out and eventually this code worked for me
Dim eCell as Range
For Each eCell In Range("O20:P20")
If Len(eCell.Value) > 0 Then
eCell.NumberFormat = "#"
eCell.Value = CDate(eCell.Value)
End If
Next
The CDate function converts the serial date back to normal date
Wow I feel stupid! It was easier than I could have ever thought:
Step1: Drag the date that is in start date to the row you will populate data. (Do the same for Enddate column).
Step2: Notice a small square box at the bottom (it appears after following the step1). Expand the square box.
Step3: Choose the option that says: Fill Formatting Only.
Congrats! Now try to enter/paste a date. It should stay in left (text format).

Convert text (12.05.79) to date in excel

I have imported some dates into an excel document and I need to format them as dates not text strings.
The format is like this: 12.05.79 but when I set the format to date excel doesn't do anything.
Any way to do this?
C
Two possible ways to convert "in situ" without an extra column
1 - use "Text to columns"
Select column of dates then Data > text to columns > Next > Next > "under column data format" select "Date" and the format from dropdown (MDY or DMY) > OK
2 - use Edit/Replace. Replace "." with "/" (in both cases without quotes)
Changing the format doesn't change the value (or in this case text). You'll have to use a macro, or just use a helper column with the following formula. You can then copy / paste values and remove the first column
=DATEVALUE(SUBSTITUTE(A1,".","/"))

Excel Date to String conversion

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?
=TEXT(A1,"DD/MM/YYYY hh:mm:ss")
(24 hour time)
=TEXT(A1,"DD/MM/YYYY hh:mm:ss AM/PM")
(standard time)
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.
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.
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
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)
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.)
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
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
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/
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.

Resources