2017-05-04 255 views
-1

我正在創建一個使用日曆創建警報的應用程序。我可以在正確的日期正確設置警報。例如,我爲4th of May 2017 1 PM設置了一個鬧鐘。將日曆日期轉換爲正確日期

當我嘗試獲取日曆事件時,它會以UTC爲單位返回其他一些日期。

enter image description here

正如你所看到的,它返回我10 AM與UTC同一天。我想知道如何從日曆中獲取確切的日期。

+0

您需要更正時區,或許在自定義日曆中設置時區可能會有幫助。 – NeverHopeless

+0

這應該有所幫助,你只需要將日期轉換爲當前時區即可:http://stackoverflow.com/questions/29392874/converting-utc-date-format-to-local-nsdate – SeanLintern88

回答

1

你只需要轉換UTC當地時區

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; 
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]]; 
NSDate *date1 = [dateFormatter dateFromString:@"2017-05-04 10:00:00"]; 

// change to a readable time format and change to local time zone 
[dateFormatter setDateFormat:@"MM/dd/yyyy hh:mm a"]; 
[dateFormatter setTimeZone:[NSTimeZone localTimeZone]]; 
NSString *strCurrentLocalTimezoneDate = [dateFormatter stringFromDate:date1]; 
-1

//除了此代碼,您可能還必須設置timeZone。

NSDateFormatter *format = [[NSDateFormatter alloc] init]; 
[format setDateFormat:@"MMM-dd-yyyy"]; 

NSDate *now = [[NSDate alloc] init]; 

NSString *dateString = [format stringFromDate:now]; 
    NSLog(@"%@",dateString); 
0

Date始終把當前時區,直到我們改變other.If我們打印Date它可能會表現出不同,但實際上它需要的電流。