0

Can someone tells me whats wrong with my time formatter? when i used ipod 6.0 the time formatter works. but when i used iphone 5 6.1 the time formatter returns nil.

Here are my codes regarding to the timeFormatter.

    NSDateFormatter *tempFormatter = [[[NSDateFormatter alloc]init] autorelease];
    [tempFormatter setDateFormat:@"yyyy-MM-dd hh:mm"];
    NSString *dateandtime = @"2013-05-27 19:00";

    //set end date and time
    NSDateFormatter *tempFormatterTo = [[[NSDateFormatter alloc]init] autorelease];
    [tempFormatterTo setDateFormat:@"yyyy-MM-dd hh:mm"];
    NSString *dateandtimeTo = @"2013-05-27 20:00";

    NSDate *startDate = [tempFormatter dateFromString:dateandtime];
    NSDate *endDate = [tempFormatterTo dateFromString:dateandtimeTo];

Thanks in advance. please help me.

4

3 回答 3

0

尝试这个

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd hh:mm"];
NSDate* d = [df dateFromString:@"2013-05-27 19:00"];
NSLog(@"%@", d);
于 2013-05-27T05:37:24.857 回答
0

yyyy-MM-dd HH:mm当您想将任何NSString日期转换为时,格式中的问题将其更改为也使用我的波纹管方法NSDate。这是我的自定义方法,只需将此方法粘贴到您的.m类中即可。

-(NSDate *)convertStringToDate:(NSString *) date dateFormat:(NSString*)dateFormat{
        NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease]; 
        NSDate *nowDate = [[[NSDate alloc] init] autorelease];
        [formatter setDateFormat:dateFormat];// set format here which format in string date
        date = [date stringByReplacingOccurrencesOfString:@"+0000" withString:@""];
        nowDate = [formatter dateFromString:date];
        // NSLog(@"date============================>>>>>>>>>>>>>>> : %@", nowDate);
        return nowDate;
}

并使用上述方法如下...

 NSDate *startDate = [self convertStringToDate:@"2013-05-27 19:00" dateFormate:@"yyyy-MM-dd HH:mm"];
 NSDate *endDate = [self convertStringToDate:@"2013-05-27 20:00" dateFormate:@"yyyy-MM-dd HH:mm"];
于 2013-05-27T05:35:13.053 回答
0

尝试使用这种格式。 HH用于 24 小时格式。

  [tempFormatter setDateFormat:@"yyyy-MM-dd HH:mm"];
于 2013-05-27T05:23:33.013 回答