How to add certain hours or minutes to a datetime in Cache ObjectScript

361 views
Skip to first unread message

Joy Yao

unread,
Oct 4, 2013, 12:07:20 PM10/4/13
to Ensemble-in...@googlegroups.com
Hi,

Is there any sample code to add certain hours (or minutes) to a date time in Cache ObjectScript? 

Thanks,
Joy

Nigel Timothy Bloom-Salm

unread,
Oct 4, 2013, 2:31:49 PM10/4/13
to Ensemble-in...@googlegroups.com
There may be quicker functions or SQL functions that would do the truck but here is code that demonstrates the conversion of dates and times from display formats to internal cache formats as well as the use of the various datetime functions, as well as operators such as integer divide "\" and modulo "#"

AddTime(datetime, addtime)
 I '$length($get(datetime)) quit "" ; no datetime passed in
 I '$length($get(addtime)) quit datetime ; no time added, return original value
 ; assume datetime is passed in ODBC format as is addtime
 ; e.g. "2013-10-06 14:35:23" and "08:24:45"
 Set datetime = $zdth(datetime,3),date=$piece(datetime,",",1),time=$piece(datetime,",",2)+$zth(addtime,3)
 If time>86400 set date=date+(time\86400),time=time#86400
 ; return ODBC datetime
 Quit $zdt(date_","_time,3)

Nigel Timothy Bloom-Salm
--
You received this message because you are subscribed to the Google Groups "InterSystems: Ensemble in Healthcare Community" group.
To post to this group, send email to Ensemble-in...@googlegroups.com
To unsubscribe from this group, send email to Ensemble-in-Healt...@googlegroups.com
For more options, visit this group at http://groups.google.com/group/Ensemble-in-Healthcare?hl=en
---
You received this message because you are subscribed to the Google Groups "InterSystems: Ensemble in Healthcare" group.
To unsubscribe from this group and stop receiving emails from it, send an email to Ensemble-in-Healt...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Nigel Timothy Bloom-Salm

unread,
Oct 4, 2013, 2:43:11 PM10/4/13
to Ensemble-in...@googlegroups.com
Having thought a bit more about the code I wrote the if time>86400 .... Is not required

You can just use:

Set date=date+(time\86400),time=time#86400

Nigel Timothy Bloom-Salm

On 04 Oct 2013, at 6:07 PM, Joy Yao <joy...@gmail.com> wrote:

--

Dale du Preez

unread,
Oct 4, 2013, 2:58:38 PM10/4/13
to Ensemble-in...@googlegroups.com
Hi Joy,

I think one of the simplest ways of achieving this is to make use of the $system.SQL.DATEADD() method, which exposes the SQL DATEADD() function. The syntax is fairly easy to understand (except for one or two distinctions -- I always mix up "mm" for months and "mi" for minutes), and easy to read as well as handling ODBC and internal time formats.

You can call the function as follows:
    Set tTimeType = "month"
    Set tTimeIncrement = 1
    Set tNextMonth = $system.SQL.DATEADD(tTimeType, tTimeIncrement, tYourDate)

I hope that helps,
Dale

Lawrence Harris

unread,
Oct 4, 2013, 2:05:48 PM10/4/13
to <Ensemble-in-Healthcare@googlegroups.com>
Depends but you can access the SQL date functions like:

USER>w $system.SQL.DATEADD("week",1,"2013-10-01")
2013-10-08 00:00:00
USER>w $system.SQL.DATEADD("minute",1,"2013-10-01")
2013-10-01 00:01:00

Look up the DATEADD documentation.  All the SQL date functions are available as $System.SQL.<name>

It's also pretty simple to just take $H and adjust it with a few simple statements and the $ZDateTime/$ZDateTimeH functions to convert back and forth with formatted dates if needed.

Lawrence Harris

Reply all
Reply to author
Forward
0 new messages