3 Replies Latest reply on Mar 23, 2016 1:06 PM by Aasiyah Rashan

# Day/night based on time

Hi,

I have a date time field and I would like to do a day/night calculation based on the time recorded. I have tried this in several ways but can find a solution. I think I may be missing something obvious! I have attached a workbook below.

Thanks.

• ###### 1. Re: Day/night based on time

Try this:

IF ISNULL([Date And Time]) THEN

"Null"

ELSEIF DATEPART("hour",[Date And Time]) >= 7 AND DATEPART("hour",[Date And Time]) < 19

THEN "Day"

ELSE "Night"

END

DATEPART('hour') returns the hour number in the day, so your original calc was using numbers like 1900 (a 24h clock time) when it really needed to use just 19. Also the additional condition for Night wasn't necessary because you already had the condition for day established. In the above calc I just added another condition to test if Null.

Jonathan

• ###### 2. Re: Day/night based on time

Maybe this?

IF DATEPART("hour",[Date And Time]) >= 7 AND DATEPART("hour",[Date And Time]) < 19

THEN "Day" else "Night" END

1 of 1 people found this helpful
• ###### 3. Re: Day/night based on time

Thanks! Both calculations work and I really appreciate the explanations.