In excel, date shows as mdyyyy, want as mm-dd-yyyy - excel

Hello I have an excel file that has a date field but what is happening is the date in the spreadsheet shows as mdyyyy (example 5271960 would be 05-27-1960). How does one convert the field in excel?

Right-click on the cell and choose Format Cells.... Here you can either choose an existing Date format, or Custom format it to suit your needs. Settings include:
For days:
d: 9 -> 9, 17 -> 17, ...
dd: 9 -> 09, 17 -> 17, ...
ddd: Day of the Week (Mon, Tue, ...)
dddd: Day of the Week (Monday, Tuesday, ...)
For months:
m: 9 -> 9, 12 -> 12, ...
mm: 9 -> 09, 12 -> 12, ...
mmm: Month (Jan, Feb, ...)
mmmm: Month (January, February, ...)
For years:
y: 2014 -> 14, 2017 -> 17, ...
yy: 2014 -> 14, 2017 -> 17, ...
yyy: 2014 -> 2014, 2017 -> 2017, ...
yyyy: 2014 -> 2014, 2017 -> 2017, ...
Similar formatting holds for time stamps as well.

I suspect your date field is not containing a "real" date but just the string of numbers you show. If it does contain a "real" date, then you can merely custom format it however you wish.
If it contains the value 5271960 or similar, then first convert it to a real date using this formula (assuming your pseudodate is in A1):
=DATE(MOD(A1,10^4),INT(A1/10^6),MOD(INT(A1/10^4),100))
Then custom format the cell to show the date as you prefer.
Another option, if you are only going to be dealing with Excel on machines that are using the US format in the Windows Regional Settings: MDY
=--TEXT(A1,"00\/00\/0000")
This will convert the value to a "real date" and you can then custom format as you wish.

Excel allows to pre-scribe a user defined format for a cell as:
mm-dd-YYYY >> 12-13-2014
mmm-dd-YYYY >> XII-13-2014
mmmm-dd-YYYY >> december-13-2014

If the cell is a genuine Date, then use the Format Cells... Dialog:

If 5271960 is an example of how your dates are showing at the moment then you would use this formula in another cell to get it to 05-27-1960 if this date was in cell A2
=0&(LEFT(A2,1))&"-"&(MID(A2,2,2))&"-"&(RIGHT(A2,4))
Unfortunately, This formula will only work if there are 7 numbers in your 'date' column. If you have 8 Numbers (such as 15271960) you would use this formula in another cell..
=(LEFT(A2,2))&"-"&(MID(A2,3,2))&"-"&(RIGHT(A2,4))
Hope this helps.

Related

How can I convert a date format of type "Fri Jun 30 '17, 11:15:56 am" to "mm/dd/yyyy" format in VBA

I have a need to convert dates of format Fri Jun 30 '17, 11:15:56 am to mm/dd/yyyy format using a VBA. I tried format() function but it didn't work.
If format() doesn't work, then (as you haven't told us otherwise) we have to assume that the cell contains that date as text, not as a date.
In which case, allow me to offer one of many methods of converting this to mm/dd/yyyy:
Format(DateValue(Replace(Mid(Split(Range("A1").Value, ", ")(0), 4, 20), "'", "")), "mm/dd/yyyy")
As #Peh mentions though, this is not the best way to deal with a date. It is better to hold the date as a Date - and if you're writing to a cell, to write that Date to the cell as a Date. This makes manipulation of the date much easier and you just apply formatting to the cell to get the desired look.
To get the Date, you could use:
DateValue(Replace(Mid(Split(Range("A1").Value, ", ")(0), 4, 20), "'", ""))
Explanation:
Split(Range("A1").Value, ", ")(0)
This creates an array that contains the string split in two, the first part (0) contains the left element, the second part (1) contains the right element.
So Fri Jun 30 '17, 11:15:56 am becomes Fri Jun 30 '17
We then take a portion of the left hand part above:
Mid(<left hand part>, 4, 20)
from character 4, for (up to) 20 characters.
So Fri Jun 30 '17 becomes Jun 30 '17
We then remove the ' using a Replace(text , "'", "") function.
So Jun 30 '17 becomes Jun 30 17
The DateValue function converts strings that contain dates into an actual date value. Jun 30 17 is recognisable to Excel as a date and given that the month element is obvious (i.e. a word and not a number) then Excel is able to un-ambiguously convert it.
So Jun 30 17 becomes 42916 (- which is 42,916 days since 31st Dec 1899).

Convert sting in datetime like excel format cell does in t-sql

