NSDate的字符串描述 [英] String description of NSDate
问题描述
我有以下代码:
[ [NSDate date] descriptionWithLocale: @"yyyy-MM-dd" ]
我希望它返回以下格式的日期:2009-04-23
I want it to return a date in the following format: "2009-04-23"
但它返回:2009年4月23日星期四晚上11:27:03 GMT + 03:00
But it returns: Thursday, April 23, 2009 11:27:03 PM GMT+03:00
我做错了?
提前谢谢。
推荐答案
使用错误的方法。而应尝试 descriptionWithCalendarFormat:timeZone:locale:
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.
编辑:虽然正如迈克所说,你真的应该使用 NSDateFormatter
。有些问题与Apple在文档中提到的 descriptionWithCalendarFormat:timezone:locale:
有关。
Though as Mike noted, you really should be using NSDateFormatter
. There are some problems with descriptionWithCalendarFormat:timezone:locale:
that Apple mentions in the documentation.
这篇关于NSDate的字符串描述的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!