How to calculate a Julian date for a specific date

I have been trying to find a way to calculate a Julian date and after searching the net found
set julianDate to do shell script “date ‘+%j’”
it works but only uses the current date, can I use a selected date field with this command?
Any guidance gratefully received!

What does ‘selected date field’ mean?

Please provide an example of a date you would wish to work with?

African Julian day or European Astronomical Julian Date? There is also To JulianDateNumbers and back again to Gregorian CalendarDates

Hi.

@SunnyFrinton’s shell script actually returns the day of the year rather than anything particularly Julian. If that’s what’s wanted, and the date can be obtained as an AppleScript date object, here are a couple of ways:

use AppleScript version "2.5" -- OS X 10.11 (El Capitan) or later
use framework "Foundation"
use scripting additions

set ASDate to (current date) -- or some other AppleScript date.

-- This line works, but I'm not sure if it's supposed to! If in doubt, use the commented-out version instead.
set theDate to current application's NSDate's dateWithDate:(ASDate)
--set theDate to current application's NSDate's dateWithTimeInterval:(0) sinceDate:(ASDate)

set formatter to current application's NSDateFormatter's new()
formatter's setDateFormat:("D")
return (formatter's stringFromDate:(theDate)) as text
set theDate to (current date) -- or some other AppleScript date.

copy theDate to jan1
tell jan1 to set {its month, its day, its time} to {January, 1, 0}
return (theDate - jan1) div days + 1 as text

Edit: Subtracted date in second script changed to 1st January for shorter code.

Thank you for the solutions and sorry I misunderstood about Julian/Ordinal dates and what each one means. It was the number of days in a particular date I was trying to achieve!!

Thanks for the link, I was confused between Julian Ordinal date formats!

Thanks for your reply, I have a .csv file of vouchers that have expiry dates so was trying to use the expiry date (ddmmyyyy) to determine if the voucher had expired by trying to use the Unix date command to calculate the number of days that have passed.

You don’t need anything Julian to just get a difference in days. The shell script is returning seconds since the Unix epoch, so to use that you would subtract now from then and divide the result by the number of seconds in a day, with expired/remaining days indicated by a negative/positive result:

set dateString to "06052026" -- ddmmyyyy (expired)
set target to do shell script "date -j -f '%d%m%Y' '" & dateString & "' +%s"
log (target - (do shell script "date +%s")) div 86400

To use AppleScript dates you would do the same kind of math, the date pieces are just arranged differently so you need to manually rearrange them since there isn’t anything like a format string:

set dateString to "06062027" -- ddmmyyyy (good for a year from post date)
tell (current date) to set {now, its time} to {it, 0} -- no time
tell dateString to set {d, m, y} to {text 1 thru 2, text 3 thru 4, text 5 thru 8} -- get the pieces
tell (current date) to set {target, its year, its month, its day, its time} to {it, y, m, d, 0}
log (target - now) div 86400

Hi @red_menace.

Just fooling around with your second script, if you initialise target to a copy of now, it won’t be necessary to zero the time in either of them, because then they’ll have the same time anyway. However, it is advisable in any case to set target’s day to a low number (< 29) before changing the year and month and to set the target day last. This is to avoid any possible overflow into a neighbouring month as the properties are being set:

set dateString to "06062027" -- ddmmyyyy (good for a year from post date)
set now to (current date)
copy now to target
tell dateString to set {d, m, y} to {text 1 thru 2, text 3 thru 4, text 5 thru 8} -- get the pieces
--tell dateString as integer to set {d, m, y} to {it div 1000000, it div 10000 mod 100, it mod 10000}
tell target to set {its day, its year, its month, its day} to {1, y, m, d}
(target - now) div days