+ Reply to Thread
Results 1 to 8 of 8

Extract Date/Time to Date and Time

Hybrid View

ernestgoh Extract Date/Time to Date and... 01-09-2010, 08:03 AM
daddylonglegs Re: Extract Date/Time to Date... 01-09-2010, 08:34 AM
Marcol Re: Extract Date/Time to Date... 01-09-2010, 09:18 AM
daddylonglegs Re: Extract Date/Time to Date... 01-09-2010, 09:27 AM
Marcol Re: Extract Date/Time to Date... 01-09-2010, 11:59 AM
daddylonglegs Re: Extract Date/Time to Date... 01-09-2010, 12:31 PM
ernestgoh Re: Extract Date/Time to Date... 01-09-2010, 02:53 PM
Marcol Re: Extract Date/Time to Date... 01-09-2010, 02:49 PM
  1. #1
    Registered User
    Join Date
    07-02-2005
    Posts
    53

    Extract Date/Time to Date and Time

    Hi have this simple problem but I do not know the solution.

    In Cell A1 = 01/12/2008 12:00:00 AM

    I am trying to extract in cell A2 = 01/12/2008 and A3 = 12:00:00 AM.

    But I will end up having a string of numbers each time I try to right (A1,11).

    My end result is I will use the values in A3 if between 12am to 5am, assign a value in A4 as "Night Shift"

    A1 = 01/12/2008 12:00:00AM
    A2 = 01/12/2008
    A3 = 12:00:00Am
    A4 = if(And(A3>12,A3<5),"Night Shift","Day Shift")

    Thanks in advance for helping!
    Last edited by ernestgoh; 01-09-2010 at 02:53 PM.

  2. #2
    Forum Expert daddylonglegs's Avatar
    Join Date
    01-14-2006
    Location
    England
    MS-Off Ver
    Microsoft 365
    Posts
    14,723

    Re: Extract Date/Time to Date and Time

    You can extract the date from a date and time with

    =INT(A1)

    and the time with

    =MOD(A1,1)

    but you could do everything without the intermediate step if you want, i.e.

    =if(And(MOD(A1,1)>"12:00"+0,MOD(A1,1)<"17:00"+0),"Night Shift","Day Shift")

  3. #3
    Forum Guru (RIP) Marcol's Avatar
    Join Date
    12-23-2009
    Location
    Fife, Scotland
    MS-Off Ver
    Excel '97 & 2003/7
    Posts
    7,216

    Red face Re: Extract Date/Time to Date and Time

    Sorry - This dosn't work I'll have another try!
    Daddylonglegs is absolutely correct
    You can't simply use Format Cells

    Range "A1" format cells Custom dd/mm/yyyy hh:mm AM/PM
    Range "A2" format cells Date *14/03/2001
    Range "A2" =A1
    Range "A3" format cells Time 1:30:55 PM
    Range "A3" =A1
    Range "A4" format cells General
    Range "A4" = IF(AND(A3>12,A3<5),"Night Shift","Day Shift")

    Might be easier to use Columns rather than Rows in your sheet.
    ie A2 becomes B1, A3 becomes C1, A4 becomes D1
    That way you can format each column, then use Fill Down for B1 to D1
    as required.

    Only a thought.
    Last edited by Marcol; 01-09-2010 at 11:41 AM. Reason: My mistake it simply dosn't work!

  4. #4
    Forum Expert daddylonglegs's Avatar
    Join Date
    01-14-2006
    Location
    England
    MS-Off Ver
    Microsoft 365
    Posts
    14,723

    Re: Extract Date/Time to Date and Time

    Hello Marcol,

    The problem with using formatting is that it only changes the display it doesn't change the underlying value.

    It seems to me that ernestgoh wants to extract the time so that he can evaluate it in his IF function. If A3 contains just the same value as A1, only formatted to look different, then that won't work......

  5. #5
    Forum Guru (RIP) Marcol's Avatar
    Join Date
    12-23-2009
    Location
    Fife, Scotland
    MS-Off Ver
    Excel '97 & 2003/7
    Posts
    7,216

    Re: Extract Date/Time to Date and Time

    Sorry for earlier letting my mouth run before my brain was in gear!
    Further to Daddylonglegs solution

    I found this formula helped to explain how it works with the 24hr clock.

    = IF(AND(MOD(A1,1)*24>12,MOD(A1,1)*24<17),"Day Shift","Night Shift")

    This can be expanded if required as follows

    If you add Day Shift Start Time in "A2"
    and Day Shift End Time in "A3"

    Then

    = IF(AND(MOD(A1,1)>MOD(A2,1),MOD(A1,1)<MOD(A3,1)),"Day Shift","Night Shift")

    Hope this might help you and cool my red face a little.

  6. #6
    Forum Expert daddylonglegs's Avatar
    Join Date
    01-14-2006
    Location
    England
    MS-Off Ver
    Microsoft 365
    Posts
    14,723

    Re: Extract Date/Time to Date and Time

    Quote Originally Posted by ernestgoh View Post
    My end result is I will use the values in A3 if between 12am to 5am, assign a value in A4 as "Night Shift"
    That looks like a better approach Marcol but I fear we've both misinterpreted a little based on the above quote. I assumed 12 noon to 5pm, I'm not sure why..... but from the above it appears that "Night shift" would be returned if the time from A1 is any time before 5 AM, in which case you don't need to check for "after midnight" as every time will fulfill that (unless you need to make a specific case for exactly 12:00) so the solution's a little simpler, i.e. either

    =IF(MOD(A1,1)*24<5,"Night Shift","Day Shift")

    or even use the HOUR function like this

    =IF(HOUR(A1)<5,"Night Shift","Day Shift")
    Last edited by daddylonglegs; 01-09-2010 at 12:34 PM.

  7. #7
    Registered User
    Join Date
    07-02-2005
    Posts
    53

    Re: Extract Date/Time to Date and Time

    thanks guys! it works for me. the hour function is my favorite!

    thanks again.

  8. #8
    Forum Guru (RIP) Marcol's Avatar
    Join Date
    12-23-2009
    Location
    Fife, Scotland
    MS-Off Ver
    Excel '97 & 2003/7
    Posts
    7,216

    Re: Extract Date/Time to Date and Time

    Thanks Daddylonglegs.

    I missed this
    My end result is I will use the values in A3 if between 12am to 5am, assign a value in A4 as "Night Shift"
    And used this as posted not noticing that 12 should have read 0 and assumed 5 to be 17
    A4 = if(And(A3>12,A3<5),"Night Shift","Day Shift")
    The dangers of using AM/PM instead of the 24hr system?

    I did wonder why I had to reverse the If conditions.

    Again you are correct, assuming night shift never begins before midnight.
    Last edited by Marcol; 01-14-2010 at 10:07 PM. Reason: Mouth, Brain, and Gear spring to mind again! And make post more readable

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1