Applescript transform date format

applescripttime

I'm using this to transform my date from a DD/MM/YYYY to MM/DD/YYYY

set today to short date string of (current date)
set t to (time string of (current date))
set [_day, _month, _year] to [day, month, year] of date today

set _month to _month * 1 --> 3
set _month to text -1 thru -2 of ("0" & _month) --> "03"
set the text item delimiters to "/"
set today to {_month, _day, _year} as string
set fullDateToday to today & " " & t as string

e.g 04/20/2019 > 11/15/18

Although how can I transform the year in YY instead of YYYY ?

Best Answer

NOTE: I took what was described in a comment and converted it to an answer for completeness on this Q&A.

To convert the date so that the year is in YY format vs. YYYY format you could use this line to set _year.

set _year to text -1 thru -2 of ("0" & _year)

Example

Here's a full example of the your AppleScript snippet:

$ cat showdate.sh
#!/bin/bash

osascript <<END
set today to short date string of (current date)
set t to (time string of (current date))
set [_day, _month, _year] to [day, month, year] of date today

set _month to _month * 1 --> 3
set _month to text -1 thru -2 of ("0" & _month) --> "03"
set _year to text -1 thru -2 of ("0" & _year)
set the text item delimiters to "/"
set today to {_month, _day, _year} as string
set fullDateToday to today & " " & t as string
END

And when it's executed we get the following.

Today's date
$ date
Sun Apr 21 00:21:08 EDT 2019
via AppleScript
$ ./showdate.sh
04/21/19 00:20:45

How it works

This AppleScript line:

set _year to text -1 thru -2 of ("0" & _year)

When executed, will set the variable _year to the last 2 digits of the contents of _year prior vale. So if the value was 2019, the above line would result in _year being 19.

  • ("0" & _year) - converts _year's contents to a string by prefixing a 0 to the beginning
  • -1 thru -2 of ... - will slice the last 2 digits from _year, and remove everything else from _year
  • -1 starts indexing from the right instead of the left

References