我正在創建一個使用日曆創建警報的應用程序。我可以在正確的日期正確設置警報。例如,我爲4th of May 2017 1 PM
設置了一個鬧鐘。將日曆日期轉換爲正確日期
當我嘗試獲取日曆事件時,它會以UTC爲單位返回其他一些日期。
正如你所看到的,它返回我10 AM
與UTC同一天。我想知道如何從日曆中獲取確切的日期。
我正在創建一個使用日曆創建警報的應用程序。我可以在正確的日期正確設置警報。例如,我爲4th of May 2017 1 PM
設置了一個鬧鐘。將日曆日期轉換爲正確日期
當我嘗試獲取日曆事件時,它會以UTC爲單位返回其他一些日期。
正如你所看到的,它返回我10 AM
與UTC同一天。我想知道如何從日曆中獲取確切的日期。
你只需要轉換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];
//除了此代碼,您可能還必須設置timeZone。
NSDateFormatter *format = [[NSDateFormatter alloc] init];
[format setDateFormat:@"MMM-dd-yyyy"];
NSDate *now = [[NSDate alloc] init];
NSString *dateString = [format stringFromDate:now];
NSLog(@"%@",dateString);
Date
始終把當前時區,直到我們改變other.If我們打印Date
它可能會表現出不同,但實際上它需要的電流。
您需要更正時區,或許在自定義日曆中設置時區可能會有幫助。 – NeverHopeless
這應該有所幫助,你只需要將日期轉換爲當前時區即可:http://stackoverflow.com/questions/29392874/converting-utc-date-format-to-local-nsdate – SeanLintern88