+ Reply to Thread
Results 1 to 7 of 7

Thread: Compare now() against two times

  1. #1
    Registered User
    Join Date
    09-30-2011
    Location
    Arizona, US
    MS-Off Ver
    Excel 2003
    Posts
    7

    Compare now() against two times

    Hi,

    I have a need to fill in a spreadsheet as time advances during the day every 15 minutes. So in column A, I simply created those time values (with a formatting of "time 13:30"), namely:

    8:00
    8:15
    8:30
    8:45

    In an adjacent column, I wish to take action if I'm between those time values. My formula isn't working, it is

    =IF(AND(NOW()>=A1,NOW()<A2),"Do 1","Do 2")

    Can you tell me how to fix this?

    Thanks

  2. #2
    Forum Guru NBVC's Avatar
    Join Date
    12-06-2006
    Location
    Mississauga, CANADA
    MS-Off Ver
    2003:2010
    Posts
    32,636

    Re: Compare now() against two times

    If you want just the time, add MOD()

    e.g.

    =IF(AND(MOD(NOW(),1)>=A1,MOD(NOW(),1)<A2),"Do 1","Do 2")
    Microsoft MVP - Excel

    Where there is a will there are many ways. Pick One!


    Please read the Forum Rules

    If you are happy with the results, please add to the contributor's reputation by clicking the reputation icon (star icon) below

    Please also mark the thread as Solved once it is solved. Check the FAQ's to see how.

    Preferred Charities: Lupus Canada and Sick Kids Foundation.
    Feel Free to Donate if you want to, for the assistance you received today.

  3. #3
    Forum Guru 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Vienna, VA, USA
    MS-Off Ver
    Excel 2003, 2007
    Posts
    2,257

    Re: Compare now() against two times

    Welcome to the Forum!

    NBVC has answered your question but I thought I would add some explanation around it.

    NOW() returns date + time. So for example, as I am typing this it returns

    9/30/2011 14:43

    and the underlying numeric value is 40816.613232. When you put in a time, you only get the decimal portion. For example, 08:00 has a value of 0.3333 (repeating) because it represents the fraction of a 24-hour day.

    So to compare NOW() to a time value you have to lop off the interest portion from NOW().

    As NBVC so concisely has done.
    Making the world a better place one fret at a time
    ||||||

    If someone helped you, please click on the star icon at the bottom of their post

    If your problem is solved, please update the first post:
    EDIT, Go Advanced button, set Prefix to SOLVED

    [code]
    ' Enclose code in tags like this
    [/code]

    Don't attach a screenshot
    --just attach your Excel file! It's easier and will let us experiment with your data, formulas, and code.

  4. #4
    Registered User
    Join Date
    09-30-2011
    Location
    Arizona, US
    MS-Off Ver
    Excel 2003
    Posts
    7

    Re: Compare now() against two times

    Wonderful, thanks NBVC, it works great.

    Thanks too 6StringJazzer, for explaining the underlying time format and why it works.

  5. #5
    Forum Guru NBVC's Avatar
    Join Date
    12-06-2006
    Location
    Mississauga, CANADA
    MS-Off Ver
    2003:2010
    Posts
    32,636

    Re: Compare now() against two times

    apologies for the conciseness
    Microsoft MVP - Excel

    Where there is a will there are many ways. Pick One!


    Please read the Forum Rules

    If you are happy with the results, please add to the contributor's reputation by clicking the reputation icon (star icon) below

    Please also mark the thread as Solved once it is solved. Check the FAQ's to see how.

    Preferred Charities: Lupus Canada and Sick Kids Foundation.
    Feel Free to Donate if you want to, for the assistance you received today.

  6. #6
    Forum Moderator zbor's Avatar
    Join Date
    02-10-2009
    Location
    Croatia
    MS-Off Ver
    Excel 2007
    Posts
    6,204

    Re: Compare now() against two times

    You can use =NOW()-TODAY() or =NOW()-INT(NOW()) for example
    "Relax. What is mind? No matter. What is matter? Never mind!"

  7. #7
    Forum Guru NBVC's Avatar
    Join Date
    12-06-2006
    Location
    Mississauga, CANADA
    MS-Off Ver
    2003:2010
    Posts
    32,636

    Re: Compare now() against two times

    Those should both also work.
    Microsoft MVP - Excel

    Where there is a will there are many ways. Pick One!


    Please read the Forum Rules

    If you are happy with the results, please add to the contributor's reputation by clicking the reputation icon (star icon) below

    Please also mark the thread as Solved once it is solved. Check the FAQ's to see how.

    Preferred Charities: Lupus Canada and Sick Kids Foundation.
    Feel Free to Donate if you want to, for the assistance you received today.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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.2.0