Expand my Community achievements bar.

SOLVED

Formcalc or Javascript equivilent of =now() in Excel

Avatar

Level 2

Could someone please tell me if there is a Formcalc or Javascript equivilent of =now() in Excel? I know there is that time function, but that seems to come out with a long number that doen't mean anything to me. I want to be able to tab into a field, and the field show the current system time in HH:MM format.

Thanks.

1 Accepted Solution

Avatar

Correct answer by
Level 10

Wouldn't Formcalc be a little easier?

$ = Num2Time(Time(), "HH:MM")

View solution in original post

12 Replies

Avatar

Level 2

In JavaScript you would use the Date object:

// Create variables
var currentDate;
var hours;
var minutes;
var formattedTime;

// Get time
currentDate   = new Date();
hours         = currentDate.getHours();
minutes       = currentDate.getMinutes();

// Format time
if (hours   < 10) { hours = "0" + hours };
if (minutes < 10) { minutes = "0" + minutes };
formattedTime = hours + ":" + minutes;

// Display formatted time
this.rawValue = formattedTime;

In your case, you would put this script in the 'enter' event of your field.

If you are doing this across multiple fields, it would make sence to implement this as a function in a script object.

Ben Walsh

www.avoka.com

Avatar

Former Community Member

JavaScript

var date = new Date();
this.rawValue = date.getHours() + ":" + date.getMinutes();

or

var date = new Date();
var hours = date.getHours();
if (hours > 12) {
    this.rawValue = (hours - 12) + ":" + date.getMinutes() + " PM";
}
else {
    this.rawValue = hours + ":" + date.getMinutes() + " AM";
}

Avatar

Level 2

Unfortunately I can't get any of these to work. What am I doing wrong?

Avatar

Former Community Member

Take a look at the attached. It contains 'Ben' time and 'Steve' time.

Avatar

Former Community Member

The virus scanner takes a while to release attachments. Send an email to stwalker.adobe@gmail.com and I will forward the sample.

Avatar

Level 2

Check the settings in your script editor.

It is common to have the wrong Language set - it should be 'JavaScript'. This defaults to 'FormCalc' until you change it in your form properties (File>Form Properties>Defaults).

It should look like the screenshot below with either 'Ben' time or 'Steve' time:

CurrentTime-screenshot.jpg

Ben Walsh

www.avoka.com

Avatar

Level 2

I can get it to work in a single cell, but not in my table.

Avatar

Level 2

email is advanta@advantacf.com.au.

Avatar

Former Community Member

I have attached a new sample that includes the calculations in a table. I will forward to email as well.

Avatar

Correct answer by
Level 10

Wouldn't Formcalc be a little easier?

$ = Num2Time(Time(), "HH:MM")