使用 NSDateFormatter 管理日期

发布于 2024-11-09 18:47:20 字数 831 浏览 0 评论 0原文

我尝试使用 NSDateFormatter 获取和管理日期,但如何使其工作。 我认为这是我的第一个“dateFormatter”的问题,它没有正确的格式,因为当我使用带有函数“stringFromDate”和参数“[NSDate date]”的第二个格式化程序时,它工作得很好,但当我使用我的 NSDate 时则不然日期从字符串”。

//"dateString" have this format: "2011-05-26T16:18:26Z"
NSString *dateString = [[tracks objectAtIndex:0] objectForKey:@"to-date"];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"EEE, MMM d, yyyy"];
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [dateFormatter dateFromString:dateString];
[dateFormatter release];

dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"EEE, MMM d, yyyy"];

//NSString *toDate = [dateFormatter stringFromDate:dateFromString];
NSString *toDate = [dateFormatter stringFromDate:[NSDate date]];

感谢您的帮助 :)

I try to get and manage date with NSDateFormatter but how to make it works.
I think it's a problem with my first "dateFormatter" which have not the right format, because when I use the second formatter with function "stringFromDate" and paramter "[NSDate date]" it works well, but not when I use my NSDate "dateFromString".

//"dateString" have this format: "2011-05-26T16:18:26Z"
NSString *dateString = [[tracks objectAtIndex:0] objectForKey:@"to-date"];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"EEE, MMM d, yyyy"];
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [dateFormatter dateFromString:dateString];
[dateFormatter release];

dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"EEE, MMM d, yyyy"];

//NSString *toDate = [dateFormatter stringFromDate:dateFromString];
NSString *toDate = [dateFormatter stringFromDate:[NSDate date]];

Thank you for your help :)

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

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

发布评论

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

评论(2

两人的回忆 2024-11-16 18:47:20

当然,DateFormat 与您从服务器接收到的 DateFormat 不同。

试试这个:

dateFormatter = [[NSDateFormatter alloc] init]; 
[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss Z"];
NSDate *date = [dateFormatter dateFromString:[dateString stringByReplacingOccurrencesOfString:@"Z" withString:@" +0000"]];

Well of course, the DateFormat is not the same as the one you are receiving from the server.

Try this:

dateFormatter = [[NSDateFormatter alloc] init]; 
[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss Z"];
NSDate *date = [dateFormatter dateFromString:[dateString stringByReplacingOccurrencesOfString:@"Z" withString:@" +0000"]];
帅哥哥的热头脑 2024-11-16 18:47:20

假设 to-date 是一个字符串,并且还不是 NSDate,请尝试以下操作:

//"dateString" have this format: "2011-05-26T16:18:26Z"
NSString *dateString = [[tracks objectAtIndex:0] objectForKey:@"to-date"];
NSDate *dateFromString = [[NSDate dateFromString:dateString];

如果您想向用户显示以特定格式格式化的日期,则可以使用设置一个 NSDateFormatter 并使用 stringFromDate: 获取要显示给用户的字符串。如果它仍然无法正确显示,请设置日历(我忘记是否始终需要设置它或是否有默认值)。

Assuming that to-date is a string and not already an NSDate try this instead:

//"dateString" have this format: "2011-05-26T16:18:26Z"
NSString *dateString = [[tracks objectAtIndex:0] objectForKey:@"to-date"];
NSDate *dateFromString = [[NSDate dateFromString:dateString];

If you want to display a date to the user formatted in a particular format you can then use set up an NSDateFormatter and use stringFromDate: to get a string to show the user. If it still doesn't display correctly set the calendar (I forget if you always have to set it or if there is a default).

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