我使用datepicker
來挑選time
,但在發送到服務器時,當我檢索回來時,它顯示5-6小時的差異。ios中的日期和時間問題
服務器在美國託管。
所以我如何準確無誤地做到這一點,用戶從任何地方做請求。
感謝,
阿倫
我使用datepicker
來挑選time
,但在發送到服務器時,當我檢索回來時,它顯示5-6小時的差異。ios中的日期和時間問題
服務器在美國託管。
所以我如何準確無誤地做到這一點,用戶從任何地方做請求。
感謝,
阿倫
UTC是要使用的標準時區。以下是獲取UTC日期的代碼
+(NSString *)getCurrentTime{
NSDate *date = [NSDate date];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"yyyy-MM-ddHH:mm:ss"];
[dateFormat setTimeZone:[NSTimeZone timeZoneWithName:@"UTC"]];
NSString *dateStr = [dateFormat stringFromDate:date];
RELEASE_OBJECT(date)
RELEASE_OBJECT(dateFormat)
return dateStr;
}
如果您發送您的iPhone默認的時間到服務器和服務器還與發送,你必須把它然後同時這將是轉換你NSDate
的問題NSString
NSDateFormater
與一些不同的時區。
[dateFormatter setTimeZone:[NSTimeZone systemTimeZone]];
當您使用NSDateFormater
時,使用上述代碼。
希望這會幫助你。
所有最好的!
這是時區問題。您可以使用[yourDate dateByAddingInterval:[NSTimeZone secondsFromGMT]];
。
請嘗試設置語言環境的時間:
以哪種方式NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"dd MM yyyy hh:mm:ss"]; [dateFormatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"] autorelease]]; NSDate* sourceDate = [dateFormatter dateFromString:dateString]; //Timezones NSTimeZone* sourceTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"UTC"]; NSTimeZone* destinationTimeZone = [NSTimeZone systemTimeZone]; //Interval in Timezones NSInteger sourceGMTOffset = [sourceTimeZone secondsFromGMTForDate:sourceDate]; NSInteger destinationGMTOffset = [destinationTimeZone secondsFromGMTForDate:sourceDate]; NSTimeInterval interval = destinationGMTOffset - sourceGMTOffset; //converted date NSDate* destinationDate = [[NSDate alloc] initWithTimeInterval:interval sinceDate:sourceDate] ; NSString *strFinalDate = [dateFormatter stringFromDate:destinationDate]; [dateFormatter release]; [destinationDate release];
你獲得從服務器中的日期? – 2013-03-22 09:34:08
請顯示您的代碼並解釋用於將日期發送到服務器的格式並將其讀回。很可能是一個時區問題。 – 2013-03-22 09:35:18
[我應該複製粘貼這個嗎?](http://stackoverflow.com/a/7213629/1756131) – 2013-03-22 09:36:10