Skip to content

Timestamp generation as a string in ImageJ macros

G20th March 2018

CNo Comments

TImageJ, JavaScript

Generate a timestamp in the format yyyyMMddhhmmss within a macro for ImageJ.

I recently wrote an ImageJ macro, for which I needed to generate a timestamp number string that would be incorporated into the file name of a CSV used to store results generated in the ImageJ results window.

An existing macro that utilised getDateAndTime() did not work as I wanted, because it was alphanumeric the order was incorrect.

Here is a revised version that will generate a timestamp in the format yyyyMMddhhmmss as a variable TimeStamp:

macro "Time Stamp" {
     MonthNames = newArray("01","02","03","04","05","06","07","08","09","10","11","12");
     getDateAndTime(year, month, dayOfWeek, dayOfMonth, hour, minute, second, msec);
     TimeString ="";
     if (dayOfMonth<10) {TimeString = TimeString+"0";}
     TimeString = ""+year+""+MonthNames[month]+""+dayOfMonth+""+TimeString;
     if (hour<10) {TimeString = TimeString+"0";}
     TimeString = TimeString+hour+"";
     if (minute<10) {TimeString = TimeString+"0";}
     TimeString = TimeString+minute+"";
     if (second<10) {TimeString = TimeString+"0";}
     TimeString = TimeString+second;

     print(TimeString); // Prints the time stamp

}

The TimeStamp variable then can be used to assemble file names or for other purposes.

   

Comments:

No comments have yet been submitted. Be the first!

Have your say:

The following HTML is permitted:
<a href="" title=""> <b> <blockquote cite=""> <code> <em> <i> <q cite=""> <strike> <strong>

Comments will be published subject to the Editorial Policy.