我怎样才能从 NSDate 获得时间?

在我的申请中,我需要分别获得时间和分钟:

NSString *currentHour=[string1 substringWithRange: NSMakeRange(0,2)];
int currentHourInNumber=[currentHour intValue];

考虑 string1包含来自数据采集器的 11:59:13 AM

在这里,如果我使用上面的代码,可以得到小时,如果它大于9。否则,我需要改变 NSMakeRange(0,1),以获得1至9小时之间。

有没有什么方法可以得到小时、分钟等?

预先感谢,请提供我的样本代码。

131935 次浏览

使用 NSDateFormatterstring1转换成 NSDate,然后得到所需的 NSDateComponent:

Obj-C :

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"<your date format goes here"];
NSDate *date = [dateFormatter dateFromString:string1];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [calendar components:(NSCalendarUnitHour | NSCalendarUnitMinute) fromDate:date];
NSInteger hour = [components hour];
NSInteger minute = [components minute];

Swift 1 and 2:

let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "Your date Format"
let date = dateFormatter.dateFromString(string1)
let calendar = NSCalendar.currentCalendar()
let comp = calendar.components([.Hour, .Minute], fromDate: date)
let hour = comp.hour
let minute = comp.minute

斯威夫特3:

let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "Your date Format"
let date = dateFormatter.date(from: string1)
let calendar = Calendar.current
let comp = calendar.dateComponents([.hour, .minute], from: date)
let hour = comp.hour
let minute = comp.minute

关于 Dateformat 日期格式的更多信息在官方 Unicode 站点上

如果您只需要它来表示为字符串,那么下面的代码要简单得多

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"HH:mm"];


NSString *startTimeString = [formatter stringFromDate:startTimePicker.date];

在我看来,这就是问题所在,不需要格式化程序:

NSDate *date = [NSDate date];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [calendar components:(NSCalendarUnitHour | NSCalendarUnitMinute) fromDate:date];
NSInteger hour = [components hour];
NSInteger minute = [components minute];

如果你要使用 C 语言库,那么可以这样做:

time_t t;
struct tm * timeinfo;


time (&t);
timeinfo = localtime (&t);


NSLog(@"Hour: %d Minutes: %d", timeinfo->tm_hour, timeinfo->tm_min);

使用斯威夫特:

var t = time_t()
time(&t)
let x = localtime(&t)


println("Hour: \(x.memory.tm_hour) Minutes: \(x.memory.tm_min)")

有关进一步的指导,请参阅: http://www.cplusplus.com/reference/ctime/localtime/

Swift 2.0

    let dateNow = NSDate()
let calendar = NSCalendar.currentCalendar()
let hour = calendar.component(NSCalendarUnit.Hour, fromDate: dateNow)
let minute = calendar.component(NSCalendarUnit.Minute, fromDate: dateNow)
print(String(hour))
print(String(minute))

请注意 print 语句中对 String 的强制转换,您可以很容易地将该值赋给变量,如下所示:

var hoursString = String(hour)
var minutesString = String(minute)

然后您可以像下面这样将值连接起来:

var compoundString = "\(hour):\(minute)"
print(compoundString)

在 iOS8中,苹果引入了一个辅助方法来从 NSDate 对象中检索 hourminutesecondnanosecond

目标 C

NSDate *date = [NSDate currentDate];
NSInteger hour = 0;
NSInteger minute = 0;
NSCalendar *currentCalendar = [NSCalendar currentCalendar];
[currentCalendar getHour:&hour minute:&minute second:NULL nanosecond:NULL fromDate:date];
NSLog(@"the hour is %ld and minute is %ld", (long)hour, (long)minute);

斯威夫特

let date = NSDate()
var hour = 0
var minute = 0
let calendar = NSCalendar.currentCalendar()
if #available(iOS 8.0, *) {
calendar.getHour(&hour, minute: &minute, second: nil, nanosecond: nil, fromDate: date)
print("the hour is \(hour) and minute is \(minute)")
}

Swift 2.0

你可以做以下事情来获得约会的时间:

let dateFromat = NSDateFormatter()
dateFromat.dateFormat = "hh:mm a"
let date = dateFromat.dateFromString(string1) // In your case its string1
print(date) // you will get - 11:59 AM

这是 NSDate 的分机号码

    @interface NSDate (KTelDateUtility)


-(NSUInteger)dateYear;
-(NSUInteger)dateMonth;
-(NSUInteger)dateDay;


-(NSUInteger)dateHours;
-(NSUInteger)dateMins;
-(NSUInteger)dateSeconds;


@end


@implementation NSDate (KTelDateUtility)


-(NSDateComponents*)dateCalenderComponents {
NSDateComponents* dateComponents = [[NSCalendar currentCalendar] components:NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear fromDate:self];
return dateComponents;
}


-(NSDateComponents*)dateTimeComponents {
NSDateComponents* timeComponents = [[NSCalendar currentCalendar] components:NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond fromDate:self];
return timeComponents;
}


-(NSUInteger)dateYear {
return [[self dateCalenderComponents] year];
}


-(NSUInteger)dateMonth {
return [[self dateCalenderComponents] month];
}


-(NSUInteger)dateDay {
return [[self dateCalenderComponents] day];
}


-(NSUInteger)dateHours {
return [[self dateTimeComponents] hour];
}


-(NSUInteger)dateMins {
return [[self dateTimeComponents] minute];
}


-(NSUInteger)dateSeconds {
return [[self dateTimeComponents] second];
}




@end