Object-C 格式化日期

Object-C关于日期的操作

#import <UIKit/UIKit.h>
#import "AppDelegate.h"

int main(int argc, char * argv[]) {
//    NSString * appDelegateClassName;
    @autoreleasepool {
//       获取当前时间
        NSDate *currentDate = [NSDate date];
        NSLog(@"currentDate = %@",currentDate);
        // 获取距离当前时间1个小时之后的时间
        NSDate *secondDate = [NSDate dateWithTimeInterval:3600 sinceDate:currentDate];
        NSLog(@"secondDate = %@",secondDate);
        // 获取距离当前时间1个小时之前的时间
        NSDate *thirDate = [NSDate dateWithTimeIntervalSinceNow:-3600];
        NSLog(@"thirDate = %@",thirDate);
        // 获得从格林威治时间1970年1月1日00时00分00秒,到现在为止的总秒数
        NSLog(@"timeIntervalSince1970 = %f",[currentDate timeIntervalSince1970]);
        // 获取1个小时之前的时间距离当前时间的秒数
        NSLog(@"timeIntervalSnceDate = %f",[currentDate timeIntervalSinceDate:thirDate]);
        // 获取1个小时之后的时间距离当前时间的秒数
        NSLog(@"timeIntervalSinceNow = %f",[secondDate timeIntervalSinceNow]);
        
        // 判断两个日期对象是否相同
        NSLog(@"isEqualToDate = %d",[currentDate isEqualToDate:secondDate]);
        NSLog(@"isEqualToDate = %d",[currentDate isEqualToDate:thirDate]);
        
        // 将两个日期进行比较之后,存储在一个枚举类型的变量。由于当前的日期,遭遇第二个日期,此处的枚举值为NSOrderdAscending,转换为数字之后值为-1
        // secondDate --> 当前时间1个小时之后的时间
        // thirDate --> 当前时间1个小时之前的时间
        NSComparisonResult comparison1 = [currentDate compare:secondDate];
        NSLog(@"result = %ld",(long)comparison1);
        // 相等值为0
        NSComparisonResult comparison2 = [currentDate compare:currentDate];
        NSLog(@"result = %ld",(long)comparison2);
        // 当前时间大于一小时之前的时间,值为1
        NSComparisonResult comparison3 = [currentDate compare:thirDate];
        NSLog(@"result = %ld",(long)comparison3);
        
        // 初始化一个日期格式化对象,它可以对日期的显示样式进行格式化,还可以讲一个日期格式的字符串,转换为日期对象
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        // 依次设置日期格式化对象的日期属性为包含星期的完整样式,时间格式为包含上午或下午字样的形式。
        [formatter setDateStyle:NSDateFormatterFullStyle];
        [formatter setTimeStyle:NSDateFormatterMediumStyle];
        // 设置日期格式化对象的本地化属性,其值为简体中文,这样将以简体中文的方式,显示日期和时间的数值
        [formatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"zh-CN"]];
        // 使用日期格式化对象,对日期进行格式化,并将结果存储在字符串中,然后在控制台输出这个字符串结果
        NSString *currentDateString = [formatter stringFromDate:currentDate];
        NSLog(@"%@",currentDateString);//2020年12月14日 星期一 下午9:47:01
        
        // 初始化另一个日期格式对象,并以字符串的方式,设置日期和时间的格式
        NSDateFormatter *formatter2 = [[NSDateFormatter alloc] init];
        [formatter2 setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
        // 初始化一个日期对象,并以新的日期格式化对象对日期进行格式化对象,对日期对象进行格式化,同时在控制台输出
        NSDate *date = [NSDate date];
        NSLog(@"Date string: %@",[formatter2 stringFromDate:date]);
        
        // 日期格式化对象和可以将一个字符串转换为日期对象,首先定义一个具有日期格式的字符串
        NSString *dateString = @"2019-03-26 12:21:20";
        // 初始化一个日期格式化对象,并设置它的日期和时间格式
        NSDateFormatter *formatter3 = [[NSDateFormatter alloc] init];
        [formatter3 setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
        // 使用这个日期格式化对象,将它转换为日期对象
        NSDate *convertedDate = [formatter3 dateFromString:dateString];
        NSLog(@"convertedDate = %@",convertedDate);
        
    }
    
    return 0;
}

截屏2020-12-14 下午9.49.10

原文地址:https://www.cnblogs.com/exlo/p/14135909.html