0

I searched high and low and cannot find a specific answer that works. I need to add a button to a form in App Maker to record a timestamp, not a date, when clicked. So far the only thing that I've managed to get to work is

widget.datasource.item.Timestamp_OUT = new Date();

I've also tried

var timestamp = getTimeStamp();

But keep getting an error "ReferenceError: "getTimeStamp" is not defined". I'm probably missing the obvious as it shouldn't be this difficult to do something this simple. Any help is appreciated, thanks.

  • 1
    Do you have a function in your client scripts called function getTimeStamp(){}? If not, that would be why your error states getTimeStamp is not defined. I suppose if the first solution works for you, why don't you implement that one? – Markus Malessa Feb 22 '19 at 14:43
  • 1
    If you are working on client script, try this `var timestamp = + new Date()`; – Morfinismo Feb 22 '19 at 14:52
  • If you need your timestamp to be in a specific format and that's what you're having trouble with, try looking into moment.js. – Ian Hyzy Feb 22 '19 at 15:52
  • I've created the following function: --> function getTimeStamp() { var timestamp = + new Date(); } <-- and then updated the onlick to --> var timestamp = getTimeStamp(); widget.datasource.item.Timestamp_IN = new timestamp(); <-- but am now getting the error 'timestamp is not a constructor at NewPage.Form1.Form1Body.Button1.onClick:2:39' – Wayne Strydom Feb 22 '19 at 16:49

2 Answers2

1

Based on your comment:

You got almost everything right with a couple of exceptions. So your getTimeStamp function should look like this:

function getTimeStamp(){
    var timestamp = + new Date();
    return timestamp;
}

That is, because you have to return a value. The other problem is that when you are assigning the value to the datasource item, you don't have to use the keyword new; therefore, it should be like this:

var timestamp = getTimeStamp();
widget.datasource.item.Timestamp_IN = timestamp;

The above is basic javascript. I recommend you to dig into javascript before moving forward with appmaker.

Morfinismo
  • 4,985
  • 4
  • 19
  • 36
  • Thats good advice, I'm busy learning and am challenging myself by building while learning. Everything there now comes back with no error regarding the formulae, but I still cannot get the button to do the job as I'm getting the error **Type mismatch: Cannot set type Number for property Timestamp_OUT. Type Date is expected.at NewPage.Form1.Form1Body.Button1.onClick:2:38 **, Though the field in the SQL table is Date, then changed SQL type to timestamp in the advanced setting. Seems its still expecting a date though? – Wayne Strydom Feb 23 '19 at 16:06
  • @WayneStrydom I see, I started the same way. Good for you! Keep it up! I'd recommend you to change the SQL type of the field from date to number and handle the date conversion with javascript. – Morfinismo Feb 23 '19 at 16:34
  • Thanks man, its a great challenge but learning a lot quickly. Your help so far has been greatly appreciated, its nice not being flamed for asking for help. Cheers! – Wayne Strydom Feb 24 '19 at 21:05
  • could you maybe tell me why the timestamp here is locked to a specific timezone and not taking the time from the users device? Is there a way to change this? – Wayne Strydom Jun 21 '19 at 08:07
  • @WayneStrydom It is not. Unless you are using the code in a server script. Server code is always bound to a time zone. – Morfinismo Jun 21 '19 at 10:50
1

Success! With the help of everyone here and the guys at this link: Google Groups - Solution I've managed to crack the case (and learn some stuff along the way).

Everything originally above works just fine if you take the " + " out of the function so it reads

function getTimeStamp(){
var timestamp = new Date();
return timestamp;
}

Then, onclick of

var timestamp = getTimeStamp();
widget.datasource.item.Timestamp_IN = timestamp;

Next, format the table to show the correct data with

@datasource.item.Test_Timestamp#formatDate('EEEE \x27at\x27 h:mm:ss a')

And "Hey Presto!" you get a button that, when clicked, gives you a timestamp. Thaks to everyone who pitched in, you're the reason people who are learning continue to do so and don't throw in the towel when things get difficult :)