2011-08-04 24 views
10

Tôi có thể tìm cách nhận được ngày tháng giữa một phạm vi hay không, nhưng dường như tôi không thể tạo ngày tại một thời điểm cụ thể.Tìm xem thời gian hiện tại có nằm trong khoảng từ

Cách đơn giản nhất để xem [NSDate date] có nằm trong khoảng thời gian không?

Tôi muốn hiển thị một lời chào cá nhân như sau:

12 pm - 4: 59: 9999 pm @ "Xin chào, foo"
17:00 - 11: 59: 9999 pm @" Xin chào, foo"
12:00 - 11: 59: 9999 am @ "chào buổi sáng, foo"

Trả lời

49

Có, bạn có thể sử dụng NSDateComponents sẽ trả lại giờ theo định dạng 24 giờ.

NSDateComponents *components = [[NSCalendar currentCalendar] components:NSHourCalendarUnit fromDate:[NSDate date]]; 
NSInteger hour = [components hour]; 

if(hour >= 0 && hour < 12) 
    NSLog(@"Good morning, foo"); 
else if(hour >= 12 && hour < 17) 
    NSLog(@"Good afternoon, foo"); 
else if(hour >= 17) 
    NSLog(@"Good evening, foo"); 

Swift 3

let hour = Calendar.current.component(.hour, from: Date()) 

if hour >= 0 && hour < 12 { 
    print("Good Morning") 
} else if hour >= 12 && hour < 17 { 
    print("Good Afternoon") 
} else if hour >= 17 { 
    print("Good Evening") 
} 
+0

Rất tiếc, tôi đã hết phiếu bầu cho hôm nay. Nice Answer mặc dù. –

+2

Tôi không ra khỏi phiếu bầu. +1. –

+0

Kể từ iOS8, bạn cần sử dụng NSCalendarUnitHour nhưng câu trả lời hay. –

1

Sử dụng một thể hiện NSCalendar để tạo ra một trường hợp NSDateComponents ra khỏi NSDate của bạn, sau đó chỉ cần kiểm tra giờ, phút và giây tài sản của NSDateComponents và trình bày thông báo thích hợp.

-1

Bạn chỉ có thể sử dụng lớp NSDate để lấy thời gian trên iPhone.

NSDate * today = [NSDate date]; 
NSCalendar * cal = [[NSCalendar alloc] initWithCalendarIdentifier: NSGregorianCalendar]; 

NSDateComponents * comps = [cal components:NSHourCalendarUnit fromDate:today]; 

if ([comps hour]>0 && [comps hour] < 12) 
    NSLog(@"Good morning, foo"); 

if ([comps hour] > 12 && [comps hour] < 17) 
    NSLog(@"Good afternoon, foo"); 

if ([comps hour] >17 && [comps hour]<24 ) 
    NSLog(@"Good evening, foo"); 

tham khảo: NSDate Documentation

+1

Vì vậy, những gì sẽ xảy ra giữa 12:00 pm và 12: 59.9999 pm và 5 pm và 5: 59.9999 pm? :) – Joe

+0

lol ............... – Legolas

2

Cập nhật cho Swift 3,0

let hour = Calendar.current.component(.hour, from: Date()) 

if hour >= 0 && hour < 12 { 
    print("Good Morning") 
} else if hour >= 12 && hour < 17 { 
    print("Good Afternoon") 
} else if hour >= 17 { 
    print("Good Evening") 
} 
0

Noon12:00. Buổi chiều là từ 12:01 PM tới khoảng 5:00 PM. Buổi tối là từ 5:01 PM đến 8 PM hoặc quanh hoàng hôn. Đêm là từ lúc hoàng hôn đến bình minh, do đó, từ 8:01 PM cho đến 5:59 AM.

NSDateComponents *components = [[NSCalendar currentCalendar] components:NSCalendarUnitHour fromDate:[NSDate date]]; 
[components setTimeZone:[NSTimeZone localTimeZone]]; 
NSInteger hour = [components hour]; 

if(hour >= 6 && hour < 12) 
    NSLog(@"Good morning!"); 
else if(hour >= 12 && hour < 17) 
    NSLog(@"Good afternoon!"); 
else if(hour >= 17 && hour < 20) 
    NSLog(@"Good evening!"); 
else if((hour >= 20) || (hour >= 0 && hour < 6)) 
    NSLog(@"Good night!"); 
Các vấn đề liên quan