Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/26.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
Objective c 如何将特定日期转换为am/pm格式?_Objective C_Nsdateformatter - Fatal编程技术网

Objective c 如何将特定日期转换为am/pm格式?

Objective c 如何将特定日期转换为am/pm格式?,objective-c,nsdateformatter,Objective C,Nsdateformatter,我从字符串中得到了一个特定的日期 NSString*str=@“2016-05-04 08:42:00+0000”; 我想把它转换成08:42 AM格式。 这是我的密码 NSString *str = @"2016-05-04 08:42:00 +0000";// put here item.TimeStart NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init] ; [dateFormatter1 setDateFo

我从字符串中得到了一个特定的日期 NSString*str=@“2016-05-04 08:42:00+0000”; 我想把它转换成08:42 AM格式。 这是我的密码

NSString *str = @"2016-05-04 08:42:00 +0000";// put here item.TimeStart

NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init] ;
[dateFormatter1 setDateFormat:@"EEE, dd MMM YYYY HH:mm:ss VVVV"];// here give the format which you get in TimeStart

NSDate *date1 = [dateFormatter1 dateFromString: str];

dateFormatter1 = [[NSDateFormatter alloc] init] ;
[dateFormatter1 setDateFormat:@"hh:mm a"];

NSString *convertedString = [dateFormatter stringFromDate:date1];
NSLog(@"Converted String : %@",convertedString);

但这将在转换后的字符串中返回nil。为什么?有什么想法吗?

请添加以下行以将时区时差设置为0,您将获得实际时间

        //Add this line to set the time zone time difference to 0
        dateFormatter1.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:0];
添加此代码后,您的最终代码将如下所示:

    NSString *str = @"2016-05-04 08:42:00 +0000";

    NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init] ;
    [dateFormatter1 setDateFormat:@"YYYY-dd-MM HH:mm:ss VVVV"];

    NSDate *date1 = [dateFormatter1 dateFromString: str];

    dateFormatter1 = [[NSDateFormatter alloc] init];

    //Add this line to set the time zone time difference to 0
    dateFormatter1.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:0];

    [dateFormatter1 setDateFormat:@"hh:mm a"];

    NSString *convertedString = [dateFormatter1 stringFromDate:date1];
    NSLog(@"Converted String : %@",convertedString);
它会帮助你的

   -(NSDate *)calculateDate:(NSString *)getString
    {

        NSDateFormatter *Format = [[NSDateFormatter alloc] init];
        [Format setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss"];

        NSDate *aDate = [Format dateFromString:getString];

        if( !aDate )
        {
            [Format setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss a"];
            [Format setLocale:[NSLocale localeWithLocaleIdentifier:@"en_US_POSIX"]];
            aDate = [Format dateFromString:getString];
        }

        return aDate;
    }

当我们有一个像2001-02-15T13:25:00这样的字符串,并且我们想要将其格式化为12H格式时,我们只需要定义本地

dateFormatter1的
dateFormat
str的格式不匹配。如果进行调试,您将看到
date1
为零。因此,它指出您的问题出现在第一个日期格式化程序
dateFormatter1
,在您解决该问题之前,代码的其余部分是无关的。耶@Larme date1为零。日期格式化程序[dateFormatter1 setDateFormat:@“EEE,dd-MMM-YYYY-HH:mm:ss-vvv”]正确吗?是的,我找到了,我用“YYYY-dd-mm-HH:mm:ss-vvv”替换了日期格式化程序“但我的日志记录是下午2:21。为什么?不,我说不是。了解dateFormat的含义,不要复制/粘贴您在web上看到的格式。你清楚地看到,你从这一年开始。所以,至少,格式应该以“yyyy”开头。然后是月份(在“-”之间)。所以你的格式是“yyyy-MM”,等等。只需在文本编辑器的一行上写下
2016-05-04 08:42:00+0000
,然后写下格式,每个部分的顺序都必须相同。记录
date1
,我猜您所在的时区是+6:30。第二种格式中缺少要渲染的时区。@Microprocessor8085谢谢。。。嘿,你有在watchconnectivity工作的想法吗??对不起,我不能再问这个问题了。。所以拒绝了我的要求facility@ChelseaShawra嘿对不起,我不知道手表套件。@ChelseaShawra是的,为什么不呢。