VBA Time Conversion - excel

I've autogenerated time from an API in the format "15-03-2022 01:10", here I would like to take the timepart and convert it to 13:10 and so on. As there is no AM/PM at the end of time, it is getting difficult to apply the Excel time format, is there any way I can achieve this using excel VBA.
I Searched everywhere, but couldn't find the correct solution for the above issue. Some example like
11:30 --> 11:30 Morning
11:30 --> 23:30 Night
just trying to figure out how to distinguish between the above 2 formats in a single day. But the end output needs to be in 24hr format.

Always handle date and time as Date, not text, no exceptions. So:
NewTime = DateAdd("h", 12, CDate("15-03-2022 01:10"))
Then apply the format you wish for display, here:
dd-mm-yyyy hh:nn

Every day in Excel is 1, so half a day is 0.5.
CDate("15-03-2022 01:10") + 0.5 to add half a day.
Also you can concatenate PM to the end of the string
CDate("15-03-2022 01:10 PM")
Then you can output any display text you want with the Format function.
Format(MyTime, "MMM dd, hh:mm:ss am/pm") would display "Mar 15, 01:10:00 PM"
For 24 Hour time formatting, remove the am/pm:
Format(MyTime, "MMM dd, hh:mm:ss") would display "Mar 15, 13:10:00"

Related

Converting integer to time format in excel

Date
19112018
19112016
19112015
19112013
I have a column named Date.
I want to convert 19112018 to 2019-11-20 18:00
19 means year 2019
11 month
20 days
18 is hour
Thanks .
Use the following formula:
=DATE(20&LEFT(A2,2),MID(A2,3,2),MID(A2,5,2))+RIGHT(A2,2)/24
Or:
=--(20&REPLACE(REPLACE(REPLACE(A2,7,0," "),5,0,"-"),3,0,"-")&":00")
Then format the output cell:
yyyy-mm-dd hh:mm

Trouble creating a custom date format

I am trying to build a custom date format that matches the format below. I've included an example of the number value (date) and how the output should look. I've tried something like: ddd mmm d, yyyy h:mm:ssAM/PM [ddd mmm d, yyyy h:mm:ssAM/PM] as a custom format, but can't seem to get it to work. Can you tell me where I am going wrong? Thank you in advance!
(Date time) 1546926706 = (Formatted Output) Mon Jan 7, 2019 9:51:46pm PST [Mon Jan 7, 2019 10:51:46pm]
Attempt at translating format into custom format Excel understands:
`
ddd mmm d, yyyy h:mm:ssAM/PM [ddd mmm d, yyyy h:mm:ssAM/PM]
Basically I'm trying to reverse engineer a custom date format because I can't access the system that is sending me the data and need it to make sense on for users on the front end.
`
1546926706 is UNIX/POSIX time where each 1 is a second after at 1-Jan-1970. You need a conversion to an excel datetime where each 1 is a day that starts 1-Jan-1900.
'Tue Jan 8, 2019 5:51:46 AM PST
=TEXT(1546926706/86400 + 25569, "ddd mmm d, yyyy h:mm:ss am/pm \P\S\T")
That's returns text, not a true datetime but it shows you the conversion and format mask.

Excel Day shift and night shift schedule

