Reputation: 7
I have string with date time format e.g.: 2013-05-28 10:56:31
How can I format this string to the iPhone default format? (in iPhone settings)
And how to set datetime in GMT plus time shift offset (set in iphone too). e.g. GMT+1
Is this possible?
Upvotes: 0
Views: 2035
Reputation: 10201
You can use NSDateFormatter to achieve this.
NSString *dateString = @"2013-05-28 10:56:31";
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
//Special Locale for fixed dateStrings
NSLocale *locale = [[NSLocale alloc]initWithLocaleIdentifier:@"en_US_POSIX"];
[formatter setLocale:locale];
//Assuming the dateString is in GMT+00:00
//formatter by default would be set to local timezone
NSTimeZone *timeZone = [NSTimeZone timeZoneForSecondsFromGMT:0];
[formatter setTimeZone:timeZone];
[formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate *date =[formatter dateFromString:dateString];
//After forming the date set local time zone to formatter
NSTimeZone *localTimeZone = [NSTimeZone localTimeZone];
[formatter setTimeZone:localTimeZone];
NSString *newTimeZoneDateString = [formatter stringFromDate:date];
NSLog(@"%@",newTimeZoneDateString);
A few pointers for using dateFormatter.
en_US_POSIX
localeUpvotes: 6
Reputation: 1839
See the documentation about NSDateFormatter, specifically dateFromString
, which returns "A date representation of string interpreted using the receiver’s current settings."
Upvotes: 0