String description of NSDate

前端 未结 4 2017
星月不相逢
星月不相逢 2021-01-31 10:30

I have the following code:

[ [NSDate date] descriptionWithLocale: @\"yyyy-MM-dd\" ]

I want it to return a date in the following format: \"2009-

4条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-31 11:13

    You are using the wrong method. Instead try descriptionWithCalendarFormat:timeZone:locale:

    [[NSDate date] descriptionWithCalendarFormat:@"%Y-%m-%d"
                                        timezone:nil
                                          locale:nil];
    

    Also note that the method is expecting a different format than the one in your question. The full documentation for that can be found here.

    EDIT: Though as Mike noted, you really should be using NSDateFormatter. There are some problems with descriptionWithCalendarFormat:timezone:locale: that Apple mentions in the documentation.

提交回复
热议问题