Help with javascript dates in renaming script

Advanced Renamer forum
#1 : 11/12-13 09:07
Andy Roberson
Andy Roberson
Posts: 2
Sorry if this is a duplicate, I can't find my original post, else this would be a reply to that post.

I've been working on a script all day to evaluate various EXIF dates, determine the earliest valid not-null date of those, and then return that date in the desired format to the calling program via item.newBasename.

I've got it somewhat working, but am having issues moving dates to/from strings. I have programming experience, but I'm new to javascript and to ARen.

Problems I "think" I'm having:

1) Initializing date variables with content from (string?) item.exifToolvalue.<name>.
EX. var WSCreateDate = new Date.parse(item.exifToolValue.CreateDate); <- doesn't seem to be working.

This is of course causing all of the logic around my local dates to fail, which results in today's date being returned instead of the more appropriate date available from EXIF.

2) Today's date is 2013-12-11; however, the value returned in item.newBasename is 2013-11-11 (wrong month). I've checked and checked, and can't figure out why that is happening.

Thanks in advance for any assistance, and of course for the great program! I will make a donation as soon as I can get this working!

Script below. May not be the most efficient javascript, but it does return data. Just not the right data yet.

//
// This function evaluates three dates from EXIF data, and determines the
// earliest populated date of the three. This date will then be returned to the
// calling program for subsequent manipulation, such as removing invalid
// and/or unwanted characters.
//
// INITIALIZE LOCAL WS VARIABLES
//
var WSCreateDate = new Date.parse(item.exifToolValue.CreateDate);
var WSFileCreateDate = new Date.parse(item.exifToolValue.FileCreateDate);
var WSFileModifyDate = new Date.parse(item.exifToolValue.FileModifyDate);
var WSDateTimeOriginal = new Date.parse(item.exifToolValue.DateTimeOriginal);
var WSDateToUseString;
var WSYear;
var WSMonth;
var WSDay;
var WSHours;
var WSMinutes;
var WSSeconds;
//
// INITIALIZE WSDateToUse with current date
//
var WSDateToUse = new Date()
//
// Evalulate each date against WSDateToUse, and move date to the WSDateToUse
// variable if it is "less than" the current value of WSDateToUse, and is
// a valid not-null date
//
if (WSCreateDate<WSDateToUse && WSCreateDate.getFullYear() != null)
{
WSDateToUse=WSCreateDate;
}
if (WSFileCreateDate<WSDateToUse && WSFileCreateDate.getFullYear() != null)
{
WSDateToUse=WSFileCreateDate;
}
if (WSFileModifyDate<WSDateToUse && WSFileModifyDate.getFullYear() != null)
{
WSDateToUse=WSFileModifyDate;
}
if (WSDateTimeOriginal<WSDateToUse && WSDateTimeOriginal.getFullYear() != null)
{
WSDateToUse=WSDateTimeOriginal;
}
//
// Individually extract and concatenate data from the date field into the
// WSDateToUse-String field producing an edit mask of YYYY-MM-DD HH-MM-SS.
// I can't find the JS method that would do this automatically.
// Surely there is one!
//
// Format Month as two-digit text string
if (WSDateToUse.getMonth()<10)
{WSMonth="0"+WSDateToUse.getMonth();}
else
{
WSMonth=WSDateToUse.getMonth();
}
// Format Day as two-digit text string
if (WSDateToUse.getDate()<10)
{WSDay="0"+WSDateToUse.getDate();}
else
{WSDay=WSDateToUse.getDate();}
// Format Hours as two-digit text string
if (WSDateToUse.getHours()<10)
{WSHours="0"+WSDateToUse.getHours();}
else
{WSHours=WSDateToUse.getHours();}
// Format Minutes as two-digit text string
if (WSDateToUse.getMinutes()<10)
{WSMinutes="0"+WSDateToUse.getMinutes();}
else
{WSMinutes=WSDateToUse.getDate();}
// Format Seconds as two-digit text string
if (WSDateToUse.getSeconds()<10)
{WSSeconds="0"+WSDateToUse.getSeconds();}
else
{WSSeconds=WSDateToUse.getSeconds();}
//
// Concatenate above variables into properly formmatted text string for return to calling program
WSDateToUseString=WSDateToUse.getFullYear()+"-"+WSMonth+"-"+WSDay+" "+WSHours+"-"+WSMinutes+"-"+WSSeconds
//
// Move the newly derived WSDateToUse-String value to item.newBasename field
// before returning to calling program.
//
return item.newBasename=WSDateToUseString;

Thanks in advance!
Andy


11/12-13 09:07
#2 : 11/12-13 09:32
Andy Roberson
Andy Roberson
Posts: 2
Reply to #1:

On the current date, specifically the getMonth method, not working, I've narrowed it down to a single line of code. Row 56.

WSMonth=WSDateToUse.getMonth(); <- Returns a value of '11' for current month, should be 12.

If I change it to:

WSMonth=12; <- 12 gets returned to the program as expected. Hence, this seems to be the offending code.

WTF? :-\

Andy


11/12-13 09:32