Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set UIDatePicker's date's seconds value to 00?

How to always get seconds value "00" for UIDatePicker's date?

Now I am getting value like this format 5:30:45, I need to get this format of time 5:30:00.

How to achieve this?

like image 375
iSara Avatar asked Aug 18 '12 12:08

iSara


1 Answers

I used this code it is working fine.

NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *dateComponents = [calendar components:( NSYearCalendarUnit | NSMonthCalendarUnit |  NSDayCalendarUnit ) fromDate:datePicker.date];
    NSDateComponents *timeComponents = [calendar components:( NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit ) fromDate:datePicker.date];

    [dateComponents setHour:[timeComponents hour]];
    [dateComponents setMinute:[timeComponents minute]];
    [dateComponents setSecond:0.0];

    NSDate *newDate = [calendar dateFromComponents:dateComponents];  
like image 94
iSara Avatar answered Oct 27 '22 20:10

iSara