如何将NSDate转换为NSString? [英] How to convert NSDate to NSString?

查看:87
本文介绍了如何将NSDate转换为NSString?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将NSDate转换为NSString?
怎么可能?

I want to convert NSDate to NSString? How's that possible?

我试过这个,但它没有用(它产生异常):

I tried this, but it didn't work (it is generating Exception) :

   NSString *dateToString=[[NSString alloc] initWithFormat:(NSString *)dateObj];

提前致谢。

推荐答案

您可以使用 NSDateFormatter 实现此目的。

You can achieve this with the NSDateFormatter.

示例:

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];

NSString *string = [dateFormatter stringFromDate:date];
[dateFormatter release];

有关控制字符串格式的更多方法,请参阅 NSDateFormatter

For more ways of controlling the format of the string, see the reference documentation for NSDateFormatter.

这篇关于如何将NSDate转换为NSString?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