problem with int/float input for setting date

mharter's icon

Ints and floats coming into a js object are not being handled properly in javascript. The number posts properly, but is treated like a zero by the date constructor. I have tried every technique out there for setting a date by milliseconds, but all seem to result in the output being December 31st 1969. (zero millisecond offset).

I have had good luck with other types of date input, but milliseconds do not work in Max like they do in a web browser.

Any insights?
Thanks!

// inlets and outlets
inlets = 1;
outlets = 1;

function msg_float(millisec)
{
    var d=new Date(millisec);
    var mo=d.getMonth()+1;
    var dt=d.getDate();
    var yr=d.getFullYear();
    post (millisec + " " + mo + " " + dt + " " + yr + "n");
}

MIB's icon
Max Patch
Copy patch and select New From Clipboard in Max.

seems to be working correctly when I test it with this little code snippet. It converts years into ms...

4224.ScreenShot20120722at12.35.32PM.png
png
MIB's icon

or

// inlets and outlets
inlets = 1;
outlets = 1;

function msg_float(millisec)
{

var d=new Date(millisec * 365 * 24 * 60 * 60 * 1000);
var mo=d.getMonth()+1;
var dt=d.getDate();
var yr=d.getFullYear();
post (millisec + " " + mo + " " + dt + " " + yr + "n");
}

Unless I misread your question...

mharter's icon

Thanks!

I think your last example is what is best for my purposes, except that I decided on using a single integer input to represent days instead of years like your example. I am only interested in days anyway. I still don't know why it didn't work when entering the input as milliseconds instead of multiplying a longer interval of time. Maybe the number gets too big to be handled as an integer or float in Max?

// inlets and outlets
inlets = 1;
outlets = 1;
// define global variables and set defaults
function msg_int(days)
{
    var millisecs=days*86400000;
    var d=new Date(millisecs);
    var mo=d.getMonth()+1;
    var dt=d.getDate();
    var yr=d.getFullYear();
    post (millisecs + " " + mo + " " + dt + " " + yr + "n");
}