将 NSDate 转换为 NSString

发布于 2024-09-12 05:11:00 字数 301 浏览 0 评论 0原文

我有以下内容:

NSDateFormatter* df = [[NSDateFormatter alloc]init];
[df setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZ"];
NSDate* date = [df dateFromString:[sDate stringByReplacingOccurrencesOfString:@"Z" withString:@"-0000"]];
[df release];

我希望输出字符串为“9/20/10”

我该怎么做?

I have the following:

NSDateFormatter* df = [[NSDateFormatter alloc]init];
[df setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZ"];
NSDate* date = [df dateFromString:[sDate stringByReplacingOccurrencesOfString:@"Z" withString:@"-0000"]];
[df release];

I would like the output string to be "9/20/10"

How can I do this?

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

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

发布评论

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

评论(3

旧人 2024-09-19 05:11:00

你可以这样做:

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setFormatterBehavior:NSDateFormatterBehavior10_4];
[formatter setDateStyle:NSDateFormatterShortStyle];
[formatter setTimeStyle:NSDateFormatterNoStyle];
NSString *result = [formatter stringFromDate:[NSDate date]];

这将在用户设置中显示它(但以类似的方式),所以对我来说结果将是 7/28/10

You can do it like this:

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setFormatterBehavior:NSDateFormatterBehavior10_4];
[formatter setDateStyle:NSDateFormatterShortStyle];
[formatter setTimeStyle:NSDateFormatterNoStyle];
NSString *result = [formatter stringFromDate:[NSDate date]];

This will display it in the users settings (but in a similar way), so for me results will be 7/28/10

孤独岁月 2024-09-19 05:11:00

您也可以这样做:

NSDateFormatter* df = [[NSDateFormatter alloc]init];
[df setDateFormat:@"MM/dd/yyyy"];
NSString *result = [df stringFromDate:date];
[df release];

You can also do this way :

NSDateFormatter* df = [[NSDateFormatter alloc]init];
[df setDateFormat:@"MM/dd/yyyy"];
NSString *result = [df stringFromDate:date];
[df release];
灼痛 2024-09-19 05:11:00

你也可以尝试

NSString *dateString = [NSString stringWithFormat:[NSDate date]];

You can also try

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