Output time as: hh mm (rounded off to the nearest 5) am/pm

Discussions and Tech Support specific to the iMacros Firefox add-on.
Forum rules
Before asking a question or reporting an issue:
1. Please review the list of FAQ's.
2. Use the search box (at the top of each forum page) to see if a similar problem or question has already been addressed.
3. Try searching the iMacros Wiki - it contains the complete iMacros reference as well as plenty of samples and tutorials.
4. We can respond much faster to your posts if you include the following information: CLICK HERE FOR IMPORTANT INFORMATION TO INCLUDE IN YOUR POST
Post Reply
cmuld3r
Posts: 18
Joined: Wed Nov 17, 2010 9:16 pm

Output time as: hh mm (rounded off to the nearest 5) am/pm

Post by cmuld3r » Fri Feb 18, 2011 11:12 pm

I have a javascript challenge for the macro I'm working on. I need to output the time as HH (12 hour format), MM (rounded off to the nearest 5 minutes) and AM or PM.

Sample imacro code:

Code: Select all

TAG POS=1 TYPE=SELECT ATTR=ID:ScheduleTimeHH CONTENT=%HH
TAG POS=1 TYPE=SELECT ATTR=ID:ScheduleTimeMM CONTENT=%MM
TAG POS=1 TYPE=SELECT ATTR=ID:ScheduleTimeAMPM CONTENT=%AMorPM
Last edited by cmuld3r on Tue Feb 22, 2011 9:01 pm, edited 1 time in total.
cmuld3r
Posts: 18
Joined: Wed Nov 17, 2010 9:16 pm

Re: Output time as: hh mm (rounded off to the nearest 5) am/

Post by cmuld3r » Tue Feb 22, 2011 5:03 pm

*deleted
Last edited by cmuld3r on Tue Feb 22, 2011 7:37 pm, edited 1 time in total.
cmuld3r
Posts: 18
Joined: Wed Nov 17, 2010 9:16 pm

Re: Output time as: hh mm (rounded off to the nearest 5) am/

Post by cmuld3r » Tue Feb 22, 2011 7:16 pm

I have an incomplete solution. It is not adding an hour when rounding up from 58 or 59 mins to 00, even though I have the following line:

Code: Select all

if (min >57) {hour = hour + 1;}
Please see the js macro below which inputs the time in a wikipedia search box. For the example, the time is set at 10:58AM and should round up to 11:00AM, but outputs 10:00AM as it's not adding the hour correctly when rounded up:

Code: Select all

//SET TIME
//var macroExtractTime;
//macroExtractTime =  "CODE:";
//macroExtractTime +=  "ADD !EXTRACT {{!NOW:hh}}" + "\n";
//macroExtractTime +=  "ADD !EXTRACT {{!NOW:nn}}" + "\n";
//iimPlay(macroExtractTime)

//var hour = (iimGetLastExtract(1));
//var min = (iimGetLastExtract(2));
//var ampm = "AM";

var hour = "10";
var min = "58";
var ampm = "AM";

//ROUND TO NEAREST 5 MIN
var mm1 = min%5; // remainder after division
var mm2 = (parseInt(min/5)); //units of 5 minutes
if (mm1 <=2) {min = mm2*5} //0-2 minutes
else {min = mm2*5+5} //3-4 minutes
if (min >57) {min = "0";}
if (min >57) {hour = hour + 1;}

//AM-PM
if (hour > 11) {ampm = "PM";}
if (hour > 12) {hour = hour - 12;}
if (hour == 0) {hour = 12;}

//Add 0 to less than 10
if (hour < 10) { hour= "0" + hour;}
if (min < 10) { min = "0" + min;}

iimSet("hour", hour.toString());
iimSet("min", min.toString());
iimSet("ampm", ampm.toString());

var macroSetTime;
macroSetTime =  "CODE:";
macroSetTime +=  "URL GOTO=http://en.wikipedia.org/wiki/Main_Page" + "\n";
macroSetTime +=  "TAG POS=1 TYPE=INPUT:TEXT FORM=ACTION:/w/index.php ATTR=ID:searchInput CONTENT={{hour}}:{{min}}<SP>{{ampm}}" + "\n";
iimPlay(macroSetTime)
What am I missing?
Tom, Tech Support
Posts: 3599
Joined: Mon May 31, 2010 4:59 pm

