How to extract yyyy, mm, dd from NSDatePicker?

Hello,
I have a NSDatePicker and i want to get only the year or month or day, how can I do?

Thanks!

Hi,

assuming there is a variable theDate containing datePicker’s dateValue(), you can use this


set unitFlags to 28 -- NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay
set dateComponents to current application's NSCalendar's currentCalendar()'s components:unitFlags fromDate:theDate
log dateComponents's |year|
log dateComponents's |month|
log dateComponents's |day|


If you want a formatted date string, use NSDateFormatter

Thank you!

Is unitFlags an enum?

it’s an enum representing a bit mask

Another thing: I tried with the following code to get only the year of the current date


set formatter to current application's NSDateFormatter's alloc()'s init()
        formatter's setDateFormat:"yyyy"
        set year to formatter's stringFromDate:(current application's NSDate's |date|())
        log year

And in the console I get:

2014-12-02 19:51:48.400 --[1410:324852] <NSAppleEventDescriptor: 'obj '{ 'form':'prop', 'want':'prop', 'seld':'year', 'from':'obj '{ 'form':'name', 'want':'pcls', 'seld':'utxt'("NSObject"), 'from':null() } }>

How can I get the year as integer?

the date format pattern of NSDateFormatter is slightly different and year is a reserved word in AppleScript


       set formatter to current application's NSDateFormatter's alloc()'s init()
        formatter's setDateFormat:"y"
        set |year| to formatter's stringFromDate:(current application's NSDate's |date|())
        log |year| as integer
 

Thanks, but I think the issue was only the private word, as I tried with the date pattern “yyyy” and it worked…

And how can I change only the year, month or day in an NSDatePicker via code?

get the date - do date math - set the date

date math can be accomplished by adding/substracting an absolute NSTimeInterval value or by splitting the date into NSDateComponents and manipulating the units

I’ve opted for the second chance, and this is the code so far:


        set dateParts to current application's NSDateComponents's alloc()'s init()
        set dateParts's |year| to |year|
        set dateParts's |month| to |month|
        set dateParts's |day| to |day|

        set NSGregorianCalendar to current application's NSGregorianCalendar
set gregorian to current application's NSCalendar's alloc()'s initWithCalendarIdentifier:NSGregorianCalendar --THIS
        set currentDate to gregorian's dateFromComponents:dateParts

And I end up with a EXC_BAD_ACCESS, the line which causes this, is the one highlighted.
Where am I wrong?

seems to be a bug. You can use this instead


 set gregorian to current application's NSCalendar's currentCalendar()

Ok, now all is right, except a thing: when I try to make a comparison between the current year and the date picker’s year

if (|year| as integer) > (dateComponents's |year| as integer) then

I get this error:

2014-12-03 19:17:17.655 --[1474:378860] *** -[AppDelegate awakeFromNib]: Can't make «class ocid» id «data optr000000002531320000000000» into type number, date or text. (error -1700)

that means that one of the |year| values is not a number or cannot be coerced to number/integer.
There are very powerful date comparison methods in Cocoa

I’d recommend to avoid those |piped| variables a much as possible