- NSString -> NSDate 處理國內(nèi)時間格式
void string2date(){
//國內(nèi)化時間
NSString *string = @"2017-11-14 16:14:00";
//1.將NSDate轉(zhuǎn)為NSString
//2.將NSString轉(zhuǎn)為NSDate
NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
//設(shè)置日期格式
fmt.dateFormat = @"yyyy-MM-dd HH:mm:ss";
NSDate *date = [fmt dateFromString:string];
NSLog(@"%@--", date);
}
- NSString -> NSDate 處理國際時間格式
void string2date2(){
//國際化時間
NSString *string = @"Tue Nov 24 16:26:26 2017";
//1.將NSDate轉(zhuǎn)為NSString
//2.將NSString轉(zhuǎn)為NSDate
NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
//設(shè)置日期格式
fmt.dateFormat = @"EEE MMM dd HH:mm:ss yyyy";
//設(shè)置語言區(qū)域
fmt.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
NSDate *date = [fmt dateFromString:string];
NSLog(@"%@", date);
}
- NSString -> NSDate 處理時間戳格式
void string2date3(){
//時間字符串 - 時間戳
NSString *string = @"1432182932982";
//毫秒轉(zhuǎn)換為秒
NSTimeInterval second = string.longLongValue / 1000.0;
NSDate *date = [NSDate dateWithTimeIntervalSince1970:second];
NSLog(@"%@",date);
}
void date2string(){
NSDate *date = [NSDate date];
NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
fmt.dateFormat = @"yyyy-MM-dd HH:mm:ss";
NSString *string = [fmt stringFromDate:date];
NSLog(@"%@", string);
}
void getComponentsOfDate(){
NSString *string = @"2017-11-14 16:14:00";
NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
//設(shè)置日期格式
fmt.dateFormat = @"yyyy-MM-dd HH:mm:ss";
//字符串轉(zhuǎn)為date對象
NSDate *date = [fmt dateFromString:string];
//利用NSCalendar處理日期
NSCalendar *calendar = [NSCalendar currentCalendar];
NSInteger month = [calendar component:NSCalendarUnitMonth fromDate:date];
NSLog(@"%zd", month);
}
#define iOS(version) [UIDevice currentDevice].systemVersion.doubleValue >= (version)
void getComponentsOfDate2(){
NSString *string = @"2017-11-14 16:14:00";
NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
//設(shè)置日期格式
fmt.dateFormat = @"yyyy-MM-dd HH:mm:ss";
//字符串轉(zhuǎn)為date對象
NSDate *date = [fmt dateFromString:string];
//NSCalendar系統(tǒng)適配
NSCalendar *calendar = nil;
// if (iOS(8.0)) {
// calendar = [NSCalendar calendarWithIdentifier:NSCalendarIdentifierGregorian];
// }else{
// calendar = [NSCalendar currentCalendar];
// }
//利用NSCalendar生成日期
//[calendar dateWithEra:<#(NSInteger)#> year:<#(NSInteger)#> month:<#(NSInteger)#> day:<#(NSInteger)#> hour:<#(NSInteger)#> minute:<#(NSInteger)#> second:<#(NSInteger)#> nanosecond:<#(NSInteger)#>]
NSCalendarUnit unit = NSCalendarUnitMonth | NSCalendarUnitDay;
NSDateComponents *cmps = [calendar components:unit fromDate:date];
NSLog(@"%@", cmps);
}
void compareDate(){
NSString *string = @"2017-11-14 16:14:00";
NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
//設(shè)置日期格式
fmt.dateFormat = @"yyyy-MM-dd HH:mm:ss";
//字符串轉(zhuǎn)為date對象
NSDate *date = [fmt dateFromString:string];
NSDate *nowDate = [NSDate date];
NSComparisonResult result = [date compare:nowDate];
if (result == NSOrderedAscending) {
//升序
}else if(result == NSOrderedDescending){
//降序
}else{
//same
}
//比較字符串
NSString *str1 = @"abc";
NSString *str2 = @"aBc";
[str1 compare:str2];
}
void compareDate2(){
NSString *string = @"2017-11-14 16:14:00";
NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
//設(shè)置日期格式
fmt.dateFormat = @"yyyy-MM-dd HH:mm:ss";
//字符串轉(zhuǎn)為date對象
NSDate *date = [fmt dateFromString:string];
NSDate *nowDate = [NSDate date];
//獲取時間間隔的秒
NSTimeInterval interval = [date timeIntervalSinceDate:nowDate];
NSLog(@"%f", interval);
}
- 通過 NSDateComponents 獲取日期比較的結(jié)果
void compareDate3(){
NSString *string = @"2017-11-14 16:14:00";
NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
//設(shè)置日期格式
fmt.dateFormat = @"yyyy-MM-dd HH:mm:ss";
//字符串轉(zhuǎn)為date對象
NSDate *date = [fmt dateFromString:string];
NSDate *nowDate = [NSDate date];
NSCalendar *calendar = nil;
//系統(tǒng)適配
if ([NSCalendar respondsToSelector:@selector(calendarWithIdentifier:)]) {
calendar = [NSCalendar calendarWithIdentifier:NSCalendarIdentifierGregorian];
}else{
calendar = [NSCalendar currentCalendar];
}
NSCalendarUnit unit = NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour;
NSDateComponents *cmps = [calendar components:unit fromDate:nowDate toDate:date options:0];
NSLog(@"%@",cmps);
}
void other(){
NSString *string = @"2017-11-14 16:14:00";
NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
//設(shè)置日期格式
fmt.dateFormat = @"yyyy-MM-dd HH:mm:ss";
//字符串轉(zhuǎn)為date對象
NSDate *date = [fmt dateFromString:string];
NSCalendar *calendar = nil;
//系統(tǒng)適配
if ([NSCalendar respondsToSelector:@selector(calendarWithIdentifier:)]) {
calendar = [NSCalendar calendarWithIdentifier:NSCalendarIdentifierGregorian];
}else{
calendar = [NSCalendar currentCalendar];
}
//其他的比較方法
[calendar isDateInToday:date];
}