[英]How to specify the NSDate Format in Iphone app?
I am trying to make a date format in such a manner hours.minutes day/month/year 我正在尝试以小时/分钟/天/月/年的方式制作日期格式
I have uses this code : 我已经使用此代码:
NSDateFormatter *formatter =[[NSDateFormatter alloc] init];
NSDate *dateAndtime = [NSDate date];
[formatter setTimeStyle:NSDateFormatterShortStyle];
[formatter setDateStyle:NSDateFormatterShortStyle];
//[formatter setTimeStyle:NSDateFormatterShortStyle];
NSString* dateforBD=[[NSString alloc]init];
dateforBD =[formatter stringFromDate:dateAndtime];
But this code not give me my format. 但是这段代码没有给我我的格式。
Also i have tried this code: 我也尝试过此代码:
NSLocale *locale = [NSLocale currentLocale];
NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];
NSString *dateFormat = [NSDateFormatter dateFormatFromTemplate:@"E MMM d yyyy"
options:0 locale:locale];
[formatter setDateFormat:dateFormat];
[formatter setLocale:locale];
It's also give me different format. 它也给了我不同的格式。 How can i achieve the required format means 13.10 21/4/2011 我如何实现所需的格式表示13.10 21/4/2011
All the formats return in month before date. 所有格式均在日期前一个月返回。 May i have used wrong way for this ? 我可以为此使用错误的方式吗?
Thanks in advance. 提前致谢。
NSDate *today = [NSDate date];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"MM/dd/yyyy hh:mma"];
NSString *dateString = [dateFormat stringFromDate:today];
NSLog(@"date: %@", dateString);
[dateFormat release];
To explore more visit http://www.stepcase.com/blog/2008/12/02/format-string-for-the-iphone-nsdateformatter/ 要了解更多信息,请访问http://www.stepcase.com/blog/2008/12/02/format-string-for-the-iphone-nsdateformatter/
Hope it helps. 希望能帮助到你。
用于月份使用前的日期-
[dateFormat setDateFormat:@"dd/MM/yyyy hh:mm"];
As specified in Apple's NSDateFormatter reference, the NSDateFormatter uses Unicode Date Format Patterns . 如Apple的NSDateFormatter参考中所指定,NSDateFormatter使用Unicode日期格式模式 。
According to the unicode patterns, you would need the following pattern to achieve " 13.10 21/4/2011 ": 根据unicode模式,您需要以下模式才能实现“ 13.10 21/4/2011 ”:
hh:mm dd/MM/yyyy
So you would use this code: 因此,您将使用以下代码:
NSDate *dateAndtime = [NSDate date];
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:@"hh:mm dd/MM/yyyy"];
NSString *newlyFormattedDateString = [dateFormatter stringFromDate:dateAndtime];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.