Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/39.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/117.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
Iphone 将NSString转换为NSDate_Iphone_Ios_Nsdateformatter_Datetime Format - Fatal编程技术网

Iphone 将NSString转换为NSDate

Iphone 将NSString转换为NSDate,iphone,ios,nsdateformatter,datetime-format,Iphone,Ios,Nsdateformatter,Datetime Format,我正在将两个日期和时间字符串转换为NSDate,但我总是得到错误的时间,但得到正确的日期,我不知道为什么xcode给出错误的日志消息,我似乎有很多NSDate代码,但都不起作用,当我更改日期格式时,这一个起作用,告诉我如何处理时间格式 NSDate * date; //Assume dateString is populated and of format NSString * dateString =@"2011-11-21 11:20"; NSDateFormatt

我正在将两个日期和时间字符串转换为
NSDate
,但我总是得到错误的时间,但得到正确的日期,我不知道为什么xcode给出错误的日志消息,我似乎有很多
NSDate
代码,但都不起作用,当我更改日期格式时,这一个起作用,告诉我如何处理时间格式

     NSDate * date;
    //Assume dateString is populated and of format NSString * dateString =@"2011-11-21 11:20";
    NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init];
    NSString *dateString =[NSString stringWithFormat:@"%@ %@", socialEvent.SocialEventsDC_EventDate,socialEvent.SocialEventsDC_EventStartTime];
    NSLog(@"%@ %@",socialEvent.SocialEventsDC_EventDate,socialEvent.SocialEventsDC_EventStartTime);

    [dateFormatter setDateFormat:@"MM/dd/yyyy HH:mm a"];
    if (dateString != nil)  {
        date = [dateFormatter dateFromString:dateString];
//        [date release];
    }

    NSLog(@"%@",date);
我的日志消息是

2013-04-25 10:45:04.417 BNI UK & Ire[450:5203] 01/20/2015 11:00 PM
2013-04-25 10:45:09.679 BNI UK & Ire[450:5203] 2015-01-20 07:00:00 +0000
虽然我的时间是11:00,但我的时间始终是7:00:00,请帮助我尝试此代码

    NSString * dateString =@"2011-11-21 11:20";
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd hh:mm"];
    // [formatter setTimeZone:[NSTimeZone localTimeZone]];

    NSDate *datTemp = [formatter dateFromString:dateString];
    NSTimeZone* sourceTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];
    NSTimeZone* destinationTimeZone = [NSTimeZone systemTimeZone];

    NSInteger sourceGMTOffset = [sourceTimeZone secondsFromGMTForDate:datTemp];
    NSInteger destinationGMTOffset = [destinationTimeZone secondsFromGMTForDate:datTemp];
    NSTimeInterval interval = destinationGMTOffset - sourceGMTOffset;
    NSDate* destinationDate = [[[NSDate alloc] initWithTimeInterval:interval sinceDate:datTemp] autorelease];
    NSLog(@"\n\n\n Destination date is:- %@",destinationDate);
    NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init];

    [dateFormatter1 setDateFormat:@"MM/dd/yyyy hh:mm a"];
    [dateFormatter1 setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]];
    NSDate *myDate = [dateFormatter1 dateFromString:@"01/20/2015 11:00 pm"];
    NSLog(@"mydate is :%@",myDate);
    NSLog(@"DATE IS : %@",[dateFormatter1 stringFromDate:myDate]);
请尝试此代码

    NSString * dateString =@"2011-11-21 11:20";
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd hh:mm"];
    // [formatter setTimeZone:[NSTimeZone localTimeZone]];

    NSDate *datTemp = [formatter dateFromString:dateString];
    NSTimeZone* sourceTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];
    NSTimeZone* destinationTimeZone = [NSTimeZone systemTimeZone];

    NSInteger sourceGMTOffset = [sourceTimeZone secondsFromGMTForDate:datTemp];
    NSInteger destinationGMTOffset = [destinationTimeZone secondsFromGMTForDate:datTemp];
    NSTimeInterval interval = destinationGMTOffset - sourceGMTOffset;
    NSDate* destinationDate = [[[NSDate alloc] initWithTimeInterval:interval sinceDate:datTemp] autorelease];
    NSLog(@"\n\n\n Destination date is:- %@",destinationDate);
    NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init];

    [dateFormatter1 setDateFormat:@"MM/dd/yyyy hh:mm a"];
    [dateFormatter1 setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]];
    NSDate *myDate = [dateFormatter1 dateFromString:@"01/20/2015 11:00 pm"];
    NSLog(@"mydate is :%@",myDate);
    NSLog(@"DATE IS : %@",[dateFormatter1 stringFromDate:myDate]);
试着这样,

NSDate *mydate = [NSDate date];
NSTimeInterval secondsInEightHours = 4 * 60 * 60;
NSDate *dateEightHoursAhead = [mydate dateByAddingTimeInterval:secondsInEightHours];
试着这样,

NSDate *mydate = [NSDate date];
NSTimeInterval secondsInEightHours = 4 * 60 * 60;
NSDate *dateEightHoursAhead = [mydate dateByAddingTimeInterval:secondsInEightHours];

