Search code examples
datesmalltalkpharosqueak

How to set day month and year values for a Date or DataAndTime object in Smalltalk (Pharo / Squeak)


I'm trying to find a a pre-defined method for objects of Class Date or Class DateAndTime, that allows me to create an new Date (or a new DateAndTime) by supplying three integers: one integer for the day of the month (1-31); one for the month (1 - 12); and a four-digit integer for the year.

( The closest I've found so far is Integer>>asYear )

Is there a method that can set all three parameters at once?


Solution

  • If I understand you correctly, you are trying to create an instance of Date from three integers representing the day, month and year of said date.

    When facing a question like this you can browse the class, Date in this case, and check its protocol for instance creation methods. In Pharo there are several methods in this category, but it is easy so see (I think) that #year:month:day looks like a good candidate. So, you can try it. Just evaluate the expression

    Date year: 2015 month: 12 day: 31
    

    and see what happens (you can inspect or print it to see the result).

    You will also find #newDay:month:year as another good candidate. You could try it too. Or you could just see that it sends our previous message and thus it is just a synonymous (which is present for compatibility with other dialects that support the Smalltalk-80/ANSI specification).