UNIX Timestamp

steprec's icon

I have been looking for an object to translate [date] to UNIX time with no luck. Is it not included in Max? Am I missing something?

Jan M's icon

I wouldn't know that there is an object for that, but you can get the unix timestamp in JavaScript. Check the web or the Date() object in js.

The simplest implementation would be:

function bang() {
var myDate = new Date();
outlet(0, myDate.getTime()) // will send out the unix time in milliseconds when receiving a bang
}

j

steprec's icon

That is a really good idea! Thanks :) Can I make it update every second somehow?

Jan M's icon

sure, the Task object can take car of this:

var timer = new Task(_timestamp);
timer.interval = 1000; // Change this to the interval you need (in ms)

function msg_int(onOff)
{
    if(onOff == 1) {
        if(! timer.running) {
            timer.repeat();
        }
    } else {
        timer.cancel();
    }
}

function _timestamp()
{
    var date = new Date();
    outlet(0, date.getTime());
}
_timestamp.local = 1;

Send 1/0 to the [js] to switch timer on/off

cheers!

steprec's icon

You just saved my day Sir! Kudos!

Misha Rabinovich's icon

Hello,

I am using the exact same code but getting the same timestamp everytime, it is the time I saved the js file. I am using Max 6.1.10.
Any thoughts would be greatly appreciated!

Cheers,
Misha

Glyn Hanmer's icon

I'm also having the same problem as Misha Max on 6.1.10.
Did anyone ever have the problem and them manage to sort it?

Regards,
Glyn.

pdelges's icon

I confirm that it doesn't work under Max 6.1.10 but does work with Max7.
I tried to get UTC time with Java instead of JavaScript, but strangely I couldn't get the right time.

Sorry,

p

jko's icon

My Guess is unixTime Stamp exceeds the maximum number max can handle an if that's that it's more a question of 32bit vs 64bit.
If you need the unixTime Stamp as String you can Convert it to a string in JS land though.

Max Patch
Copy patch and select New From Clipboard in Max.
(new Date()).getTime()+'';

Best, J

pdelges's icon

Exactly! (I don't know why my Max6.1 was set to 32 bits…)
Thanks.

Evangelos Angelakis's icon

Hello!
Thank you all for the helpful comments!
Could somebody please help me by converting the above MaxPatch by @JKO to output 16 digits (microseconds) of the timestamp instead of the 13 (milliseconds) it now does? I need to synchronize data I will collect for my PhD experiment and these include time sensitive events...

Thanks again in advance!!
V.