-1

I want to get localnotification when I set time so In the timepickker i am getting the time format like 07:14 Am/Pm that I required to convert like 19:14 so that I can set the values like

I want to convert 07 to 19 how to do that to put the values in datecomponet..

 //getting time format 07:14 Am/Pm
     NSString * time1 = Timepicker.text;
                NSArray *array = [time1 componentsSeparatedByString:@":"];



            [datecomponet setHour:19];
            [datecomponet setMinute:38];



[dateformater setDateFormat:@"hh:mm a"];
        self.Timepicker.text= [NSString stringWithFormat:@"%@",[dateformater stringFromDate:datepicker.date]];


/// This is local notification code

NSCalendar * gregcalender = [[NSCalendar alloc]initWithCalendarIdentifier:NSCalendarIdentifierGregorian];

    [gregcalender setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"IST"]];

    NSDateComponents *datecomponet = [gregcalender components:NSCalendarUnitYear|NSCalendarUnitMonth |  NSCalendarUnitDay|NSCalendarUnitHour|NSCalendarUnitMinute fromDate:date];

//    [datecomponet setYear:2017];
//    [datecomponet setMonth:12];
//    [datecomponet setDay:16];
//    [datecomponet setHour:18];
//    [datecomponet setMinute:38];

    NSInteger year = [datecomponet year];
    NSInteger month = [datecomponet month];
    NSInteger day = [datecomponet day];


    NSString * time1 = Timepicker.text;
//    NSInteger hour = [datecomponet hour];
//    NSInteger min = [datecomponet minute];
    //NSString *testString= @"It's a rainy day";
    NSArray *array = [time1 componentsSeparatedByString:@":"];

        [datecomponet setHour:18];
        [datecomponet setMinute:38];


    UIDatePicker * dd = [[UIDatePicker alloc]init];
    [dd setDate:[gregcalender dateFromComponents:datecomponet]];

    NSLog(@"======>NSNotification method tab<=====");
VyTcdc
  • 986
  • 10
  • 32
  • Try to use NSDateFormatter instead. – biloshkurskyi.ss Dec 16 '17 at 14:36
  • Used like this but no use [dateformater setDateFormat:@"HH:mm"]; – VyTcdc Dec 16 '17 at 14:41
  • 2
    Neither your description of your problem nor your code make much sense. What is `TimePicker`? Is that a custom class, or an instance of some system UI object? A UIDatePicker doesn't have a text property, so I don't think it's a system date picker. your code also shows you installing values into a variable `datecomponet`, and then a few lines down creating a new local variable with the same name. – Duncan C Dec 16 '17 at 16:28
  • Hey Already One Good Person Gave Answer and my Issue Resloved, If you are not able give answer then keep silent, dont give negative marks. – VyTcdc May 30 '18 at 12:58

1 Answers1

1

Try to do in playground like this:

Swift Elample:

let stringDate = "16:00:00"

let dateFormatter1 = DateFormatter()
dateFormatter1.dateFormat = "HH:mm:ss"
let date1 = dateFormatter1.date(from: stringDate)
print(date1!)

let dateFormatter2 = DateFormatter()
dateFormatter2.dateFormat = "hh:mm a"
print(dateFormatter2.string(from: date1!))
print(dateFormatter1.string(from: date1!))

Objective-C Elample: (paste to any viewDidLoad)

NSString *stringDate = @"16:00:00";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"HH:mm:ss"];
NSDate *date1 = [dateFormatter dateFromString:stringDate];
NSLog(@"%@", date1);

NSDateFormatter *dateFormatter2 = [[NSDateFormatter alloc] init];
[dateFormatter2 setDateFormat:@"hh:mm a"];
NSLog(@"%@", [dateFormatter2 stringFromDate:date1]);
NSLog(@"%@", [dateFormatter stringFromDate:date1]);

Result:

  • 2000-01-01 14:00:00 +0000 / Sat Jan 1 16:00:00 2000 (included my timezone)
  • 04:00 PM
  • 16:00:00

Note: If you want to get:

  • 12 AM, 5 PM then use "hh mm a"
  • 00, 17 then use "HH mm a"
biloshkurskyi.ss
  • 1,358
  • 3
  • 15
  • 34