如何将日期字符串转换为格式“2010年11月17日” [英] How to convert date string into format "17 Nov 2010"

查看:139
本文介绍了如何将日期字符串转换为格式“2010年11月17日”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的日期格式为 2010-11-17 。格式为 NSString

I have the date that is in format 2010-11-17.This is of the form NSString.

我想将此 NSString日期转换为格式 2010年11月17日并显示在标签中

I want to convert this NSString date into format 17 Nov 2010 and display in a label

date不是当前日期,但可能是较旧的日期。

This date is just not the current date but may even be the older dates.

所以我不能使用[NSDate date]实例来获取日期。

So I cant use the [NSDate date] instance to get the date.

我尝试使用 NSDateFormatter 和方法 dateFromString

但是它给我一个 null 值。

这是代码片段

     NSString *dates  = @"2010-11-16";

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];

[dateFormatter setDateFormat:@"dd MMM yyyy"];

NSDate *dates1 = [dateFormatter dateFromString:dates];

NSString *dates2 = [dateFormatter stringForObjectValue:dates1];

NSLog(@"dates : %@",dates);

NSLog(@"Dates 2 :  %@",dates2);

[dateLabel setText:dates2];

NSLog(@"Formatted Date is : %@",dateLabel.text);

该怎么办?

请帮助和建议。

谢谢。

推荐答案

试试这个

NSDateFormatter *formater = [[NSDateFormatter alloc] init];
[formater setDateFormat:@"yyyy-MM-dd"];
NSDate *date2 = [formater dateFromString:@"2010-11-17"];
[formater setDateFormat:@"d MMM yyyy"];
NSString *result = [formater stringFromDate:date2];
NSLog(@"RESULT %@",result);

我得到了结果。如果这种方法错误,请更正我。

I got the result.Correct me if this approach is wrong.

这篇关于如何将日期字符串转换为格式“2010年11月17日”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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