2016-04-21 57 views
0

我正在處理在不同國家有用戶的應用程序。例如印度和美國。設置時間00:00:00在NSDate當時區不是本地

我希望NSDate在任何時區都不用時間。

例如,2016-03-01將在兩個時區2016-03-01 00:00:00 +0000保持不變。

我使用

NSDate* dateOnly = [[NSCalendar currentCalendar] dateBySettingHour:0 minute:0 second:0 ofDate:[NSDate date] options:NSCalendarMatchLast]; 

不過我得到這個: 2016年4月21日05:00:00 +0000

我需要2016年4月21日00:00 + 0000

請建議適當的解決方案。提前致謝。

回答

1
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 
dateFormatter.dateFormat = @"yyyy-MM-dd'T'HH:mm"; 

NSTimeZone *gmt = [NSTimeZone timeZoneWithAbbreviation:@"GMT"]; 
[dateFormatter setTimeZone:gmt]; 
NSString *timeStamp = [dateFormatter stringFromDate:[NSDate date]]; 

嘗試上面的代碼行。希望它能幫助你。

+0

我需要NSDate的,不是的NSString。 –

+0

您可以將日期轉換爲字符串 –

0

你可以試試嗎?

NSTimeZone * timeZone = [NSTimeZone timeZoneWithName:@「UTC」];
[dateFormatter setTimeZone:timeZone];

hope help you this

2

這一個應該工作

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:@"UTC"]]; 
NSLog(@"%@", [dateDormatter stringFromDate:dateOnly]); 
相關問題