Skip to main content
Participating Frequently
February 24, 2017
Answered

Can InDesign return the week day to a given date?

  • February 24, 2017
  • 3 replies
  • 2550 views

Hello all,

I am working regularly on event brochures with 200++ events which are cross-referenced several times throughout the document. Often times, in the original data that I get from clients, week days get confused. For example, the script says: Friday, Feb 25th 2017. Unfortunately though, that would have been a Saturday …

I was wondering, if I could make InDesign take the date like a variable? I would write down every date like this: DD.MM.YYYY (since I am in Germany that would be our format). Through magic/witch craft, InDesign could recognize the variable and handle it according to my character-/paragraph-/ or GREP-styles. InDesign would then generate the output: "Samstag, 25.02.17", or "SA 25", or "25. Februar 2017", or "02/25/17", or however I specify the date to be displayed in that table/paragraph/document.

Since week days can be calculated, it should be quite easy? But I haven´t found any solution yet.

If this doesn´t exist or work at all: Do you know any script which will scan a text and – if it recognizes a date – will display the according week day in a tool tip?

Thanks a lot for any input!

Kind regards,

Martin

This topic has been closed for replies.
Correct answer Jongware

Automatically searching for dates and replacing everything it finds may not be safe. Here is a short snippet of JavaScript that will replace just a selected date – you can see how to rebuild a date string, so you can adjust it to taste.

date = new Date(app.selection[0].contents)

datestr = ["Sonntag", "Montag", "Dienstag", "Mittwoch", "Dienstag", "Freitag", "Samstag"][date.getDay()]

datestr += ', '

datestr += ["Jan", "Feb", "Mär", "Apr", "Mai", "Jun", "Jul", "Aug", "Sep", "Okt", "Nov", "Dez"][date.getMonth()]

datestr += ' '+ date.getDate()+', '+date.getFullYear()

alert(datestr);

app.selection[0].contents = datestr

(You can safely remove the 'alert' line, so it will immediately effect the selected text.)

(Edit: huh - the image uses 'getYear', which returns the year from 1900 on, and hence the "117". So, better use "getFullYear" there. I changed it in the script.)

3 replies

TᴀW
Legend
February 27, 2017

This is very scriptable, but there's nothing built-in InDesign to do it.

For instance, this 1-line script will allow you to type in a date (in many formats, though not separated by periods [12.5.87 won't work]; try using slashes [12/5/87]). It will display the date in your local machines current formatting (so if that is set to show the day of the week, you will see that).

alert((new Date(prompt())).toLocaleString());

A lot of customization is possible for the output, of course. And it's not a large step from here to looping through all dates in your document and substituting the correct day...

Ariel

Visit www.id-extras.com for powerful InDesign scripts that save hours of work — automation, batch tools, and workflow boosters for serious designers.
Participating Frequently
February 27, 2017

Nice.

Then it would be the last step before going to print, right? Because it would replace any string that matches [M/D/YY]? Dates would not be handled like cross-references/hyperlinks inside the document, that update themselves whenever a page moves?

Would that script replace every string inside the document with the same output format that is defined through how i set up my computer? Say, in my example I have the table output, which should differ from the output in regular text frames. Both have different paragraph styles – could that be addressed?

Martin

TᴀW
Legend
February 27, 2017

"Then it would be the last step before going to print, right? Because it would replace any string that matches [M/D/YY]?"

I guess so.

"Dates would not be handled like cross-references/hyperlinks inside the document, that update themselves whenever a page moves?"

No. But this makes it sound like you're looking for something different. You could set up perhaps, a running header with the name of the day, on each page. Then, when you move an event to a different page, the day would get picked up by the running-header variable. It depends exactly what  you're after.

"Would that script replace every string inside the document with the same output format that is defined through how i set up my computer? Say, in my example I have the table output, which should differ from the output in regular text frames. Both have different paragraph styles – could that be addressed?"

Anything is possible. It depends what you would want.

I can't offer to write a complete script here for free, but you could (a) ask in the scripting forum -- there are some people there who are getting their feet wet and might be interested in taking this on as a challenge; or (b) contact me through a private message for a quote; or (c) best option: take the plunge yourself and try and put something together in the scripting department (it's not as hard as it looks!) :-)

Ariel

Visit www.id-extras.com for powerful InDesign scripts that save hours of work — automation, batch tools, and workflow boosters for serious designers.
Sandee Cohen
Legend
February 24, 2017

I can do it by inserting an anchored text frame that is linked to an external file or a Content Grabber source.

But the results may not be what you want as the date doesn't break across the lines of body text.

So it depends on your layout.

Show an example of the various layouts and I'll let you know if my technique would work. And then how to do it.

Participating Frequently
February 27, 2017

Wow, thanks for your kind offer Sandee,

I´ll attach two screen shots to show my layouts.

This is (last year´s) table layout with 200+ single dates, each date having it´s own cell:

And this is another instance of date inside a normal text frame:

My idea is to just write down the proper date like DD.MM.YYYY and let ID convert it into the format needed: in the table only into "SA 20", inside the text frame to "Samstag, 12. November 2016", mainly so the week day is added automatically.

Did I get that right: with your solution I would have to provide an additional file with the correct dates for ID to look after?

Curios to see if your idea is applicable

Steve Werner
Community Expert
Community Expert
February 24, 2017

If you don't get an answer here, ask in the InDesign Scripting forum:

InDesign Scripting