Need time displayed in 24 hour format in LS

I have script that needs to put text in a particular field depending on the time from another field. My script (without all the DateTime methods) is:

If time is >= 08:00:00 and < 09:00:00 then the text is placed in the 8:00 field… and so on.

This works find through the day until I get to 12:00pm. My script says if time >= 12:00:00 and < 13:00:00 then put the text in the 12:00 field. The problem I’m having is in viewing the code in the debugger it is displaying the time as 01:00:00 PM so the statement is considered false. I have the field set to 24 hour clock and it displays as 13:00 on the form but in the doc properties it is shown as 01:00:00 PM.

How can I get my script to display the 24 hour clock? Any ideas are welcomed!

Thanks!

Subject: Need time displayed in 24 hour format in LS

If I understand correctly, your script is comparing time values based on their text transcription, rather than comparing them to their real time value.

It is difficult to help you without your code, but I bet you are converting your values as strings before your comparisons, otherwise, it should work.

Assuming you got a valid date time expression in LS, you can simply extract the hour from it with:

hour (dateExpr)

Which will already return a 24h military number (o to 23).

Here is an extract from the help files for the Hour keyword in LS:

"Returns the hour of the day for a date/time argument as an integer from 0 to 23.

Syntax

Hour ( dateExpr )

Elements

dateExpr

Any of the following:

A valid date/time string of String or Variant data type.

For Notes or Domino, LotusScript interprets a 2-digit year designation in a date/time string so that 50 through 99 represent the years 1950 through 1999 and 00 through 49 represent the years 2000 through 2049.

For SmartSuite, LotusScript interprets the years differently. For more information, see the SmartSuite online Help entry entitled Year 2000.

A number within the valid date range: the range -657434 (Jan 1, 100 AD) to 2958465 (Dec 31, 9999 AD), inclusive.

NULL

Return value

Hour returns a Variant containing a value of DataType 2 (Integer). If the dateExpr is a Variant containing the value NULL, then Hour returns NULL. "

HTH

Nicolas Abesdris

Quintessence e-solutions Inc.

Subject: RE: Need time displayed in 24 hour format in LS

… and date/time values will be displayed in the Document Properties dialog and in the debugger according to your system preferences. There is no formatting associated with the value, it’s strictly a UI thing.

Subject: RE: Need time displayed in 24 hour format in LS

I didn’t want to include all the time date code in my post but I am doing that in my script. Here’s a little bit:

Set item = doc.GetFirstItem(“Time”)

Set TimeCheck = item.DateTimeValue

Set twelve = New NotesDateTime(“12:00:00 PM”)

Set thirteen = New NotesDateTime(“13:00:00 PM”)

If TimeCheck.TimeOnly >= twelve.TimeOnly And TimeCheck.TimeOnly < thirteen.TimeOnly Then…

I had been looking through the help and here for information and could not find anything. I will try this Hour function. After looking at the code can you see anything obvious that may stop this from working?

Thanks

Subject: RE: Need time displayed in 24 hour format in LS

Ah… TimeOnly is a string, not a time value, so the comparison would be alphabetical according to the date/time settings on the machine the code runs on. So when you pump “13:00:00” into the datetime object, you get “1:00:00PM” back out when you use TimeOnly if your machine is set for a 12-hour clock.

Subject: RE: Need time displayed in 24 hour format in LS

I would suggest using TimeDifference here. Set a base time of midnight, and just compare the time you get from the field to the base time. TimeDifference compares seconds difference between one time and another through subtraction. If you’re unsure which value is going to be larger and all you actually want is the distance from one another on a time line then you have to abs() the value.

In this case you know that midnight (your base time) will always be less than or equal to the time on the form so you don’t have to bother with abs(). Instead make sure to use the base time as the parameter in the TimeDifference call.

There are 3600 seconds in an hour (60 seconds * 60 minutes) so just integer divide the TimeDifference result by 3600 to get the hour of the day. I used your code snippet as the basis for an example of what I’m getting at:

const ONE_HOUR% = 3600

set item = doc.GetFirstItem(“Time”)

set baseTime = New NotesDateTime(“12:00 AM”)

set compareTime = New NotesDateTime(item.DateTimeValue.TimeOnly)

lngTimeDifference = compareTime.TimeDifference(baseTime)

intHourOfDay = lngTimeDifference\ONE_HOUR

set targetItem = doc.GetFirstItem( “TimeSlot_” + cstr(intHourOfDay) )

And now you can put the text into your target item. I’m making assumputions about how you named the target fields but if you haven’t done something like this where you can just insert a variable string related to the hour of the day I’d seriously consider changing the naming. :slight_smile:

One nice side benefit to this method is if your users hated when you changed the field value to display 24hour time you can change it back for them because that’s irrelevant. You’re just comparing the seconds difference between the time and midnight.

Subject: RE: Need time displayed in 24 hour format in LS

Thank you Jeremiah! I will try this.

Subject: RE: Need time displayed in 24 hour format in LS

This worked great. Since the target field name includes the hour of the day (Time_8 for the 8:00 hour, etc) then I don’t have to run the If statements. I just set the target field “Time_” & intHourOfDay.

Thanks so much!

Subject: RE: Need time displayed in 24 hour format in LS

My pleasure. Glad it worked out! :slight_smile: