如何将字符串转换为日期和字符串中的日期? [英] How can i convert the string to date and date from string?

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

问题描述

我的输入字符串是

NSString *inputString=@"15 February 2012 17:05";
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"dd MMMM YYYY HH:mm"];
NSDate *dateVal = [dateFormat dateFromString:inputString];

NSDateFormatter *dateFormat2 = [[NSDateFormatter alloc] init];
[dateFormat2 setDateFormat:@"MM/dd/YYYY HH:mm"];
NSString *outputString = [dateFormat2 stringFromDate:dateVal];
[dateFormat release];
[dateFormat2 release];

我有以下日期 outputString = 12/25/2011 17:05

I got the following date outputString=12/25/2011 17:05

但这应该是2012年2月15日17:05 ..我在哪里弄错了?

But it should be 02/15/2012 17:05..Where did i do the mistake?

推荐答案

您为日期格式使用了错误的格式,也无需创建NSDateFormatter的另一个实例-

You are using wrong format for date format and also there is no need to create another instance of NSDateFormatter -

    NSString *inputString=@"15 February 2012 17:05"; 
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init]; 
    [dateFormat setDateFormat:@"dd MMMM yyyy HH:mm"]; 
    NSDate *dateVal = [dateFormat dateFromString:inputString];

    [dateFormat setDateFormat:@"MM/dd/yyyy HH:mm"];
    NSString *outputString = [dateFormat stringFromDate:dateVal];

这篇关于如何将字符串转换为日期和字符串中的日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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