I have END_Date data like 43830.99931. When I paste it in the excel and format the cell to Date. It convert it to 12/31/2019 11:59:00 PM. I want same functionality in T-SQL. How can I achieve same result using T-SQL?
Excel (by default) uses the 1900 date system. This simply means that the date 1 Jan 1900 has a true numeric value of 1, 2 Jan 1900 has a value of 2 etc. These values are called "serial values" in Excel and it is these serial values that allows us to use dates in calculations.
The code to convert:
DECLARE #SerialDate FLOAT;
SET #SerialDate = 43830.99931;
SELECT CAST(#SerialDate - 2 AS DATETIME);
returning back the date time value of:
2019-12-31 23:59:00.383
You may have noticed the -2 in the cast. That's because Excel, due to issues with 29th Feb 1900 and the inclusiveness of the start/end dates. (1900-01-01 is day 1 not day 0, and 1900 was not a leap year but excel calculates it wrongly) we have to subtract 2. Details on that issue can be found here.

How to create a date in excel

I have excel file with 2 columns, year, and month. The year is given in YYYY format and the month the full name of the month like January, February.
Data sample,
column1, Column2
2014, January
2014, February
2018, March
As of now I have tried below code which is giving the error.
=date(A2, B2, 01)
and it is not returning any date values.
Requesting your assistance.
It will also work for me if it is solved by sas code.
Try:
=DATEVALUE(CONCATENATE(B3," 1, ",A3))
This will concatenate the month, follow by a 1 and the year (ex: January 1, 2014). It will then convert this to a datevalue (dates are stored as numbers in Excel). If you format your cell as a date (MMMM YYYY) you will have the desired result.
Try following
=DATEVALUE("1-" & LEFT(B1,3) & "-" & A1)
If you were importing the data into SAS, here is a solution to create a date from month name and year variables.
data have;
length month $ 15;
infile datalines delimiter=',';
input year month $;
datalines;
2014, January
2014, February
2018, March
;
Run;
Data want(keep=date);
Set Have;
/*Length mon $3 yr $4 dt $15;*/
/*Mon=substr(month,1,3);*/
/*Yr=put(year,4.);*/
/*Dt=cats('01',mon,yr);*/
/*Date = input(dt,date9.);*/
/* All in one line */
Date = input(cats('01',substr(month,1,3),put(year,4.)),date9.);
Format DATE mmddyy10.;
Run;

Transform text-date in format

I have a text formatted date that looks like this:
June 12th 2017, 9:07am PDT
How can I transform it in a date format (the classic methods Format Cells and DateValue() don't work)?
Thank you.
My solution uses a helper table listing all the 12 months and their serial number at A1:B12
January 1
February 2
March 3
April 4
May 5
June 6
July 7
August 8
September 9
October 10
November 11
December 12
Assuming the value is in D10
Getting the separate elements
Month:
=VLOOKUP(LEFT(D10,FIND(" ",D10)-1),A1:B12,2,FALSE)
Date:
=IFERROR(VALUE(RIGHT(LEFT(D10,FIND(" ",D10)+2),2)),RIGHT(LEFT(D10,FIND(" ",D10)+1),1))
Year:
=RIGHT(LEFT(D10,FIND(",",D10)-1),4)
Hour:
=IF(RIGHT(LEFT(D10,FIND(":",D10)+4),2)="am",RIGHT(LEFT(D10,FIND(":",D10)-1),2),RIGHT(LEFT(D10,FIND(":",D10)-1),2)+12)
Minute:
=RIGHT(LEFT(D10,FIND(":",D10)+2),2)
All this combined in the DATE and TIME function to give a single formula:
=DATE(RIGHT(LEFT(D10,FIND(",",D10)-1),4),VLOOKUP(LEFT(D10,FIND(" ",D10)-1),A1:B12,2,FALSE),IFERROR(VALUE(RIGHT(LEFT(D10,FIND(" ",D10)+2),2)),RIGHT(LEFT(D10,FIND(" ",D10)+1),1)))+TIME(IF(RIGHT(LEFT(D10,FIND(":",D10)+4),2)="am",RIGHT(LEFT(D10,FIND(":",D10)-1),2),RIGHT(LEFT(D10,FIND(":",D10)-1),2)+12),RIGHT(LEFT(D10,FIND(":",D10)+2),2),0)
Then change the format of the cell to:
mmmm dd yyyy hh:mm AM/PM "PDT"
This will give:
June 12 2017 09:07 AM PDT
To add a comma(,), use the custom format:
mmmm dd yyyy"," hh:mm AM/PM "PDT"

Convert date format from dd/mm/yyyy to mm/dd/yyyy?

I have the following dates on my Excel sheet.
02/01/2017
02/02/2017
02/03/2017
02/07/2017
2/15/2017
2/16/2017
I want it to read as Feb 1, Feb 2. etc however Excel reads the dates as January 2, 2017, Feb 02, 2017, March 02, 2017.
How do I do this? Help please.
This is a built in function in Excel. Try from the ribbon above,
Number Group > More Number Formats > Date > Your desired Format
If you're looking for more customizability, try separating them by the / symbol using text to columns function from the Data tab in the ribbon.
This is what I got from punching in your data:
February 1, 2017
February 2, 2017
February 3, 2017
February 7, 2017
February 15, 2017
February 16, 2017

Resources