Setting auto-enter for DATETIME columns - programmatically

For some business rule reason, I need to insert not just TIMESTAMP but also DATE and TIME in the database table.

Also, I am trying to set auto-enter for DATETIME columns programmatically because the auto-enter options are not working for DATETIME fields in Servoy version 5.1.x. (For more info about this see: http://forum.servoy.com/viewtopic.php?f=22&t=14491#p76836)

In any case, I have tried the following code in my create new record function (method):

creation_tstp = application.getTimeStamp();
creation_dt = application.getTimeStamp().getDate();
//creation_time = application.getTimeStamp().getTime();
creation_time = application.getTimeStamp().getHours() + ":" + application.getTimeStamp().getMinutes() + ":" + application.getTimeStamp().getSeconds();

creation_tstp = application.getTimeStamp(); returns the right value (the application timestamp).

I have problems with the other two.

  1. re creation_dt

creation_dt = application.getTimeStamp().getDate(); returns 12/31/1969 (in the db table is inserted as 1969-12-31).

  1. creation_time

creation_time = application.getTimeStamp().getHours() + “:” + application.getTimeStamp().getMinutes() + “:” + application.getTimeStamp().getSeconds(); returns a error. See below:

Caused by: java.lang.IllegalArgumentException: Setting dataprovider with name ‘creation_time’, type ‘DATETIME’ with value of wrong type 10:20:34

Interesting enough, I created a variable to ‘test’ the code that defines creation_time.

var test = application.getTimeStamp().getHours() + ":" + application.getTimeStamp().getMinutes() + ":" + application.getTimeStamp().getSeconds();

It returns what I was expecting. In addition, if I copy and paste (on the entry form) the value from test into creation_time, the value is accepted (inserted) without any error. But the same value cannot be inserted in creation_time = application.getTimeStamp().getHours() + “:” + application.getTimeStamp().getMinutes() + “:” + application.getTimeStamp().getSeconds();. I am not a programmer of the caliber of most people in this forum, but this doesn’t make sense to me.

  1. about getTime()

I don’t understand the getTime() object. It returns a number

1282239314082. Is this wrong?

Therefore I have not been able to use it to get creation_time = application.getTimeStamp().getTime(); It also throws an error.

Any help to resolve these issues will be more than appreciated!

JC

this:

creation_time = application.getTimeStamp().getHours() + ":" + application.getTimeStamp().getMinutes() + ":" + application.getTimeStamp().getSeconds();

creates a string… Not a date object.

So you first have to parse it to a valid date object, something like:

var creation_time_date = utils.dateFormat(creation_time,"hh:mm:ss")

(do look what is the right format for you to use)

Excellent. Thank you very much for the tip.

I did the following:

creation_tstp = application.getTimeStamp();
var creation_dt_var = application.getTimeStamp().getFullYear() + ":" + application.getTimeStamp().getMonth() + ":" + application.getTimeStamp().getDate();
creation_dt = utils.dateFormat(creation_dt_var,"yyyy-MM-dd");
var creation_time_var = application.getTimeStamp().getHours() + ":" + application.getTimeStamp().getMinutes() + ":" + application.getTimeStamp().getSeconds();
creation_time  = utils.dateFormat(creation_time_var,"hh:mm:ss");

It’s all working as expected.

Just one more question, how can I get the user name (string) in a method?

(I know I could figure it out but you may know it by heart.)

As you can see, I want Servoy to make these inserts (at the create, update and delete events) rather than have the database doing it.

Again, thank you very much for your help.

JC

Hi All,

I just noticed a mistake in:

var creation_dt_var = application.getTimeStamp().getFullYear() + ":" + application.getTimeStamp().getMonth() + ":" + application.getTimeStamp().getDate();

The date object in JavaScript defines the month as zero-based. The first month (January) is 0, December is 11, etc, etc…

This is the way I should have written the code to get the right month number:

(application.getTimeStamp().getMonth() + 1)

This is how it all works:

creation_tstp = application.getTimeStamp();
var creation_dt_var = application.getTimeStamp().getFullYear() + "-" + (application.getTimeStamp().getMonth()+1)  + "-" + application.getTimeStamp().getDate();
creation_dt = utils.dateFormat(creation_dt_var,"yyyy-MM-dd");
var creation_time_var = application.getTimeStamp().getHours() + ":" + application.getTimeStamp().getMinutes() + ":" + application.getTimeStamp().getSeconds();
creation_time  = utils.dateFormat(creation_time_var,"hh:mm:ss");
creation_user = security.getUserName();

BTW, there are various way to implement these auto-enter functions. I’d declare global variables, create global methods and link these methods to the tables’ events. But the update, where I need to insert modification date, modification time, etc. I’ll do at the form level because I need to tailor those events to specific fields/columns and not to the table as a whole.

jcarlos:
Just one more question, how can I get the user name (string) in a method?

security.getUserName();