# Boilerplate Date Handler

**URL:** <https://discourse.devontechnologies.com/t/boilerplate-date-handler/72736>\
**Category:** Automation\
**Created:** [September 12, 2022, 10:59pm UTC](https://discourse.devontechnologies.com/t/boilerplate-date-handler/72736 "2022-09-12T22:59:26Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![BLUEFROG](https://discourse.devontechnologies.com/user_avatar/discourse.devontechnologies.com/bluefrog/32/135_2.png) [@BLUEFROG](https://discourse.devontechnologies.com/u/BLUEFROG)\
**Post date:** [September 12, 2022, 10:59pm UTC](https://discourse.devontechnologies.com/t/boilerplate-date-handler/72736/1 "2022-09-12T22:59:26Z")

</div>

Many people want to process dates, e.g., for use in a filename. However, the _correct_ format is often colloquial and up for debate.

Here is a little pure AppleScript routine that may be useful for producing a date in the desired format…

```auto
set theDateString to my createDate((current date)) -- Call the handler to create your date string
--> "2022-12-09"

-- Do stuff here, whatever your script calls for

-- Handlers
on createDate(theDate) -- Process Date
	set {year:y, day:d, month:m} to theDate -- Set variables for each date component
	set od to AppleScript's text item delimiters -- Cache the default text delimiters
	set AppleScript's text item delimiters to "-" -- Switch the delimiters to the desired value
	set dateString to {y, my zeropad(d), my zeropad(m as integer)} as string -- Create a string from the date components, zeropadded. Reorder the components, as desired.
	set AppleScript's text item delimiters to od -- Reset the text item delimiters
	return dateString -- Pass the date string back to the script
end createDate

on zeropad(num) -- Add prefixing zero
	if num ≥ 10 then
		return num
	else
		return ("0" & num as string)
	end if
end zeropad

```

---

<div class="post-metadata">

**Author:** ![cgrunenberg](https://discourse.devontechnologies.com/user_avatar/discourse.devontechnologies.com/cgrunenberg/32/7172_2.png) [@cgrunenberg](https://discourse.devontechnologies.com/u/cgrunenberg)\
**Post date:** [September 13, 2022, 7:02am UTC](https://discourse.devontechnologies.com/t/boilerplate-date-handler/72736/2 "2022-09-13T07:02:39Z")

</div>

As I personally prefer as few lines as possible (makes it easier to few more code on the screen), I couldn’t resist to do a shorter version of your script:

```auto
on createDate(theDate) -- Process Date
	return (year of theDate as string) & "-" & my zeropad(day of theDate) & "-" & my zeropad(month of theDate as integer)
end createDate

on zeropad(num) -- Add prefixing zero
	if num ≥ 10 then return (num as string)
	return ("0" & num as string)
end zeropad

```

And soon @chrillek will probably provide an even shorter JavaScript revision 🙂

---

<div class="post-metadata">

**Author:** ![cgrunenberg](https://discourse.devontechnologies.com/user_avatar/discourse.devontechnologies.com/cgrunenberg/32/7172_2.png) [@cgrunenberg](https://discourse.devontechnologies.com/u/cgrunenberg)\
**Post date:** [September 13, 2022, 7:04am UTC](https://discourse.devontechnologies.com/t/boilerplate-date-handler/72736/3 "2022-09-13T07:04:38Z")

</div>

This revision is even shorter but not really beautiful/obvious anymore:

```auto
on zeropad(num) -- Add prefixing zero
	return (characters -2 thru -1 of ("0" & num as string)) as string
end zeropad

```

---

<div class="post-metadata">

**Author:** ![BLUEFROG](https://discourse.devontechnologies.com/user_avatar/discourse.devontechnologies.com/bluefrog/32/135_2.png) [@BLUEFROG](https://discourse.devontechnologies.com/u/BLUEFROG)\
**Post date:** [September 13, 2022, 8:26am UTC](https://discourse.devontechnologies.com/t/boilerplate-date-handler/72736/4 "2022-09-13T08:26:46Z")

</div>

Yes, but there are fewer concepts to learn from in yours 😉

---

<div class="post-metadata">

**Author:** ![Blanc](https://discourse.devontechnologies.com/user_avatar/discourse.devontechnologies.com/blanc/32/8008_2.png) [@Blanc](https://discourse.devontechnologies.com/u/Blanc)\
**Post date:** [September 13, 2022, 8:40am UTC](https://discourse.devontechnologies.com/t/boilerplate-date-handler/72736/5 "2022-09-13T08:40:11Z")

</div>

> [@BLUEFROG](#):
>
> the _correct_ format is often colloquial and up for debate

> [@BLUEFROG](#):
>
> `{y, my zeropad(d), my zeropad(m as integer)}`

I think we can _all_ agree that it isn’t yyyy-dd-mm :D:D

---

<div class="post-metadata">

**Author:** ![BLUEFROG](https://discourse.devontechnologies.com/user_avatar/discourse.devontechnologies.com/bluefrog/32/135_2.png) [@BLUEFROG](https://discourse.devontechnologies.com/u/BLUEFROG)\
**Post date:** [September 13, 2022, 8:42am UTC](https://discourse.devontechnologies.com/t/boilerplate-date-handler/72736/6 "2022-09-13T08:42:05Z")

</div>

Actually I had a support ticket recently and this **exactly** what they wanted. 🤷🏻

---

<div class="post-metadata">

**Author:** ![Blanc](https://discourse.devontechnologies.com/user_avatar/discourse.devontechnologies.com/blanc/32/8008_2.png) [@Blanc](https://discourse.devontechnologies.com/u/Blanc)\
**Post date:** [September 13, 2022, 8:46am UTC](https://discourse.devontechnologies.com/t/boilerplate-date-handler/72736/7 "2022-09-13T08:46:48Z")

</div>

I’m surrounded by people who don’t want the right thing 😆

---

<div class="post-metadata">

**Author:** ![chrillek](https://discourse.devontechnologies.com/user_avatar/discourse.devontechnologies.com/chrillek/32/29340_2.png) [@chrillek](https://discourse.devontechnologies.com/u/chrillek)\
**Post date:** [September 13, 2022, 2:08pm UTC](https://discourse.devontechnologies.com/t/boilerplate-date-handler/72736/8 "2022-09-13T14:08:16Z")

</div>

Taking the bait (but without any warranty since I’m away from my Mac). Unfortunately, it’s a no-brainer.

```javascript
function createDate(date) {
return date.toISOString();
}

```

This will convert a date to its ISO representation, including the time. If the time is not needed, the caller should remove everything after and including the „T“ in the string.

---

<div class="post-metadata">

**Author:** ![DTLow](https://discourse.devontechnologies.com/user_avatar/discourse.devontechnologies.com/dtlow/32/18161_2.png) [@DTLow](https://discourse.devontechnologies.com/u/DTLow)\
**Post date:** [September 13, 2022, 2:24pm UTC](https://discourse.devontechnologies.com/t/boilerplate-date-handler/72736/9 "2022-09-13T14:24:31Z")

</div>

> [@cgrunenberg](#):
>
> a shorter version of your script:

There’s also

```auto
on createDate(theDate) -- Process Date to yyyy-mm-dd
	return text 1 thru 10 of ((theDate) as «class isot» as string)
end createDate
```

---

<div class="post-metadata">

**Author:** ![cgrunenberg](https://discourse.devontechnologies.com/user_avatar/discourse.devontechnologies.com/cgrunenberg/32/7172_2.png) [@cgrunenberg](https://discourse.devontechnologies.com/u/cgrunenberg)\
**Post date:** [September 13, 2022, 2:30pm UTC](https://discourse.devontechnologies.com/t/boilerplate-date-handler/72736/10 "2022-09-13T14:30:04Z")

</div>

Nice. But definitely not human readable for non-scripters and not easily customizable anymore.

---

<div class="post-metadata">

**Author:** ![winter](https://discourse.devontechnologies.com/letter_avatar_proxy/v4/letter/w/2bfe46/32.png) [@winter](https://discourse.devontechnologies.com/u/winter)\
**Post date:** [September 13, 2022, 7:52pm UTC](https://discourse.devontechnologies.com/t/boilerplate-date-handler/72736/11 "2022-09-13T19:52:27Z")

</div>

> [@cgrunenberg](#):
>
> ```auto
> -- Add prefixing zero
> 
> ```

Perhaps all these redundant `return` keywords are a little dysfunctional ?

After all, we’re just defining values … 🙂

AppleScript

```applescript
on zeropad(num) -- Add prefixing zero
	(characters -2 thru -1 of ("0" & num as string)) as string
end zeropad

```

JavaScript

```javascript
let createDate = date =>
    date.toISOString()
    .slice(0, 16)
    .replace("T", " ");

createDate(new Date());

```

---

<div class="post-metadata">

**Author:** ![winter](https://discourse.devontechnologies.com/letter_avatar_proxy/v4/letter/w/2bfe46/32.png) [@winter](https://discourse.devontechnologies.com/u/winter)\
**Post date:** [September 13, 2022, 7:59pm UTC](https://discourse.devontechnologies.com/t/boilerplate-date-handler/72736/12 "2022-09-13T19:59:02Z")

</div>

Though on reflection, we do need `return` if we wrap it up properly 😊

```javascript
(() => {
    "use strict";

    const createDate = date =>
        date.toISOString()
        .slice(0, 16)
        .replace("T", " ");

    return createDate(new Date());
})();

```

---

<div class="post-metadata">

**Author:** ![chrillek](https://discourse.devontechnologies.com/user_avatar/discourse.devontechnologies.com/chrillek/32/29340_2.png) [@chrillek](https://discourse.devontechnologies.com/u/chrillek)\
**Post date:** [September 13, 2022, 10:26pm UTC](https://discourse.devontechnologies.com/t/boilerplate-date-handler/72736/13 "2022-09-13T22:26:13Z")

</div>

Given that @BLUEFROG’s AS implementation only returns the date, I’d go for

```javascript
const createDate = date =>
        date.toISOString()
        .slice(0, 10);

```

---

<div class="post-metadata">

**Author:** ![meta](https://discourse.devontechnologies.com/user_avatar/discourse.devontechnologies.com/meta/32/5928_2.png) [@meta](https://discourse.devontechnologies.com/u/meta)\
**Post date:** [September 19, 2022, 12:15am UTC](https://discourse.devontechnologies.com/t/boilerplate-date-handler/72736/14 "2022-09-19T00:15:00Z")

</div>

Weirdly this seems to be something that’s way easier in Shortcuts. There’s a built-in action “Format” which takes a format string. I just used it to set up a shortcut to add a new journal entry in Markdown to my journal entries folder.

 ![Screen Shot 2022-09-18 at 19.13.31](https://devontech-discourse.s3.dualstack.us-east-1.amazonaws.com/uploads/original/3X/1/2/1290026cc8b6fc51911a64f42b1c7d1988c0e562.png)

---

<div class="post-metadata">

**Author:** ![BLUEFROG](https://discourse.devontechnologies.com/user_avatar/discourse.devontechnologies.com/bluefrog/32/135_2.png) [@BLUEFROG](https://discourse.devontechnologies.com/u/BLUEFROG)\
**Post date:** [September 19, 2022, 12:54pm UTC](https://discourse.devontechnologies.com/t/boilerplate-date-handler/72736/15 "2022-09-19T12:54:17Z")

</div>

Yes, but there are just many other problems and inadequacies with Shortcuts 😉

PS: You also learn nothing from it. The point of my post is a learning opportunity for those interested in scripting and dealing with a common inquiry.

---

<div class="post-metadata">

**Author:** ![DTLow](https://discourse.devontechnologies.com/user_avatar/discourse.devontechnologies.com/dtlow/32/18161_2.png) [@DTLow](https://discourse.devontechnologies.com/u/DTLow)\
**Post date:** [September 19, 2022, 2:18pm UTC](https://discourse.devontechnologies.com/t/boilerplate-date-handler/72736/16 "2022-09-19T14:18:42Z")

</div>

> [@meta](#):
>
> Weirdly this seems to be something that’s way easier in Shortcuts.

I use the date functions on both a Mac (Applescript) and iPad (Shortcuts)  
Not seeing the “weirdly…way easier”; both require a level of skill with the product  
I’m aware that Shortcuts can be used on a Mac, but find it too limited for my use

> I just used it to set up a shortcut to add a new journal entry in Markdown to my journal entries folder.

I use the shortcut with iPad shortcut to create DT notes, pre-coded with a filename date prefix  
On the Mac, it’s an Applescript  
I prefer formatted note (html) notes

---

<div class="post-metadata">

**Author:** ![system](https://devontech-discourse.s3.dualstack.us-east-1.amazonaws.com/uploads/original/2X/4/4e6ff70c96be2eb307a274d890bba8d71ba671b3.png) [@system](https://discourse.devontechnologies.com/u/system)\
**Post date:** [September 18, 2025, 2:18pm UTC](https://discourse.devontechnologies.com/t/boilerplate-date-handler/72736/17 "2025-09-18T14:18:52Z")

</div>

Closed on September 18, 2025, 2:18pm UTC

This topic was automatically closed 1095 days after the last reply. New replies are no longer allowed.
