I have a copyright notice template and I wanted it to auto update every year. Yes, I’m that lazy. So I should be able to do it with an expression, right? Well, yes… and no.
Expressions are Javascript, and they inherit a few undocumented JS features, like all the string manipulation and so on. Something I kinda didn’t expect to work was the Date object.
In Javascript Date() when called without parameters returns the current date. Try it in the console in your browser:
However if you try it in an expression like this
Date()
you get this error:
Someone who knows what they’re doing might give up at this point, but I had a look to see what would happen if I gave it some arguments. So I put it into an expression on the source text property of a text layer. I started with just one argument, Date(0), and weirdly enough, it spat out the current date time. I tried it again with Date(1) and same result:
Weird, because if I construct a Date object properly, using var d = new Date(n) it returns, as you’d expect, a Date object whose date is n millilseconds since the start of the Unix Epoch, namely January 1 1970. So
var d = new Date(1234567890123); d.toDateString()
will return the string Sat Feb 14 2009.
So if I use var d = Date(1) do I get back a Date object? Seems not.
It seems to be a string. Oh, well, regex to the rescue. Yeah as I was saying you can use a lot of JS features in expressions, like regex. Who knew? If I’m after the year I just pull out the part of the string that matches four digits in a row, i.e. [0-9]{4}
TL;DR:
//auto date insertion var d= Date(0); "© Your Name Here" + d.match("[0-9]{4}")[0]
This expression on a text layer will always return a copyright notice with the current year, eg: © pureandapplied 2017
KIRK CHAPMAN
Fantastic! Used it for an application walk through; Many thanks!!