常用的日期处理

在项目开发中经常会碰到需要对日期一些操作、格式化之类的,下面列了几个常用的日期处理

/**
 *  日期类型转字符串
 *
 *  @param inputDate 需要格式的日期
 *  @param formatter 格式化:例如@"yyyy.MM"
 *
 *  @return 格式化后的字符串
 */
+(NSString *)dateStringFormatWithDate:(NSDate*)inputDate  dateFormatter:(NSString *)formatter{
    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
    [dateFormatter setDateFormat:formatter];
    
    NSTimeZone *timeZone = [[NSTimeZone alloc] initWithName:@"Asia/Shanghai"];
    [dateFormatter setTimeZone: timeZone];
    
    return [dateFormatter stringFromDate:inputDate];
}
/**
 *  字符串转日期类型
 *
 *  @param DateStr    需要进行转换的日期String
 *  @param formatter  格式化:例如@"yyyy.MM"
 *
 *  @return 日期NSDate*
 */
+(NSDate *)DateFormatWithDateString:(NSString*)DateStr dateFormatter:(NSString *)formatter{
    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
    [dateFormatter setDateFormat:formatter];
    
    NSTimeZone *timeZone = [[NSTimeZone alloc] initWithName:@"Asia/Shanghai"];
    [dateFormatter setTimeZone: timeZone];
    
    NSDate *currentDate = [dateFormatter dateFromString:DateStr];
    
    return currentDate;
}

/**
 *  时间戳转换字符串,返回距1970年的时间
 *
 *  @param timeStamp  时间戳
 *  @param dateFormat 时间格式 @"yyyy-MM-dd HH:mm:ss (EEE)"
 *
 *  @return 转换结果
 */
+ (NSString *)timeIntervalSince1970:(NSString *)timeStamp format:(NSString *)dateFormat
{
    
    if (timeStamp.length == 13) {
        timeStamp = [timeStamp substringToIndex:10];
    }
    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:dateFormat];
    long long dateline = [timeStamp longLongValue];
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:dateline];
    return [dateFormatter stringFromDate:date];
}

//转换时区为当前地区
+(NSDate *)getNowDateFromatAnDate:(NSDate *)anyDate{
    //设置源日期时区
    NSTimeZone* sourceTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"UTC"];//或GMT
    //设置转换后的目标日期时区
    NSTimeZone* destinationTimeZone = [NSTimeZone localTimeZone];
    //得到源日期与世界标准时间的偏移量
    NSInteger sourceGMTOffset = [sourceTimeZone secondsFromGMTForDate:anyDate];
    //目标日期与本地时区的偏移量
    NSInteger destinationGMTOffset = [destinationTimeZone secondsFromGMTForDate:anyDate];
    //得到时间偏移量的差值
    NSTimeInterval interval = destinationGMTOffset - sourceGMTOffset;
    //转为现在时间
    NSDate* destinationDateNow = [[NSDate alloc] initWithTimeInterval:interval sinceDate:anyDate];
    return destinationDateNow;
}
//返回上个月日期
+ (NSDate *)lastMonth:(NSDate *)date{
    NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
    dateComponents.month = -1;
    NSDate *newDate = [[NSCalendar currentCalendar] dateByAddingComponents:dateComponents toDate:date options:0];
    return newDate;
}

//返回下个月日期
+ (NSDate *)nextMonth:(NSDate *)date{
    NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
    dateComponents.month = 1;
    NSDate *newDate = [[NSCalendar currentCalendar] dateByAddingComponents:dateComponents toDate:date options:0];
    return newDate;
}

//返回月份
+ (NSInteger)month:(NSDate *)date{
    NSDateComponents *components = [[NSCalendar currentCalendar] components:(NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay) fromDate:date];
    return [components month];
}
//返回年份
+ (NSInteger)year:(NSDate *)date{
    NSDateComponents *components = [[NSCalendar currentCalendar] components:(NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay) fromDate:date];
    return [components year];
}
//天
+ (NSInteger)day:(NSDate *)date{
    NSDateComponents *components = [[NSCalendar currentCalendar] components:(NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay) fromDate:date];
    return [components day];
}
//小时
+ (NSInteger)hour:(NSDate *)date{
    NSDateComponents *components = [[NSCalendar currentCalendar] components:(NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour) fromDate:date];
    return [components hour];
}
//分
+ (NSInteger)minute:(NSDate *)date{
    NSDateComponents *components = [[NSCalendar currentCalendar] components:(NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitMinute) fromDate:date];
    return [components minute];
}

之前项目需要展示某些日期对应周几的功能,下面是我是使用的简单转换功能。

 *  获取某一天是周几
 *
 *  @param inputDate 传入NSDate日期 NSDate *
 *  @param datestr   或者传入字符串日期 NSString * 『格式@"yyyy-MM-dd"』
 *
 *  @return
 */
+(int)weekdayStringFromDate:(NSDate *)inputDate DateStr:(NSString *)datestr{
    
    NSDate *currentDate = nil;
    
    if (inputDate != nil) {
        currentDate = inputDate;
    }else{
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
        [dateFormatter setDateFormat:@"yyyy-MM-dd"];
        currentDate = [dateFormatter dateFromString:datestr];
    }
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    
    NSTimeZone *timeZone = [[NSTimeZone alloc] initWithName:@"Asia/Shanghai"];
    
    [calendar setTimeZone: timeZone];
    
    NSCalendarUnit calendarUnit = NSWeekdayCalendarUnit;
    
    NSDateComponents *theComponents = [calendar components:calendarUnit fromDate:currentDate];
    
    return (int)theComponents.weekday;
}

//转成成对应『周几』
+(NSString*)weekdayStringFromIndex:(int)index{
    
    NSArray *weekdays = [NSArray arrayWithObjects: [NSNull null], @"周日", @"周一", @"周二", @"周三", @"周四", @"周五", @"周六", nil];
    return [weekdays objectAtIndex:index];
}


你可能感兴趣的:(常用的日期处理)