我从服务器获取此日期/时间,我需要将其转换为本地NSDate
,如何设置NSDateFormatter
的格式字符串?我应该删除一周的第一天字符串吗?
感谢
答案 0 :(得分:3)
尝试
NSString *dateString = @"Wed, 23 Oct 2013 04:56:15 GMT";
NSDateFormatter *formatter = [NSDateFormatter new];
[formatter setLocale:[[NSLocale alloc]initWithLocaleIdentifier:@"en_US_POSIX"]];
[formatter setDateFormat:@"EEE, dd MMM yyyy HH:mm:ss 'GMT'"];
[formatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
NSDate *date = [formatter dateFromString:dateString];
[formatter setDateStyle:NSDateFormatterMediumStyle];
[formatter setTimeStyle:NSDateFormatterMediumStyle];
[formatter setTimeZone:[NSTimeZone localTimeZone]];
NSString *localDateString = [formatter stringFromDate:date];
NSLog(@"%@",localDateString);
有关详细信息,请参阅我的previous answer
答案 1 :(得分:1)
请尝试
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate *date = [df dateFromString:@"Wed, 23 Oct 2013 04:56:15 GMT"];
NSDateFormatter *dfchange = [[NSDateFormatter alloc] init];
[dfchange setDateFormat:@"dd. MMM YYYY"];
NSString *s = [dfchange stringFromDate:date];
答案 2 :(得分:1)
请尝试以下方法: -
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
NSString *dateStr=@"Wed, 23 Oct 2013 04:56:15 GMT";
[dateFormat setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]];
[dateFormat setDateFormat:@"EEE, dd MMM yyyy hh:mm:ss Z"];
NSDate *changeDate=[dateFormat dateFromString:dateStr];
NSLog(@"changeDate=%@",changeDate);
NSString *str=[dateFormat stringFromDate:changeDate];
//getting your local time
NSTimeZone *tz=[NSTimeZone localTimeZone];
//setting yourlocal time
[dateFormat setTimeZone:tz];
NSDate *date = [dateFormat dateFromString:str];
//Setting your desired format
[dateFormat setDateFormat:@"dd-mm-yyyy Z"];
NSString *newDate=[dateFormat stringFromDate:date];
NSLog(@"new date=%@",newDate);
答案 3 :(得分:0)
NSDateFormatter *dateformatter = [[NSDateFormatter alloc] init];
NSLocale *locale1 = [NSLocale currentLocale];
[dateformatter setDateFormat:@"yyyy-MM-dd"];
NSDate *d=[dateformatter dateFromString:@"2013-10-22"];
NSString *base = @"dd-MMM-yyyy,EEEE";
NSString *format1 = [NSDateFormatter dateFormatFromTemplate:base options:0 locale:locale1];
[dateformatter setDateFormat:format1];
NSString* FinalString=[NSString stringWithFormat:@"%@ | %@",[dateformatter stringFromDate:d]
如果您有任何问题,请与我们联系。
答案 4 :(得分:0)
这将有效
NSString *dateStr = @"Wed, 29 Tue 2014 18:22:18 GMT";
NSDateFormatter *format = [[NSDateFormatter alloc] init];
[format setDateFormat:@"EEE, dd MMMM yyyy HH:mm:ss zzz"];
NSDate *date1 = [dateFormat dateFromString:dateStr];
//output
NSLog(@"%@",date1);