【发布时间】:2014-06-26 06:49:35
【问题描述】:
我有一个日历应用程序,我在其中突出显示当前日期,
我按照以下步骤操作,目前我的设备时间是上午 11:59,
印度时区,我把它改成早上 9 点,然后我把时区改成圣何塞,
我的时间是 6 月 25 日晚上 9 点 34 分,现在在设备日历中显示为 6 月 25 日,但是
在我的应用中,它仍然显示 6 月 26 日。
我通过这种方式获取当前日期
-(void)initialize
{
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
[gregorian setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];
NSDateComponents *weekdayComponents = [gregorian components:(NSDayCalendarUnit | NSYearCalendarUnit | NSMonthCalendarUnit | NSMinuteCalendarUnit)fromDate:[NSDate date]];
NSInteger day = [weekdayComponents day];
NSInteger month = [weekdayComponents month];
NSInteger year = [weekdayComponents year];
[m_dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];
m_dateFormatter.dateFormat = @"dd/MM/yyyy";
NSDateComponents *timeZoneComps=[[NSDateComponents alloc] init];
[timeZoneComps setDay:day];
[timeZoneComps setMonth:month];
[timeZoneComps setYear:year];
[timeZoneComps setHour:00];
[timeZoneComps setMinute:00];
[timeZoneComps setSecond:01];
m_currentDate = [gregorian dateFromComponents:timeZoneComps];
}
所以,在这方面我需要你的帮助。
【问题讨论】:
-
因为您明确使用
UTC时区? -
我删除了它,现在对于 san jose,它工作正常,在我的应用程序中显示 6 月 25 日,但现在如果我将时区更改为早于印度时区的澳大利亚,它应该显示给我6 月 26 日,但显示为 6 月 25 日。为什么会出现这个问题?
-
你好@trojanfoe,你能看看我的评论吗
标签: ios nsdate nsdateformatter nscalendar nstimezone