NSCalendar dateWithComponents 给出错误的日期

发布于 2024-10-30 02:14:41 字数 1473 浏览 0 评论 0原文

我必须用 dateComponents 来详细说明日期:

int year;
int month;
int day;
int hour;
int minute;
int second;
NSCalendar *calendar;
NSDate *actualDate;
NSDate *itemDate;
NSDateComponents *dateComps;
NSDateFormatter *dateFormatter;
UILocalNotification *sveglia;

actualDate = [NSDate date];
calendar = [[NSCalendar currentCalendar] copy];
dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeZone:[calendar timeZone]];
[dateFormatter setDateFormat:@"yyyy"];
year = [[dateFormatter stringFromDate:actualDate] intValue];
[dateFormatter setDateFormat:@"MM"];
month = [[dateFormatter stringFromDate:actualDate] intValue];
[dateFormatter setDateFormat:@"dd"];
day = [[dateFormatter stringFromDate:actualDate] intValue];
[dateFormatter setDateFormat:@"HH"];
hour = [[dateFormatter stringFromDate:actualDate] intValue];
[dateFormatter setDateFormat:@"mm"];
minute = [[dateFormatter stringFromDate:actualDate] intValue];
[dateFormatter setDateFormat:@"ss"];
second = [[dateFormatter stringFromDate:actualDate] intValue];
dateComps = [[NSDateComponents alloc] init];
[dateComps setTimeZone:[calendar timeZone]];
[dateComps setDay:day];
[dateComps setMonth:month];
[dateComps setYear:year];
[dateComps setHour:hour];
[dateComps setMinute:minute];
// Obtaining date from components.
itemDate = [calendar dateFromComponents:dateComps];
NSLog(@"ItemDate: %@", itemDate);

日历具有正确的时区,日期的所有组件都是正确的。出色地, 无论我做什么,itemDate 对象都有格林威治时区(+0000)。我应该如何解决这个问题?

I have to elaborate a date with dateComponents:

int year;
int month;
int day;
int hour;
int minute;
int second;
NSCalendar *calendar;
NSDate *actualDate;
NSDate *itemDate;
NSDateComponents *dateComps;
NSDateFormatter *dateFormatter;
UILocalNotification *sveglia;

actualDate = [NSDate date];
calendar = [[NSCalendar currentCalendar] copy];
dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeZone:[calendar timeZone]];
[dateFormatter setDateFormat:@"yyyy"];
year = [[dateFormatter stringFromDate:actualDate] intValue];
[dateFormatter setDateFormat:@"MM"];
month = [[dateFormatter stringFromDate:actualDate] intValue];
[dateFormatter setDateFormat:@"dd"];
day = [[dateFormatter stringFromDate:actualDate] intValue];
[dateFormatter setDateFormat:@"HH"];
hour = [[dateFormatter stringFromDate:actualDate] intValue];
[dateFormatter setDateFormat:@"mm"];
minute = [[dateFormatter stringFromDate:actualDate] intValue];
[dateFormatter setDateFormat:@"ss"];
second = [[dateFormatter stringFromDate:actualDate] intValue];
dateComps = [[NSDateComponents alloc] init];
[dateComps setTimeZone:[calendar timeZone]];
[dateComps setDay:day];
[dateComps setMonth:month];
[dateComps setYear:year];
[dateComps setHour:hour];
[dateComps setMinute:minute];
// Obtaining date from components.
itemDate = [calendar dateFromComponents:dateComps];
NSLog(@"ItemDate: %@", itemDate);

calendar has the correct TimeZone, all the components of the Date are rights. Well,
no matter what I do, itemDate object has the Greenwich TimeZone (+0000). How should I fix this problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

凉栀 2024-11-06 02:14:41

已解决...自从日期正确后,问题就不存在了,NSDate 描述始终给出 GMT 时间的输出。
要查看正确的格式,您应该使用descriptionWithLocale,请查看该格式的文档。

来源

Solved... The problem did not exists since the moment that the date was correct, the NSDate description give always the output in GMT time.
To see the correct format you should use descriptionWithLocale, check the documentation for this one.

Source

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文