12-05-2019 12:57 PM
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
Solved! Go to Solution.
12-05-2019 01:18 PM - edited 12-06-2019 06:09 AM
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! */
12-05-2019 01:18 PM - edited 12-06-2019 06:09 AM
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! */
12-06-2019 03:06 AM
12-06-2019 07:05 AM - edited 12-06-2019 07:34 AM
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 */
12-09-2019 06:14 AM
Thank you, Anthony. Can you please let me know what L indicates in the millisecond value?
Regards,
Libin Benedict
12-09-2019 06:20 AM - edited 12-09-2019 06:23 AM
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.
Discover and save your favorite ideas. Come back to expert answers, step-by-step guides, recent topics, and more.
New here? Get started with these tips. How to use Community New member guide