1

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 ?

2
  • Ok not sure why I add an question it just so obvious : set _year to text -1 thru -2 of ("0" & _year)
    – Kevin
    Commented Apr 20, 2019 at 18:23
  • You can use the edit function above the comments here to make changes.
    – bmike
    Commented Apr 20, 2019 at 21:06

1 Answer 1

4

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

1
  • @Kevin sure thing, thanks for the Q it was fun to figure out.
    – slm
    Commented Apr 22, 2019 at 18:51

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .