Reputation: 453
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
Upvotes: 4
Views: 183
Reputation: 1918
Please use this code,
NSString *dateStr = @"17-02-2014 05:00 PM";
NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init];
[dateFormatter1 setDateFormat:@"dd-MM-yyyy hh:mm a"];
NSDate *date = [dateFormatter1 dateFromString:dateStr];
NSDateFormatter *dateFormatter2 = [[NSDateFormatter alloc] init];
[dateFormatter2 setDateFormat:@"dd-MM-yyyy hh:mm:ssZZ"];
dateStr = [dateFormatter2 stringFromDate:date];
NSLog(@"date : %@",dateStr);
using this code you will get string, date : 17-02-2014 05:00:00+0530 Thanks.
Upvotes: 0
Reputation: 41
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);
Upvotes: 4
Reputation: 3260
Replace your code with below code...I tested in xcode and it is giving your desired result You will get your result in "prettyDate" string.
NSString *eventDttm = [[NSString alloc] init];
eventDttm = [NSString stringWithFormat:@"17-02-2014 05:00 PM"];
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd-MM-yyyy hh:mm a"];
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [dateFormatter dateFromString:eventDttm];
[dateFormatter setDateStyle:NSDateFormatterShortStyle];
[dateFormatter setTimeStyle:NSDateFormatterShortStyle];
[dateFormatter setDateFormat:@"dd-MM-yyyy hh:mm:ss a"];
NSString *prettyDate = [dateFormatter stringFromDate:dateFromString];
prettyDate = [prettyDate stringByAppendingString:@" +000"];
Let me know it is working or not!!!
Happy Coding...!!!
Upvotes: 1
Reputation: 4544
I think your issue is the "HH"
in your date format string. "HH"
is for a 24 hour clock, but your input is in 12 hour. Try using "hh"
.
See: http://www.unicode.org/reports/tr35/tr35-25.html#Date_Format_Patterns
As an aside, there are a couple of points in your code where you're creating a new object and discarding it instantly:
NSString *eventDttm = [[NSString alloc] init];
- Create new NSString object and assign to eventDttm.
eventDttm = [NSString stringWithFormat:@"%@ %@",date,time];
- Create another new NSString object and assign that to eventDttm. The string created on the previous line is now no longer referenced so will be destroyed in ARC, or abandoned in manual memory management.
The same thing happens when you declare dateFromString
.
Upvotes: 2
Reputation: 25459
Change time format string from HH to hh. HH you use when you have 24 hour time. In your example you have just 12h time.
Upvotes: 1