如果dateString如下所示,则可以使用,时区假定为您的本地时区。日期日志将以UTC为单位

NSString *dateString = @"2011-11-21 11:20";

NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm"];

NSDate * date = [dateFormatter dateFromString:dateString];
NSLog(@"%@",date);

如果dateString如下所示,则可以使用,时区假定为您的本地时区。日期日志将以UTC为单位

NSString *dateString = @"2011-11-21 11:20";

NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm"];

NSDate * date = [dateFormatter dateFromString:dateString];
NSLog(@"%@",date);

请尝试此代码

    NSString * dateString =@"2011-11-21 11:20";
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd hh:mm"];
    // [formatter setTimeZone:[NSTimeZone localTimeZone]];

    NSDate *datTemp = [formatter dateFromString:dateString];
    NSTimeZone* sourceTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];
    NSTimeZone* destinationTimeZone = [NSTimeZone systemTimeZone];

    NSInteger sourceGMTOffset = [sourceTimeZone secondsFromGMTForDate:datTemp];
    NSInteger destinationGMTOffset = [destinationTimeZone secondsFromGMTForDate:datTemp];
    NSTimeInterval interval = destinationGMTOffset - sourceGMTOffset;
    NSDate* destinationDate = [[[NSDate alloc] initWithTimeInterval:interval sinceDate:datTemp] autorelease];
    NSLog(@"\n\n\n Destination date is:- %@",destinationDate);
    NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init];

    [dateFormatter1 setDateFormat:@"MM/dd/yyyy hh:mm a"];
    [dateFormatter1 setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]];
    NSDate *myDate = [dateFormatter1 dateFromString:@"01/20/2015 11:00 pm"];
    NSLog(@"mydate is :%@",myDate);
    NSLog(@"DATE IS : %@",[dateFormatter1 stringFromDate:myDate]);

注意:时区取决于您的本地时区。

请尝试此代码

    NSString * dateString =@"2011-11-21 11:20";
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd hh:mm"];
    // [formatter setTimeZone:[NSTimeZone localTimeZone]];

    NSDate *datTemp = [formatter dateFromString:dateString];
    NSTimeZone* sourceTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];
    NSTimeZone* destinationTimeZone = [NSTimeZone systemTimeZone];

    NSInteger sourceGMTOffset = [sourceTimeZone secondsFromGMTForDate:datTemp];
    NSInteger destinationGMTOffset = [destinationTimeZone secondsFromGMTForDate:datTemp];
    NSTimeInterval interval = destinationGMTOffset - sourceGMTOffset;
    NSDate* destinationDate = [[[NSDate alloc] initWithTimeInterval:interval sinceDate:datTemp] autorelease];
    NSLog(@"\n\n\n Destination date is:- %@",destinationDate);
    NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init];

    [dateFormatter1 setDateFormat:@"MM/dd/yyyy hh:mm a"];
    [dateFormatter1 setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]];
    NSDate *myDate = [dateFormatter1 dateFromString:@"01/20/2015 11:00 pm"];
    NSLog(@"mydate is :%@",myDate);
    NSLog(@"DATE IS : %@",[dateFormatter1 stringFromDate:myDate]);


注意:时区取决于您当地的时区。

您的居住时间比格林尼治标准时间晚4小时,我想在西印度群岛、瓜亚岛等附近。如果是这样,您的时间是正确的,并且以这种方式显示。您需要将其转换为字符串。您正在使用模拟器吗?日志总是说不是格林尼治时间。别担心,这是一个日志问题,不是在你的应用程序中。我使用了模拟器和设备,但结果是一样的,但当我的时间不同时,我也会得到相同的时间。你比格林尼治标准时间晚4个小时,我猜在西印度群岛、瓜亚岛等附近。如果是这样的话,你的时间是正确的,就是这样显示的。您需要将其转换为字符串。您正在使用模拟器吗?日志总是说不是格林尼治时间。别担心,这是个日志问题,不在你的应用程序中。我使用了模拟器和设备,但结果是相同的,但当我的时间不同时,我也会得到相同的时间有些地方出错,否则它会给出正确的结果。有些地方出错,否则它会给出正确的结果。它给我此日志消息目标日期是:2013-04-20 00:40:00+0000 2013-04-2911:50:22.617 BNI UK&Ire[843:1303]目的地日期是:2015-01-20 12:00:00+000我的时间是8:40,目的地给我的时间是00:40:00这里你的时间是8:40,你得到的是40,那么它不正确,但是它的bcoz随时区不同而不同。。像EDT。查看我的博客,其中包含日期信息,以及NSDate的官方文档。。。。希望它能对您有所帮助……它给我的这个日志消息目的地日期是:2013-04-20 00:40:00+0000 2013-04-29 11:50:22.617 BNI UK&Ire[843:1303]目的地日期是:2015-01-20 12:00:00+000我的时间是8:40,目的地给我的时间是00:40:00这里你的时间是8:40,你得到40,那么它不正确,但是它的bcoz随时区不同而不同。。像EDT。查看我的博客,其中包含日期信息,以及NSDate的官方文档。。。。希望它能对你有所帮助。。。。