iOS时间字符串格式化输出技巧详解

2020-01-18 22:25:45王旭

再写一个统一转换时间字符串为 NSDate的方法,如下:


+(NSDate *)xh_dateWithDateString:(NSString *)dateString
{
  NSDate *date = nil;
  date = [self xh_dateWithFormat_yyyy_MM_dd_HH_mm_ss_string:dateString];
  if(date) return date;
  date = [self xh_dateWithFormat_yyyy_MM_dd_HH_mm_string:dateString];
  if(date) return date;
  date = [self xh_dateWithFormat_yyyy_MM_dd_HH_string:dateString];
  if(date) return date;
  date = [self xh_dateWithFormat_yyyy_MM_dd_string:dateString];
  if(date) return date;
  date = [self xh_dateWithFormat_yyyy_MM_string:dateString];
  if(date) return date;
  return nil;
}

四.将NSDate -> NSDateFormatter

为什么要再转换为NSDateFormatter,有些人可能已经明白了,我们点开NSDateFormatter可以看到NSDateFormatter有以下属性


@property (nullable, copy) NSCalendar *calendar NS_AVAILABLE(10_7, 4_0);
@property (nullable, copy) NSTimeZone *timeZone NS_AVAILABLE(10_7, 4_0);
@property NSInteger era;
@property NSInteger year;
@property NSInteger month;
@property NSInteger day;
@property NSInteger hour;
@property NSInteger minute;
@property NSInteger second;
@property NSInteger nanosecond NS_AVAILABLE(10_7, 5_0);
@property NSInteger weekday;
@property NSInteger weekdayOrdinal;
@property NSInteger quarter NS_AVAILABLE(10_6, 4_0);
@property NSInteger weekOfMonth NS_AVAILABLE(10_7, 5_0);
@property NSInteger weekOfYear NS_AVAILABLE(10_7, 5_0);
@property NSInteger yearForWeekOfYear NS_AVAILABLE(10_7, 5_0);
@property (getter=isLeapMonth) BOOL leapMonth NS_AVAILABLE(10_8, 6_0);
@property (nullable, readonly, copy) NSDate *date NS_AVAILABLE(10_7, 4_0);

@end

我们新建一个NSDateComponents 的Category,笔者取名NSDateComponents+XHCategory,并实现如下方法:


+(NSDateComponents *)xh_dateComponentsFromDate:(NSDate *)date
{
  NSDateComponents *components = [[NSCalendar currentCalendar] components:NSCalendarUnitYear| NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitWeekOfYear | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond | NSCalendarUnitWeekday | NSCalendarUnitWeekdayOrdinal fromDate:date];
  return components;

}

接着我们就可以进行转换操作了,我们新建一个NSString的Category,笔者取名NSString+XHDateFormat

在NSString+XHDateFormat.h文件中写上需要转换的类型如下:


/**
 * x年x月x日
 */
@property(nonatomic,copy,readonly)NSString *xh_formatNianYueRi;

/**
 * x年x月
 */
@property(nonatomic,copy,readonly)NSString *xh_formatNianYue;

/**
 * x月x日
 */
@property(nonatomic,copy,readonly)NSString *xh_formatYueRi;

/**
 * x年
 */
@property(nonatomic,copy,readonly)NSString *xh_formatNian;

/**
 * x时x分x秒
 */
@property(nonatomic,copy,readonly)NSString *xh_formatShiFenMiao;

/**
 * x时x分
 */
@property(nonatomic,copy,readonly)NSString *xh_formatShiFen;

/**
 * x分x秒
 */
@property(nonatomic,copy,readonly)NSString *xh_formatFenMiao;

/**
 * yyyy-MM-dd
 */
@property(nonatomic,copy,readonly)NSString *xh_format_yyyy_MM_dd;

/**
 * yyyy-MM
 */
@property(nonatomic,copy,readonly)NSString *xh_format_yyyy_MM;

/**
 * MM-dd
 */
@property(nonatomic,copy,readonly)NSString *xh_format_MM_dd;

/**
 * yyyy
 */
@property(nonatomic,copy,readonly)NSString *xh_format_yyyy;

/**
 * HH:mm:ss
 */
@property(nonatomic,copy,readonly)NSString *xh_format_HH_mm_ss;

/**
 * HH:mm
 */
@property(nonatomic,copy,readonly)NSString *xh_format_HH_mm;

/**
 * mm:ss
 */
@property(nonatomic,copy,readonly)NSString *xh_format_mm_ss;

#pragma mark - 转换为星期几
@property(nonatomic,copy,readonly)NSString *xh_formatWeekDay;