Copy link to clipboard
Copied
Hi, in my script I am trying to read the Date and Time using the XMP functions of a selected file in bridge. Then, I want to convert that date and time into a format that looks like: "01/12/10 | 01:23 PM" and write that to another metadata field like the description. Im stuck on trying to convert the date and time into what I want it to look like? Can anyone help, thanks!
Here is the part of the code I'm having trouble with:
When I read the dateTimeOriginal property of the file, it is in an unfriendly format. It looks odd and shows the time zone. The commented lines is code that I have tried, but didn't work. I was trying to convert the XMPDateTime object into a Date object so I can adjust the format, but I haven't been successful.
var myXmpFile = new XMPFile( selectedFile.fsName, XMPConst.UNKNOWN, XMPConst.OPEN_FOR_UPDATE);
var myXmp = myXmpFile.getXMP();
var Name = myXmp.getProperty(XMPConst.NS_EXIF, "DateTimeOriginal");
var date = new XMPDateTime(new Date(Name));
var date2 = new Date(date);
//dateFormat.masks.portfolioDate = 'mm/dd/yy "|" hh:MM TT';
//date.format("portfolioDate");
//date2.format("mm/dd/yy");
alert(date2.toString());
Name.convertToLocalTime();
Name.toString();
alert(Name);
This should be close...
var str = myXmp.getProperty(XMPConst.NS_EXIF, "DateTimeOriginal").toString();
var dat =str.match(/(\d+).(\d+).(\d+).(\d+).(\d+).(\d+)/);
var amOrPm = "AM";
var hours = Number(dat[4]);
if (hours > 11) amOrPm = "PM";
if (hours > 12) hours = hours - 12;
if (hours > 11) amOrPm = "PM";
if(hours <10) hours = '0' + hours;
var dateString = dat[3]+"/"+dat[2]+"/"+dat[1].substr (2) +" | "+ hours +':'+dat[5]+ ' ' +amOrPm;
$.writeln(dateString);
Copy link to clipboard
Copied
You could use somthing like this..
var str = myXmp.getProperty(XMPConst.NS_EXIF, "DateTimeOriginal").toString();
var dat =str.match(/(\d+).(\d+).(\d+)/);
var dateString = dat[3]+"/"+dat[2]+"/"+dat[1].substr (2) +" | "+ str.substr(11);
$.writeln(dateString);
Copy link to clipboard
Copied
Thanks for the quick reply. This worked very well! The only thing I need now is how to convert the time to a regular 12 hour format, rather than 24 hour and to designate AM and PM. Paul, can u help me on that part?
Thanks!
Copy link to clipboard
Copied
This should be close...
var str = myXmp.getProperty(XMPConst.NS_EXIF, "DateTimeOriginal").toString();
var dat =str.match(/(\d+).(\d+).(\d+).(\d+).(\d+).(\d+)/);
var amOrPm = "AM";
var hours = Number(dat[4]);
if (hours > 11) amOrPm = "PM";
if (hours > 12) hours = hours - 12;
if (hours > 11) amOrPm = "PM";
if(hours <10) hours = '0' + hours;
var dateString = dat[3]+"/"+dat[2]+"/"+dat[1].substr (2) +" | "+ hours +':'+dat[5]+ ' ' +amOrPm;
$.writeln(dateString);
Copy link to clipboard
Copied
Thanks for helping me out Paul. I just have one more small problem. I am trying to access the Headline XMP property, and I know i need to use the IPTC Core property, but it doesn't seem to work. Here is my current code, can you help me out?
var selectedFile = thumb.spec;
var myXmpFile = new XMPFile( selectedFile.fsName, XMPConst.UNKNOWN, XMPConst.OPEN_FOR_UPDATE);
var myXmp = myXmpFile.getXMP();
var xmpString = myXmp.getProperty(XMPConst.NS_EXIF, "DateTimeOriginal").toString();
alert(xmpString);
var xmpString2 = myXmp.getProperty(XMPConst.NS_IPTC_CORE, "Headline").toString();
alert(xmpString2);
var dat =xmpString.match(/(\d+).(\d+).(\d+).(\d+).(\d+).(\d+)/);
var amOrPm = "AM";
var hours = Number(dat[4]);
if (hours > 11) amOrPm = "PM";
if (hours > 12) hours = hours - 12;
if (hours > 11) amOrPm = "PM";
if (hours < 10) hours = '0' + hours;
var dateString = dat[2]+"/"+dat[3]+"/"+dat[1].substr (2) +" | "+ hours +':'+dat[5]+ ' ' +amOrPm;
Copy link to clipboard
Copied
The namespace should be Photoshop IE:
var head = myXmp.getProperty(XMPConst.NS_PHOTOSHOP, "Headline");
Hope that helps.
Copy link to clipboard
Copied
Thank you! That did the trick! Thanks for all the help!