Dear Friends,
I have been banging my head for hours on this issue, I guess I am too tired to find a solution:
I have this Applescript date: “Tuesday, October 7, 1997 12:00:00 AM”. (reference date)
I need to get a Julian date from this reference date to the date of “21/04/2012” (April 21st)
The result should be shown in integer : 5310 (days)
I had this line that worked for many years:
set calculated_julian_date to ((the_date - (reference_date)) / days) div 1
But I can’t do this, because if I simply:
date(the_date)
I get this weird result:
<geraLinhaDigitalClass @0x400d0bc00 : OSAID(15)>
Thank you for any help…
Have a look in my book at the gotchas chapter – that explains the problem with using date as a specifier, as well as the workarounds.
Thanks Shane,
if i use this line:
set the_date to current application's date "Tuesday, October 7, 1997 12:00:00 AM" as date
It results in:
<NSAppleEventDescriptor: 'ldt '($002C5FB000000000$)>
And when I try to do the maths, it results in this error:
Can’t make «script» into type number or date. (error -1700)
I guess I have to go to a 100% objc solution…
I am able to set the reference date using this code:
set reference_date to current application's NSDate's |date|()
set theCal to current application's NSCalendar's currentCalendar()
set theComponents to theCal's components_fromDate_(current application's NSMonthCalendarUnit,reference_date)
set theComponents's |month| to 10
set theComponents's |Day| to 7
set theComponents's |Year| to 1997
set theComponents's |Minute| to 0
set theComponents's |Hour| to 0
set theComponents's |Second| to 0
set newDate to theCal's dateFromComponents_(theComponents)
I think to retrieve the count of days in between the reference_date and the_date I need to use the equivalent in ASOC to this method:
- (NSTimeInterval)timeIntervalSinceDate:(NSDate *)anotherDate
Can someone help me write the line above in ASOC?
Thanks!
I wrote this and it works:
-(NSString *)returnJulianCount:(NSString *)theInputDate theReferenceDate:(NSString *) theReferencedate{
NSDateFormatter *dateFormat1 = [[NSDateFormatter alloc] init];
[dateFormat1 setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate *dateEvent1 = [dateFormat1 dateFromString:theInputDate];
NSDateFormatter *dateFormat2 = [[NSDateFormatter alloc] init];
[dateFormat2 setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate *dateEvent2 = [dateFormat2 dateFromString:theReferencedate];
NSTimeInterval timeDifference = [dateEvent1 timeIntervalSinceDate:dateEvent2];
NSLog(@"timeDifference = %.0f",timeDifference);
double minutes = timeDifference / 60;
double hours = minutes / 60;
double seconds = timeDifference;
double days = minutes / 1440;
NSString *dayCount = [NSString stringWithFormat:@"%f",days];
[dateFormat1 release];
[dateFormat2 release];
return dayCount ;
}
Just call it from my AS like this:
set dataJuliana to GetJulianDateClass's returnJulianCount_theReferenceDate_("2012-04-21 03:00:00","1997-10-07 03:00:00")
I hope it helps someone…
Thanks!
Bernardo, your method is just a date subtraction regardless of the calendar type.
What is the characteristic of the julian calendar?
By the way, this is a shorter version, which returns the number of days as an integer
- (NSString *)returnJulianCount:(NSString *)theInputDate theReferenceDate:(NSString *) theReferencedate{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate *inputDate = [dateFormatter dateFromString:theInputDate];
NSDate *referenceDate = [dateFormatter dateFromString:theReferencedate];
[dateFormatter release];
return [NSString stringWithFormat:@"%.0f", [inputDate timeIntervalSinceDate:referenceDate] / 86400];
}
fundidor:
Thanks Shane,
if i use this line:
set the_date to current application's date "Tuesday, October 7, 1997 12:00:00 AM" as date
It results in:
<NSAppleEventDescriptor: 'ldt '($002C5FB000000000$)>
Well that is almost an AppleScript date – it just needs coercing to one. Make you need parentheses:
set the_date to (current application's date "Tuesday, October 7, 1997 12:00:00 AM") as date
Dear Shane and StefanK,
Thank you for being so kind.