How to convert a string to a date in excel? - excel

I have the date and time in the following format in Excel:- 12Jun17/1802 How do I convert this to excel date and time format? I have tried Format Custom but I am unable to make this work.

Excel stores dates and times as days and fractions of a day since 1/1/1900. The string you show is being seen only as text.
One way to convert it is to use string functions to format it into something Excel will recognize as a date/time:
=LEFT(A2,2) & " " & MID(A2,3,3) & " " & MID(A2,6,2) & " " & TEXT(RIGHT(A2,4),"00\:00")
--> 12 Jun 17 08:02
and then convert that to a number:
=--(LEFT(A2,2) & " " & MID(A2,3,3) & " " & MID(A2,6,2) & " " & TEXT(RIGHT(A2,4),"00\:00"))
You can now format that number with any of the date/time formats.

Related

Reformatting date & time does not change after using VBA macro

I am trying to take a file with the date being mm/dd/yyyy and reformat it to yyyy-mm-dd and output that new format to a new sheet, but even after running my VBS macro it does not change although the birthDate is correct. I have seen that there is issues with Excel overwritting your format to US regardless. Is there any way around this? Thanks!
birthDate = Trim(CStr(Sheet1.Cells(currRawRow, "F")))
checkInput = InStr(1, birthDate, "/")
If (checkInput > 0) Then
birthYear = Year(birthDate)
birthMonth = Month(birthDate)
birthDay = Day(birthDate)
birthMonth = Format(CStr(birthMonth), "00")
birthDay = Format(CStr(birthDay), "00")
birthYear = Format(CStr(birthYear), "0000")
birthDate = (birthYear & "-" & birthMonth & "-" & birthDay)
Any number recognised as a date by Excel will display in the short date format of the control panel regional settings ->Date & Time for whomever opened the file.
To override this, either:
1) set the format explicitly as part of you loop as Daimian suggests or;
2) force the format to string, by preceding the date by a single quote.
e.g in your last line of code change:
birthDate = (birthYear & "-" & birthMonth & "-" & birthDay)
to
birthDate = "'" & (birthYear & "-" & birthMonth & "-" & birthDay)

Excel improving date diff function

I've defined the following formula in Excel
=DATEDIF(B595;TODAY();"y") & " years, " & DATEDIF(B595;TODAY();"ym") & " months, " & DATEDIF(B595;TODAY();"md") & " days"
But it shows a result like:
0 years, 0 months, 20 days
I would prefer to see:
20 days
Can I use a VBA function to get a nicer result? Or a formula?
Just add If statements to the formula that will remove the component if DateDif evaluates to zero:
=IF(DATEDIF(B595;TODAY();"y")=0;"";DATEDIF(B595;TODAY();"y") & " years, ") & _
IF(DATEDIF(B595;TODAY();"ym")=0;"";DATEDIF(B595;TODAY();"ym") & " months, ") & _
IF(DATEDIF(B595;TODAY();"md")=0;"";DATEDIF(B595;TODAY();"md") & " days, ")
I broke the formula into three lines so its readable here, but it should be inputted as one line

Querying data between a specific date and time in VBA

I have a database in access where every data has a date and time in two different columns.
I need to to run a query in excel using VBA which would fetch me data between two specific dates and times (eg: 01/05/2016 13:15 and 03/05/2016 10:11)
My query is as follows:
SQL = "SELECT * FROM " & database & " WHERE symbol='" & companyName & _
"' AND AdmitDate BETWEEN " & (fromDate + fromTime) & " AND " & (toDate + toTime) & ""
However, it gives me a syntax error which says:
'Missing operator in query expression'
I am not able to figure out where I have gone wrong.
Please help!
You need the right string expressions for the date/time values. Format can create these:
SQL = "SELECT * FROM " & database & " WHERE symbol='" & companyName & "' AND AdmitDate BETWEEN #" & Format(fromDate + fromTime, "yyyy\/mm\/dd hh\:nn\:ss") & "# AND #" & Format(toDate + toTime, "yyyy\/mm\/dd hh\:nn\:ss") & "#"

String Split in SSRS Report

I have string "02Years02Months". In the SSRS Report i need to show the string like
"02 Years 02 Months". Is there any good method in SSRS report Expression.
Please advice, Thanks
=Left(Fields!String.Value, 2) & " " & Mid(Fields!String.Value, 3, 7) & " " & Right(Fields!String.Value, 6)
That's assuming your numbers will always be padding with zeroes as in the example above, and given you replace the Fields!String.Value with the field containing the string being returned in your report.
I found the solution, Here is the answer
=Left(Fields!Value, 2) & " " & Mid(Fields!Value, 3, 5) & " " & Mid(Fields!Value, 8, 2) & " " & Right(Fields!Value, 6)
Thanks for the support

Calculate average from dates in text

I'm using this formulas:
=DATEDIF(B9,S9,"d") & " Days " & TEXT(S9-B9, "h:m") & " hrs:min"
=DATEDIF(B10,S10,"d") & " Days " & TEXT(S10-B10, "h:m") & " hrs:min"
etc..
And now i need to have a formula that calculates the average of those dates. The problem is that they are in text and excel cannot calculate average.. Would appreciate any input. Thanks
Your formula isn't a reliable method for calculating days and hours between two dates. Consider where B9 is 1st Jan 2013 at 22:00 and S9 is the next day 2nd Jan at 06:00 - there are only 8 hours between those two "timestamps" but your formula will give the result
1 Days 8:00 hrs:min
better to use this version
=INT(S9-B9) & " Days " & TEXT(S9-B9, "h:m") & " hrs:min"
That will give correct results in all cases
For the average you can use a formula like this
=INT(AVERAGE(S9:S18)-AVERAGE(B9:B18)) & " Days " & TEXT(AVERAGE(S9:S18)-AVERAGE(B9:B18), "h:m") & " hrs:min"
where you have data in rows 9 to 18
Consider the following:
Formulas:
C2 = B2-A2
(same for rows 2 through 6)
C7 = AVERAGE(C2:C6)
D2 = INT(C2) & " Days " & TEXT(C2, "h:mm") & " hrs:min"
(same for rows 2 through 7)

Resources