Re: Output time as: hh mm (rounded off to the nearest 5) am/

Post by Tom, Tech Support » Wed Feb 23, 2011 9:40 pm

Hello cmuld3r,

There are a couple of mistakes. First, you're using string values where you need numeric types. Secondly, look at the following two lines:

Code: Select all

if (min >57) {min = 0;}
if (min >57) {hour = hour + 1;}
You have set min = 58 at the beginning of the script, so the first line above ends up setting min to 0, which makes the following if statement false, therefore 1 never gets added to hour. You can always add some alert() statements in the code to test variable values to make sure they are what you expect:

Code: Select all

alert(min);
alert(hour);
if (min >57) {min = 0;}
if (min >57) {hour = hour + 1;}
alert(min);
alert(hour);
Here's the corrected code:

Code: Select all

var hour = 10;
var min = 58;
var ampm = "AM";

//ROUND TO NEAREST 5 MIN
var mm1 = min%5; // remainder after division
var mm2 = (parseInt(min/5)); //units of 5 minutes
if (mm1 <=2) {min = mm2*5} //0-2 minutes
else {min = mm2*5+5} //3-4 minutes
if (min >57) {hour = hour + 1;}
if (min >57) {min = 0;}

//AM-PM
if (hour > 11) {ampm = "PM";}
if (hour > 12) {hour = hour - 12;}
if (hour == 0) {hour = 12;}

//Add 0 to less than 10
if (hour < 10) { hour= "0" + hour;}
if (min < 10) { min = "0" + min;}

iimSet("hour", hour.toString());
iimSet("min", min.toString());
iimSet("ampm", ampm.toString());

var macroSetTime;
macroSetTime =  "CODE:";
macroSetTime +=  "URL GOTO=http://en.wikipedia.org/wiki/Main_Page" + "\n";
macroSetTime +=  "TAG POS=1 TYPE=INPUT:TEXT FORM=ACTION:/w/index.php ATTR=ID:searchInput CONTENT={{hour}}:{{min}}<SP>{{ampm}}" + "\n";
iimPlay(macroSetTime)
Regards,

Tom, iMacros Support
cmuld3r
Posts: 18
Joined: Wed Nov 17, 2010 9:16 pm

Re: Output time as: hh mm (rounded off to the nearest 5) am/

Post by cmuld3r » Fri Mar 04, 2011 6:19 pm

OOPS!

I did a quick workaround it in the meantime by always rounding down, but of course now it works! THANKS!

Here's the full corrected code for anyone who needs it:

Code: Select all

//SET TIME
var macroExtractTime;
macroExtractTime =  "CODE:";
macroExtractTime +=  "ADD !EXTRACT {{!NOW:hh}}" + "\n";
macroExtractTime +=  "ADD !EXTRACT {{!NOW:nn}}" + "\n";
iimPlay(macroExtractTime)

var hour = (iimGetLastExtract(1));
var min = (iimGetLastExtract(2));
var ampm = "AM";

//ROUND TO NEAREST 5 MIN
var mm1 = min%5; // remainder after division
var mm2 = (parseInt(min/5)); //units of 5 minutes
if (mm1 <=2) {min = mm2*5} //0-2 minutes
else {min = mm2*5+5} //3-4 minutes
if (min >57) {hour = hour + 1;}
if (min >57) {min = "0";}

//AM-PM
if (hour > 11) {ampm = "PM";}
if (hour > 12) {hour = hour - 12;}
if (hour == 0) {hour = 12;}

//Add 0 to less than 10
if (hour < 10) { hour= "0" + hour;}
if (min < 10) { min = "0" + min;}

iimSet("hour", hour.toString());
iimSet("min", min.toString());
iimSet("ampm", ampm.toString());

var macroSetTime;
macroSetTime =  "CODE:";
macroSetTime +=  "URL GOTO=http://en.wikipedia.org/wiki/Main_Page" + "\n";
macroSetTime +=  "TAG POS=1 TYPE=INPUT:TEXT FORM=ACTION:/w/index.php ATTR=ID:searchInput CONTENT={{hour}}:{{min}}<SP>{{ampm}}" + "\n";
iimPlay(macroSetTime)
Post Reply