Skip to main content
Inspiring
May 18, 2010
Answered

Convert XMP Date and Time

  • May 18, 2010
  • 1 reply
  • 1969 views

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 topic has been closed for replies.
Correct answer Paul Riggott

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

1 reply

Paul Riggott
Inspiring
May 18, 2010

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

Inspiring
May 19, 2010

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!

Paul Riggott
Paul RiggottCorrect answer
Inspiring
May 19, 2010

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