Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Convert XMP Date and Time

Participant ,
May 17, 2010 May 17, 2010

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

TOPICS
Scripting
1.7K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Valorous Hero , May 18, 2010 May 18, 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);

Translate
Valorous Hero ,
May 18, 2010 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);

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
May 18, 2010 May 18, 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!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
May 18, 2010 May 18, 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);

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
May 19, 2010 May 19, 2010

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;

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
May 19, 2010 May 19, 2010

The namespace should be Photoshop IE:

var head = myXmp.getProperty(XMPConst.NS_PHOTOSHOP, "Headline");


Hope that helps.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
May 20, 2010 May 20, 2010
LATEST

Thank you!  That did the trick! Thanks for all the help!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines