Reputation: 2067
I am converting two strings of date and time to NSDate
but i always get wrong time, but getting correct date, I don't know why xcode is giving wrong log message,I have seem many NSDate
code but all are not working, this one is working when I changed date format, tell me what to do for time format
NSDate * date;
//Assume dateString is populated and of format NSString * dateString =@"2011-11-21 11:20";
NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init];
NSString *dateString =[NSString stringWithFormat:@"%@ %@", socialEvent.SocialEventsDC_EventDate,socialEvent.SocialEventsDC_EventStartTime];
NSLog(@"%@ %@",socialEvent.SocialEventsDC_EventDate,socialEvent.SocialEventsDC_EventStartTime);
[dateFormatter setDateFormat:@"MM/dd/yyyy HH:mm a"];
if (dateString != nil) {
date = [dateFormatter dateFromString:dateString];
// [date release];
}
NSLog(@"%@",date);
my log message is
2013-04-25 10:45:04.417 BNI UK & Ire[450:5203] 01/20/2015 11:00 PM
2013-04-25 10:45:09.679 BNI UK & Ire[450:5203] 2015-01-20 07:00:00 +0000
my time is always 7:00:00 although my time is 11:00, please help me
Upvotes: 0
Views: 286
Reputation: 6491
Please try this code...
NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init];
[dateFormatter1 setDateFormat:@"MM/dd/yyyy hh:mm a"];
[dateFormatter1 setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]];
NSDate *myDate = [dateFormatter1 dateFromString:@"01/20/2015 11:00 pm"];
NSLog(@"mydate is :%@",myDate);
NSLog(@"DATE IS : %@",[dateFormatter1 stringFromDate:myDate]);
Note:timeZone depend upon your local timeZone.
Upvotes: 1
Reputation: 20551
try this code...
NSString * dateString =@"2011-11-21 11:20";
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MM-dd hh:mm"];
// [formatter setTimeZone:[NSTimeZone localTimeZone]];
NSDate *datTemp = [formatter dateFromString:dateString];
NSTimeZone* sourceTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];
NSTimeZone* destinationTimeZone = [NSTimeZone systemTimeZone];
NSInteger sourceGMTOffset = [sourceTimeZone secondsFromGMTForDate:datTemp];
NSInteger destinationGMTOffset = [destinationTimeZone secondsFromGMTForDate:datTemp];
NSTimeInterval interval = destinationGMTOffset - sourceGMTOffset;
NSDate* destinationDate = [[[NSDate alloc] initWithTimeInterval:interval sinceDate:datTemp] autorelease];
NSLog(@"\n\n\n Destination date is:- %@",destinationDate);
Upvotes: 1
Reputation: 10201
If the dateString is as given below this should work, timezone is assumed be your local timeZone. And log of date will be in UTC
NSString *dateString = @"2011-11-21 11:20";
NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm"];
NSDate * date = [dateFormatter dateFromString:dateString];
NSLog(@"%@",date);
Upvotes: 1
Reputation: 8460
try like this ,
NSDate *mydate = [NSDate date];
NSTimeInterval secondsInEightHours = 4 * 60 * 60;
NSDate *dateEightHoursAhead = [mydate dateByAddingTimeInterval:secondsInEightHours];
Upvotes: 1