Lets say I have two shifts at work.
Day shift is from 7:00 to 17:30
Night shift is from 18:00 to 4:30
Based on this numbers I want excel to return if its "Night Shift" or "Day Shift"
This is what i have so far with the IF statement
=IF(Q2>=7,"DAY SHIFT",IF(Q2<=17.3,"DAY SHIFT",IF(Q2>=18,"NIGHT SHIFT",IF(Q2<4.3,"NIGHT SHIFT"))))
But im not getting the results I want. Any ideas to correct the formula?
The following is what you are looking for, assuming that cell Q2 is a Date/Time value.
=IF(AND(MOD(Q2,1)>=(7/24),MOD(Q2,1)<=(17.5/24)),"Day Shift",IF(OR(MOD(Q2,1)<(4.5/24),MOD(Q2,1)>=(18/24)),"Night Shift",""))
The MOD(Q2,1) extracts the time portion of a date/time cell. These values are stored as decimal numbers, where 4:30 AM would be represented by 4.5/24 = 0.1875 (note that 30 minutes is half an hour, hence 4:30 AM is represented by 4.5/24 instead of 4.3/24, which would be 4:18 AM).
This should work:
=IF(AND(Q2>=7,Q2<=17.5),"DAY SHIFT", "NIGHT SHIFT")
But if you want to specify the "Night Shift", this would be better:
=IF(AND(Q2>=7,Q2<=17.5),"DAY SHIFT", IF(OR(Q2>= 18, Q2<4.5), "NIGHT SHIFT",""))
A little shorter:
=IF(OR(Q2<4.5/24,Q2>=3/4),"Night",IF(AND(Q2>=7/24,Q2<=17.5/24),"Day","?"))&" Shift"

How does Excel convert 12 hour time (text values) into decimal numbers?

I'm trying to find the manual function on how Excel converts am/pm time (text) values into numeric decimals (using the Format menu > Number while selecting my 12 hour time column).
For example, how does 12:30:00 PM turn into 0.52083?
Thank you.
Date and time can be handled in many ways. The time representation you are observing in Excel is based on 1 day = 24 hours = 1.0. So 12:00 pm should be exactly 0.5, while 12:30:00 pm would be a bit (30/(24*60) = 1/48 = 0.0208333) above.
Btw. 12/24 hour is just a region specific presentation issue. Internally this doesn't affect the actual date and time values.

how to find hours of the day between two date times excel or MS access

I have a report where im supposed to collect data for different hour blocks of the day between issued date time and completed date time. e.g IssueDateTime = 12/19/2016 10:00 AM and CompletedDateTime = 12/19/2016 3:00 PM i would expect the formula to return 10:00 AM, 11:00 AM, 12:00 PM, 1:00 PM, 2:00 PM, 3:00 PM. dont know if this is even possible but any insight will be greatly appreciated.
Here is how to do the same thing with VBA / Access:
cdate(DatePart("h",Now())& ":00")
This will return 1:00:00 PM for current time here (1:12 PM)
To get just hour itself, use:
DatePart("h",Now())
Here is simple function that returns all hour blocks between 2 different times:
Public Function ShowHourBlocks(StartTime As Date, EndTime As Date) As String
Dim StartBlock As Date
Dim EndBlock As Date
Dim Answer As String
StartBlock = CDate(DatePart("h", StartTime) & ":00")
EndBlock = CDate(DatePart("h", EndTime) & ":00")
Do While StartBlock <= EndBlock
Answer = Answer + Format(StartBlock, "h:mm AMPM") & " "
StartBlock = DateAdd("h", 1, StartBlock)
Loop
ShowHourBlocks = Answer
End Function
Here is what returns when you run it in immediate window:
?ShowHourBlocks(now(), DateAdd("h",4,Now()))
1:00 PM 2:00 PM 3:00 PM 4:00 PM 5:00 PM
This formula will return an array of the hours:
=TEXT(ROW(INDIRECT(HOUR(IssueDateTime) & ":" & HOUR(CompletedDateTime)))/24,"hh:mm AM/PM")
For your example, the formula returns:
{"10:00 AM";"11:00 AM";"12:00 PM";"01:00 PM";"02:00 PM";"03:00 PM"}
but when entered into a single cell, you will only see the first element.
You can display the full result either by entering the formula as an array, or by using the INDEX function.
If you are not familiar with arrays in Excel, I suggest reading the HELP topics in Excel and on MSDN, and also see Chip Pearsons Introduction to Array Formulas
Here is an example. The array formula was entered B4:B17, and Conditional Formatting was used to make the #N/A results appear blank.

Resources