Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Issue in conversion time to date

I am having time in formate "17-02-2014 05:00 PM",Now i want to convert it in 17-02-2014 05:00 :00 +000 .I am using code. in this date = 17-02-2014 and time = 05:00 PM

-(NSDate*)dateFromDate:(NSString*)date andTime:(NSString*)time{
    NSString *eventDttm = [[NSString alloc] init];
    eventDttm = [NSString stringWithFormat:@"%@ %@",date,time];

    NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
    NSLocale *local = [NSLocale currentLocale];
    [dateFormatter setLocale:local];
    [dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
    [dateFormatter setDateFormat:@"dd-MM-yyyy HH:mm a"];

    //conversion of NSString to NSDate

    NSDate *dateFromString = [[NSDate alloc] init];
    dateFromString = [dateFormatter dateFromString:eventDttm];

    return dateFromString;
}

Using this code i am getting 17-02-2014 11:00 :00 +000

like image 299
Rajeev Upadhyay Avatar asked Feb 13 '23 18:02

Rajeev Upadhyay


1 Answers

you can use this code

NSString *str=@"17-02-2014 05:00 PM";
NSDateFormatter *dateFormatter=[[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:@"dd-MM-yyyy HH:mm a"];
NSDate *datetoday=[dateFormatter dateFromString:str];
[dateFormatter setDateFormat:@"yyyy-MM-dd hh:mm:ss Z"];
NSString *strdate=[dateFormatter stringFromDate:datetoday];
 NSLog(@"%@",strdate);
like image 61
mahi sharma Avatar answered Feb 23 '23 02:02

mahi sharma