Sonu
Sonu

Reputation: 957

how to remove second from nsdate

I'm working with local notification. But while I set time for notification, its also consider second.

This is the code where I set time.

NSDateFormatter *df = [[NSDateFormatter alloc] init];
    [df setDateFormat:@"HH:mm"];

    [tfTime setText:[NSString stringWithFormat:@"%@",[df stringFromDate:datePicker.date]]];
    selectedDateTime = [datePicker date];

I want notification on rounded time, without second.

    UILocalNotification *notification = [[UILocalNotification alloc] init];
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc]init];
    //[dateFormat setDateFormat:@"dd-MM-yyyy"];

    //NSDate *notificationDate = [dateFormat dateFromString:strRemindMeBefore];
    notification.fireDate = selectedDateTime;
    notification.alertBody = tvMessage.text;
    notification.timeZone = [NSTimeZone defaultTimeZone];
    notification.soundName = UILocalNotificationDefaultSoundName;
    notification.applicationIconBadgeNumber = 1;

    [[UIApplication sharedApplication] scheduleLocalNotification:notification];

Upvotes: 1

Views: 1296

Answers (2)

Martin R
Martin R

Reputation: 539815

A convenient method to "round" a date to minutes or other units is the rangeOfUnit:… method of NSCalendar.

NSDate *selectedDateTime = [datePicker date];
NSDate *roundedDate;
NSCalendar *cal = [NSCalendar currentCalendar];
[cal rangeOfUnit:NSCalendarUnitMinute startDate:&roundedDate interval:NULL forDate:selectedDateTime];
// …
notification.fireDate = roundedDate;

Upvotes: 4

Harsh
Harsh

Reputation: 2908

What you can do is use the NSDateComponents as follows and set the seconds to 00, which might help... the following is a sample code where you can set all the components manually!

Hope this helps

NSDateComponents *comps = [[[NSDateComponents alloc] init] autorelease];
[comps setDay:1];
[comps setMonth:1];
[comps setYear:2013];
[comps setHour:10];
[comps setMinute:10];
/// You can set this component as 00
[comps setSecond:00];
localNotif.fireDate = [[NSCalendar currentCalendar] dateFromComponents:comps];

Upvotes: 2

Related Questions