Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set hour minute in nsdate programmatically?

I have 2:01:20 PM format in one label; after some calculations, I need to change the hour and/or minute of that label.

That is ,Is it possible to change that label value, by converting it as NSDate object and modifying hour and minute like 3:20:30 PM?

Are there any setter methods for this?

like image 228
mac Avatar asked Jun 04 '10 12:06

mac


2 Answers

The answer to your first question "how to set hour minute in nsdate programatically?" is following:

    NSDate* result;
    NSDateComponents *comps = [[NSDateComponents alloc] init];
    [comps setMinute:0];
    [comps setHour:0];
    NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    result = [gregorian dateFromComponents:comps];
    [comps release];
like image 109
BamboOS Avatar answered Nov 09 '22 20:11

BamboOS


If you need to calculate specific real instances in time e.g. 3:20:30 PM Oct 23, 2010, then you want to use NSCalendar which lets you decompose dates and do date related calculations.

Word of warning. Date and time calculations are surprisingly complex and detailed. Expect to spend a couple of hours learning the API.

like image 22
TechZen Avatar answered Nov 09 '22 20:11

TechZen