[英]NSDateFormatter returning wrong Date String
I am converting NSDate to string using following code: 我正在使用以下代码将NSDate转换为字符串:
+(NSString*) stringFromDate:(NSDate*) date
{
NSDateFormatter *dateFormatter=[[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"MM/dd/yyyy"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"GMT"]];
NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];
[dateFormatter setLocale:locale];
return [dateFormatter stringFromDate:date];
}
It is working fine but when i use following code it returns 1 day less 它工作正常,但是当我使用以下代码时,它返回的时间少了1天
NSString *dateString = [HJDateFormatter stringFromDate:picker.date];
What is wrong with this code???? 此代码有什么问题????
try This code 试试这个代码
+(NSString*) stringFromDate:(NSDate*) date
{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSLocale *posix = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];
[dateFormatter setLocale:posix];
[dateFormatter setTimeZone:[NSTimeZone systemTimeZone]];
[dateFormatter setDateFormat:@"MM/dd/yyyy"];
return [dateFormatter stringFromDate:date];
}
I was able to solve this problem By Aleksey Kozhevnikov answer that is: 我能够解决这个问题,按Aleksey Kozhevnikov的回答是:
"Set your UIDatePicker's timeZone property to the same zone as in your NSDateFormatter" “将UIDatePicker的timeZone属性设置为与NSDateFormatter中相同的区域”
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.