0

你好我想问一件事我有 NSString 作为时间,我想在 NSDate 时间转换这个字符串我已经这样做了,但我不知道我得到了什么............

     NSString *Time=@"17:00:00";

     NSDateFormatter *dateFormatter3 = [[NSDateFormatter alloc] init];
    [dateFormatter3 setDateStyle:NSDateFormatterMediumStyle];
    [dateFormatter3 setDateFormat:@"HH:mm:ss"];
    NSDate *date1 = [dateFormatter3 dateFromString:Time];
    NSLog(@"date1 : %@", date1);

输出date1:​​2000-01-01 11:30:00 +0000
我只想要正确的时间 17:00:00 作为 NSDate 格式...如何获得这个?....
请解决这个问题....!!!

4

6 回答 6

5

设置时区你会得到正确的输出试试这个代码,

NSString *Time=@"17:00:00";

NSDateFormatter *dateFormatter3 = [[NSDateFormatter alloc] init];
[dateFormatter3 setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"GMT"]];
[dateFormatter3 setDateStyle:NSDateFormatterMediumStyle];
[dateFormatter3 setDateFormat:@"HH:mm:ss"];
NSDate *date1 = [dateFormatter3 dateFromString:Time];
NSLog(@"date1 : %@", date1);
于 2013-10-29T05:22:44.560 回答
0

尝试这个:

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setLocale:[[NSLocale alloc] 
                 initWithLocaleIdentifier:@"en_US"]];
[formatter setDateFormat:@"HH:mm:ss"];
NSString *etaStr = @"17:00:00";
    NSDate *generatedDate = [formatter dateFromString:etaStr];
NSLog(@"%@", [formatter stringFromDate:generatedDate]);
于 2013-10-29T05:24:35.197 回答
0

这对我有用:


    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    formatter.dateFormat = @"HH:mm:ss";
    NSLog(@"%@", [formatter stringFromDate:[NSDate date]]); // 07:23:38

    NSDate *date = [formatter dateFromString:@"17:00:00"]; // 2000-01-01 15:00:00 +0000
    NSLog(@"%@", date);
于 2013-10-29T05:24:40.593 回答
0

尝试这个,

NSString *Time=@"17:00:00";
NSDateFormatter *dateFormatter3 = [[NSDateFormatter alloc] init];
[dateFormatter3 setDateFormat:@"HH:mm:ss"];
NSDate *date1 = [dateFormatter3 dateFromString:Time];
NSLog(@"date1 : %@", date1);

或者使用这些功能,

#define DATE_COMPONENTS (NSYearCalendarUnit| NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekCalendarUnit |  NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit | NSWeekdayCalendarUnit | NSWeekdayOrdinalCalendarUnit)

 +(NSDateComponents *)componentsForDate:(NSDate *)date 
{
    if (date!=nil)
    {
    NSCalendar *gregorian = [[NSCalendar alloc]
                             initWithCalendarIdentifier:NSGregorianCalendar];

    NSDateComponents *dayComponents =
    [gregorian components:DATE_COMPONENTS fromDate:date];

    return dayComponents;
    }

    return nil;
}

+(NSString *)timeFromNSDate:(NSDate *)date
{
    int hour=[self componentsForDate:date].hour;
    int minute=[self componentsForDate:date].minute;
    int second=[self componentsForDate:date].second;

    NSString *returnDateString= [NSString stringWithFormat:@"%d:%d:%d",hour, minute,second];
    return returnDateString;
}
于 2013-10-29T05:25:19.220 回答
0

您在日志中得到的可能是正确的日期。NSLog – 确切地说:-description (NSDate) – 以 UTC 打印日期。

NSDate 的意外值

顺便说一句:您应该将日期样式设置为无。

于 2013-10-29T05:26:41.653 回答
0

这是因为NSDate是日期和时间的组合。当您打印任何 NSDate 时,它​​将以这种格式打印。但是这个 NSDate 中的 NSString 显示了你真正想要的。因此,只需从您的日期中获取NSString

NSString *dateString = [dateFormatter3 stringFromDate:date1];
 NSLog(@"date : %@ , date string : %@", date1, [dateFormatter3 stringFromDate:date1]);
于 2013-10-29T05:32:04.580 回答