将日期字符串转换为X月X日形式
2013-04-02 16:43
204 查看
得到的日期字符串为:
2013-04-02 16:47:15
需要将其转换为 4月2日 的形式,实现如下:
2013-04-02 16:47:15
需要将其转换为 4月2日 的形式,实现如下:
// 根据时间来返回显示的时间字符串 - (NSString *)transformTime:(NSString *)timeStr { if ([timeStr length]<11) { return timeStr; } NSDateFormatter *formater = [[ NSDateFormatter alloc] init]; NSTimeZone *localzone = [NSTimeZone localTimeZone]; [formater setTimeZone:localzone]; [formater setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; NSDate *modefiTime = [formater dateFromString:timeStr]; NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; //设置日期里要获取的单元类型 NSInteger flag = NSYearCalendarUnit |NSMonthCalendarUnit |NSDayCalendarUnit |NSHourCalendarUnit |NSMinuteCalendarUnit |NSSecondCalendarUnit; NSDateComponents* dc = [[[NSDateComponents alloc] init] autorelease]; dc = [gregorian components:flag fromDate:modefiTime]; // NSInteger nowHour = [dc hour]; // NSInteger nowMin = [dc minute]; // NSInteger nowYear = [dc year]; NSInteger nowMonth = [dc month]; NSInteger nowDay = [dc day]; NSString *time = [NSString stringWithFormat:@"%d月%d日", nowMonth, nowDay]; NSLog(@"time = %@", time); return time; }
相关文章推荐
- date将秒数转换成日期的字符串表达形式
- 将Fri May 04 17:25:34 CST 2012形式的日期字符串转换成java.util.Date对象的方法
- js 字符串与数值相互转换,以及时间戳转换成日期形式字符串方法
- C# 日期和时间的字符串表示形式转换为其等效的DateTime(stringToDateTime)
- 将字符串形式的日期转换成日期对象
- 以字符串形式输出的日期转换为固定的日期格式输出
- 将Fri May 04 17:25:34 CST 2012形式的日期字符串转换成java.util.Date对象的方法
- javascript时间戳和日期字符串相互转换
- js把日期字符串转换成时间戳
- sql server日期时间格式转换字符串简介
- 日期格式图片在转换整个输入字符串之前结束
- DB2日期转换为字符串
- 日期类型与字符串类型相互转换工具类
- 日期字符串转换
- 【转】MS Sql server 日期转换为特定字符串格式(续)
- 把字符串转换成时间格式或日期格式
- mysql日期字符串相互转换
- java 字符串和日期类型的相互转换问题
- MFC中 日期字符串的转换
- sql server日期时间格式转换字符串简介