How to set hour minute in nsdate programmatically?

mac picture mac · Jun 4, 2010 · Viewed 11.8k times · Source

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?

Answer

BamboOS picture BamboOS · Oct 26, 2011

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];