How to convert NSDate to NSString?

前端 未结 1 1752
深忆病人
深忆病人 2021-02-09 04:30

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=[[         


        
相关标签:
1条回答
  • 2021-02-09 05:21

    You can achieve this with the NSDateFormatter.

    Example:

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateStyle:NSDateFormatterMediumStyle];
    
    NSString *string = [dateFormatter stringFromDate:date];
    [dateFormatter release];
    

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

    0 讨论(0)
提交回复
热议问题