Skip to main content
Participating Frequently
February 16, 2023
Answered

Help with util.scand

  • February 16, 2023
  • 2 replies
  • 1860 views

Good afternoon Community folks! I think I'm having an issue with understanding the purpose of what util.scand is doing. This is a function that converts a date into a number of milliseconds correct?

I'm building a worksheet that has a user put in a date and time (two separate fields) and a number of the calculated fields afterwards is to calculate a certain time afterwards (12 hours, 36 hours, 72 hours). In the past, I was able to use it to pull a date and increment it by 1 but now that this new sheet has a time component I'm finding myself failing to understand what I'm actually doing.

 

var dateStringDay = this.getField("cultureStartHighDate").valueAsString;
var dateStringTime = this.getField("cultureStartHighTime").valueAsString;
var fullDate = dateStringDay + dateStringTime
if (fullDate=="") event.value = ""; 
else {
	var date=util.scand("mm/dd/yy HH:MM", fullDate)
	date.setDate(date.getDate()+<A value here>);
	event.value=util.printd("mm/dd/yy HH:MM", date);
}

 

In the above code, I'm pulling in the start date and the start time as two separate fields then combine them with the fullDate variable. Because they are strings there shouldn't be any math done there... I think? I'm using an if statement to make sure if a field is cleared out it doesn't throw any error messages but if there is something there then the util.scand should bu converting the fullDate into a millisecond value correct?

 

I found this post (https://community.adobe.com/t5/acrobat-sdk-discussions/i-need-help-with-a-form-that-calculates-hours-worked/m-p/13296172#M90123) which I think has most of the answers but my JS knowledge might be too lacking in understanding what is going on in the thread and to convert it to my need. I've also found other post where it seems like they are using the split function to put the dates and times into arrays but if I were to do that I feel like I would need to nest arrays together and I don't know if that is even something I could manage with my knowledge either! Any help or guidance would totally be worth me buying you a beer or something!

This topic has been closed for replies.
Correct answer try67

Thank you both. This is super helpful information. I've also finally learned how to use the JS console in Acrobat to test and look at values so I can better understand what I'm actually doing. I've come up with the following code which seems to ALMOST work:

 

var dateStringDay = this.getField("cultureStartHighDate").valueAsString;
var dateStringTime = this.getField("cultureStartHighTime").valueAsString;

var fullDate = dateStringDay + " " + dateStringTime;
var calcDate = util.scand("mm/dd/yy HH:MM", fullDate);

calcDate.setDate(calcDate.getDate()+1)
event.value = util.printd("mm/dd/yy HH:MM", calcDate)

 

The last problem is figuring out how to increment the time by 12 hours, 24, 32, etc. If I use the +1 in the getDate I get 24 hours later, but if I use +.5 I don't get 12 hours later, the value doesn't seem to change. I'm not sure what I'm missing here though I feel like I'm super close!


The Date object has methods for getting and setting all kinds of properties, not just the actual date itself. You should read up a bit about it... But here you go:

 

var dateStringDay = this.getField("cultureStartHighDate").valueAsString;
var dateStringTime = this.getField("cultureStartHighTime").valueAsString;

if (dateStringDay!="" && dateStringTime!="") {
	var fullDate = dateStringDay + " " + dateStringTime;
	var calcDate = util.scand("mm/dd/yy HH:MM", fullDate);

	calcDate.setHours(calcDate.getHours()+12);
	event.value = util.printd("mm/dd/yy HH:MM", calcDate);
} else event.value = "";

2 replies

try67
Community Expert
Community Expert
February 16, 2023

The scand method is the right one to use, but you have to make sure you provide it with a complete value.

In your case you're only checking if both fields are empty, but if only one of them is you still try to use this method, which would only work partially, or not at all. If the Date field is filled in but the Time field is empty the result will be a Date object pointing to 00:00:00 on that date. If it's the other way around the result will be null.

try67
Community Expert
Community Expert
February 16, 2023

You're also missing the space between the two values when you combine them, although that shouldn't break it.

Participating Frequently
February 17, 2023

Thanks! So I'm looking at changing it up a bit...

 

var dateStringDay = this.getField("cultureStartHighDate").valueAsString;
var dateStringTime = this.getField("cultureStartHighTime").valueAsString;

var date=util.scand("mm/dd/yy", dateStringDay)
var time=util.scand("HH:MM", dateStringTime)
var fullDate = dateStringDay + dateStringTime
date.setDate(date.getDate()+1);
event.value=util.printd("mm/dd/yy HH:MM", date);

 

In my head, I use something like this, where I'm getting the date and time as two values, converting them to this millisecond thing that scand does, then I can combine the two correct? From there, if I want to do my increments by 12 hours, 36, 72, etc I can just add the appropriate millisecond value and print that out? I removed the error checking as I clearly need to think that out a little more.

Bernd Alheit
Community Expert
Community Expert
February 16, 2023

The scand method converts a date into a JavaScript Date object.

Participating Frequently
February 16, 2023

As I understand the date object in JS per the MDN (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date) that is converting the time into a value in milliseconds. I guess maybe I just need to ignore it and do something else? Do I need to make use of Date.parse() with my date and time values combined and input into it?