-1

我正在使用这种格式来获取时间

   NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    dateFormatter.dateFormat = @"hh";
    [dateFormatter setTimeZone:[NSTimeZone systemTimeZone]];

    NSString *date = [dateFormatter stringFromDate:now];

    NSInteger myHour = [date intValue];

问题是它返回例如4Pm我希望它返回16而不是 4。我该怎么做?我尝试用单个 h 替换 hh 无济于事。

4

3 回答 3

2

Here Your input:

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"hh"];
NSDate *Yourcurrenttime = [dateFormatter dateFromString:Yourstring];

And date converted to the String ..

[dateFormatter setDateFormat:@"HH"];
NSString *date = [dateFormatter stringFromDate:now];

NSInteger myHour = [date intValue];
于 2012-10-06T11:59:57.580 回答
1

Here is answer :-

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    dateFormatter.dateFormat = @"HH";
    [dateFormatter setTimeZone:[NSTimeZone systemTimeZone]];

    NSString *date = [dateFormatter stringFromDate:[NSDate date]];

    NSInteger myHour = [date intValue];
    NSLog(@"%d",myHour);

Hope it helps you!

于 2012-10-06T11:59:31.573 回答
1

它的 12 小时时间格式:-

NSDateFormatter *timeFormat = [[NSDateFormatter alloc] init];
[timeFormat setDateFormat:@"hh a"];
NSString *theTime = [timeFormat stringFromDate:[NSDate date]];    
NSLog(@"time, %@",theTime);  

它的 24 小时时间格式:-

NSDateFormatter *timeFormat = [[NSDateFormatter alloc] init];
[timeFormat setDateFormat:@"HH"];
NSString *theTime = [timeFormat stringFromDate:[NSDate date]];    
NSLog(@"time, %@",theTime);
于 2012-10-06T13:00:55.200 回答