problem with int/float input for setting date

Jul 22, 2012 at 4:39pm

problem with int/float input for setting date

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");
}

#29720
Jul 22, 2012 at 7:35pm

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

– Pasted Max Patch, click to expand. –

[attachment=199937,4224]

Attachments:
  1. ScreenShot20120722at12.35.32PM.png
#93422
Jul 22, 2012 at 7:37pm

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…

#93423
Jul 22, 2012 at 8:59pm

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”);
}

#93424

You must be logged in to reply to this topic.