How to get last day of the month in Cocoa? - iphone

I would like to make a function that input a NSDate and output the last date of the month. Do you guys know how to do it in Cocoa?

It's the day before the first day of the next month. Add a month, set the day to 1, subtract a day. For setting the day to 1, you'll find it easier to go via NSCalendar. See here for details.

If you're doing date computations, you should always use NSCalendar and related classes, because that's the only way to be forward-compatible with changes to calendars, support for non-"standard" calendars, and so on.
Read the Date and Time Programming Guide section on Calendrical Calculations to get an idea of how these classes work together.

Related

How to find previous weeks date?

The method should retrieve the date in the previous week that corresponds most closely to the specified date.
fromdate = prevMth(systemDateGet());
need to change the above code so that i could get the date of the previous week instead of previous month with respect to the present date.
No function is needed, just use date arithmetic.
systemDateGet() - 7
This will return the prior week date. This of cause implies you use a calendar with 7 day weeks.
Some additional things you may find useful:
wkOfYr(...)
dayOfWk(...)
DateTimeUtil::[VariousFunctionsHere]
In D365FinOps you can also use:
HcmDateTimeUtil::calculateDateWithOffset(PeriodUnit::Day,7,false)

Recoding dates so overnight hours count as one day

I am working with overnight data (in particular I am concerned about the hours 6PM-6AM). I want this time frame to count as “day 1” for my study. My problem is that the date changes at midnight. Can anyone recommend code to reassign the time-frames that I am concerned about as “day1, day 2, etc.” instead of the Julian date. I am new to this so any suggestions would be very much appreciated!
*Update, here is a screenshot of the data I am working with. So, the column on the left is the julian date.. in this example I am looking at day 305 and 306 and the hours 18:20 (305) to 6:20 (306). I want these hours to be classified as day 1, instead of the date changing at midnight. Also, I need to get rid of the random date that is attached to my time column. I'm sure there is an easy way to do that, but if anyone knows off the top of your head I'd appreciate that!
Again, I'm very new to R and coding language, please be gentle!
Thanks a million.
You didn't mention what language you're using.
Given timespans 6pm to 6am, I believe the simplest solution would be to decrement 7 hours from any given date - then all of your timestamps would have the start day of the timespan as their day component.

GWT DatePicker to show specific day of the Week

How do I show only a particular day of the week in gwt-datePicker ?
E.g
If I select January or any month of the year, I want to see only "Tuesdays" for that month.
Cheers
Prince
Short answer: With the default component you cannot.
I would recommend you to create your own widget. Remember that GWT doesn't have support for the Calendar Object, so you'll need to do your own calculations.
I had a similar requirement once where the datepicker popup needed to show the week number in an additional column. Since you cannot subclass/override all the necessary behaviour of the standard classes, there was nothing for it in the end but to rip out the source code and create a new implementation with minor changes.
As for date calculations, there is a port of joda time which you can import client-side. Quite a bit of overkill if you only need the day of the week, though.
http://code.google.com/p/gwt-time/

Set one date from another date

I am using Objective-C within X-code.
I am iterating through a dictionary which contains a date value as one of it's keys. All I want to do is get an array of all the distinct dates so I can use them in a table, as headers. I just plan on
iterating the dictionary and adding dates to a mutable array each time I encounter a new date.
I must set previous date to new date for comparisons to work and I am having a very difficult time figuring out how to set one date equal to another date.
This seems like it should be such a simple thing to do and I am trying to avoid converting the dates to strings first - but if that's what I have to do, then so be it.
Any help would be appreciated.
Thanks,
Gerry O.
If you know the time offset is same as GMT, you could do it by dividing the date's timestamp by 86400 seconds (or three more 0s in milliseconds) and comparing those. If a time offset is there, add or subtract by 3600 seconds per hour before you divide them. But then again, leap years and seconds would break that...
Most languages have libraries support extracting the year, date, month, etc. They take everything into account, usually.
In Objc, you can get a NSDateComponents from NSCalendar's components:fromDate: method. After this, you can call the components to see exactly what each component (I suggest year, month, day) is.
I think you want code to compare dates and You need two loops nested.... where in outer loops iterates with conditional inner loop... In inner loop you just check that previously you had the same date or not...
please go through the below post on same site...
How to compare two dates in Objective-C
hope you will get solution... else clarify your question....

Calculating times based on generic days of the week using NSDate

I'm trying to figure out how to calculate store opening times based on the way they are normally expressed. For example: Monday - Friday, 9am - 6pm. I want to know two things from this criteria. Is the store open? If it is how long will it be before it closes?
My confusion with the documentation is that NSDate calculations expect unique date data sets to perform calculations not generic as expressed above. For example NSDate expects 2010-07-12T09:00:00Z & 2010-07-12T18:00:00Z to form the start basis of the calculation and like wise for the days in between.
Do I need to create a data set for the year on this basis to calculate the results? Or is there a clever way I can get NSCalendar / NSDateComponents to do all this hard work for me?
There is no simple/automatic way. You have to parse the string yourself, form two, let's say startDate and endDate (or something alike). From them you can extract the day of the week and time (that's where NSCalendar and NSDateComponents come in handy).