Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/113.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 从字符串转换日期_Ios_Objective C_Nsdate_Nsdateformatter - Fatal编程技术网

Ios 从字符串转换日期

Ios 从字符串转换日期,ios,objective-c,nsdate,nsdateformatter,Ios,Objective C,Nsdate,Nsdateformatter,在我的代码中,我从服务器获取一个字符串,值为“06-27-2014”,然后我想转换为其他日期格式,但最终得到的结果非常不同。这是我使用的代码 NSString* dateVal = @"06-27-2014"; NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"mm-dd-yyyy"]; N

在我的代码中,我从服务器获取一个字符串,值为“06-27-2014”,然后我想转换为其他日期格式,但最终得到的结果非常不同。这是我使用的代码

        NSString* dateVal = @"06-27-2014";

        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateFormat:@"mm-dd-yyyy"];
        NSTimeZone *gmtZone = [NSTimeZone systemTimeZone];
        [dateFormatter setTimeZone:gmtZone];

        NSDate* aDate = [dateFormatter dateFromString:**dateVal**];
           I am getting the aDate as **2014-01-26 18:36:00 +0000** here the month is completely changing to first month from 6th. 

        NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
        [dateFormat setDateStyle:NSDateFormatterShortStyle];

        NSDate *theDateSelected =aDate;

        NSString *theDate = [dateFormat stringFromDate:theDateSelected];

我得到的最后日期是2014年1月27日,这是完全不同的。任何人都可以建议正确的日期格式。

您的日期格式是错误的,这个月应该是
MM
<代码>毫米用于分钟

[dateFormatter setDateFormat:@"mm-dd-yyyy"];
应该是

[dateFormatter setDateFormat:@"MM-dd-yyyy"];

您的格式字符串错误

[dateFormatter setDateFormat:@"MM-dd-yyyy"];
    NSString* dateVal = @"06-27-2014";

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"MM-dd-yyyy"];
    NSTimeZone *gmtZone = [NSTimeZone systemTimeZone];
    [dateFormatter setTimeZone:gmtZone];

    NSDate* aDate = [dateFormatter dateFromString:dateVal];// Now here you will get date like "06-27-2014", and mm is for minutes, you should use MM for month.

    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateStyle:NSDateFormatterShortStyle];

    NSDate *theDateSelected =aDate;

    NSString *theDate = [dateFormat stringFromDate:theDateSelected];
m代表分钟, M代表一个月


始终检查引用;-)

这里,您的日期格式化程序值似乎有误

[dateFormatter setDateFormat:@"MM-dd-yyyy"];
    NSString* dateVal = @"06-27-2014";

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"MM-dd-yyyy"];
    NSTimeZone *gmtZone = [NSTimeZone systemTimeZone];
    [dateFormatter setTimeZone:gmtZone];

    NSDate* aDate = [dateFormatter dateFromString:dateVal];// Now here you will get date like "06-27-2014", and mm is for minutes, you should use MM for month.

    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateStyle:NSDateFormatterShortStyle];

    NSDate *theDateSelected =aDate;

    NSString *theDate = [dateFormat stringFromDate:theDateSelected];

首选的格式是什么?你没提过。另外,您为什么要使用
**…**
来命名
NSString
?***NSString*dateVal=@“06-27-2014”***它的格式不正确。。