cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
3502
Views
10
Helpful
5
Replies

Comparing Dates in UCCX script

LibinBenedict
Level 1
Level 1

I am comparing two date variables in a UCCX script. If the current date and the date in the variable matches, it will process the next step, say play a prompt.

 

Example:
variable1: todaysDate
Type: Date
Value: D[now]

 

variable2: date1
Type: Date
Value: D[11/05/2019]

 

I am doing the comparison as below in the script
If todaysDate.date == date1.date

 

But it seems, it is returning only the value 5, not Nov 5, 2019. Hence the condition is satisfied every month whereas my intention was for Nov 5, 2019 only.

Can you please advise how I can compare 2 full dates?

 

Thanks in advance,

Libin Benedict

1 Accepted Solution

Accepted Solutions

Anthony Holloway
Cisco Employee
Cisco Employee

The first thing you should know, is that a Date object also contains the time, down to the millisecond.

 

Date today
long today_in_millis

Set today = d[now]
Set today_in_millis = today.getTime()
/* today_in_millis is 1575579988372L */

Therefore, when you create today Date objects, while they might share a portion of the same values, they are likely to not be the same down to the millisecond.

 

In fact, if you use d[now], then the hour/minute/second/millisecond potion you cannot see will be set to whatever time it is when that executes (call start, set executes, etc.).  But if you instead use a date literal like d[12/5/2019], the hour/minute/second/millisecond is set to all 0's since you didn't specify it.

 

Date today
long today_in_millis

Set today = d[12/5/2019]
Set today_in_millis = today.getTime()
/* today_in_millis is 1575525600000L */

 

There's like a hundred different ways to solve this, but knowing what we know now, and borrowing what you found out about today.date returning just the day of the month, we could do this:

 

If (todaysDate.year == date1.year && todaysDate.month == date1.month && todaysDate.date == date1.date)
  True
    /* Today matches that other date, yay! */
  False
    /* Nope, not today Junior! */

 

View solution in original post

5 Replies 5

Anthony Holloway
Cisco Employee
Cisco Employee

The first thing you should know, is that a Date object also contains the time, down to the millisecond.

 

Date today
long today_in_millis

Set today = d[now]
Set today_in_millis = today.getTime()
/* today_in_millis is 1575579988372L */

Therefore, when you create today Date objects, while they might share a portion of the same values, they are likely to not be the same down to the millisecond.

 

In fact, if you use d[now], then the hour/minute/second/millisecond potion you cannot see will be set to whatever time it is when that executes (call start, set executes, etc.).  But if you instead use a date literal like d[12/5/2019], the hour/minute/second/millisecond is set to all 0's since you didn't specify it.

 

Date today
long today_in_millis

Set today = d[12/5/2019]
Set today_in_millis = today.getTime()
/* today_in_millis is 1575525600000L */

 

There's like a hundred different ways to solve this, but knowing what we know now, and borrowing what you found out about today.date returning just the day of the month, we could do this:

 

If (todaysDate.year == date1.year && todaysDate.month == date1.month && todaysDate.date == date1.date)
  True
    /* Today matches that other date, yay! */
  False
    /* Nope, not today Junior! */

 

Thanks Anthony for the wonderful explanation.

I will check this out in my set up.

Can you please tell me a couple of other ways this can be achieved?

Regards,
Libin Benedict

Here's one more.  Since we know that the .getTime() method returns a number of milliseconds representing the date and the time, then we could eliminate the time portion from the value, by using math division.

 

Date today = d[now]
Date date1 = d[12/5/2019]
long today_in_millis = 0L
long date1_in_millis = 0L

/* 1000 milliseconds * 60 seconds * 60 minutes * 24 hours = 86,400,000 milliseconds in a day */ Set today_in_millis = today.getTime() / 86400000L Set date1_in_millis = date1.getTime() / 86400000L If (today_in_mills == date1_in_millis) True /* Today is the day you become a uccx jedi */ False /* You got a little more learning to do young padawan */

Thank you, Anthony. Can you please let me know what L indicates in the millisecond value?

 

Regards,

Libin Benedict

It's just the convention in Java to denote the value is a long versus an int.

 

An int has an upper limit of like 2 billion, at which point you need to use a long. The value returned by .getTime() is larger than an int, so I just used longs. The 86400000 didn't need to have the L on it, but I like to keep my types the same in my math expressions.