常用的日期处理

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

/**
 *  日期类型转字符串
 *
 *  @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];
}


最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,593评论 25 708
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,993评论 19 139
  • 注:文中草药疗效皆自《本草纲目》,仅供创作参考,不具备现实功效。 丽春草,亦名定参草、仙女蒿、 赛牡丹、虞美人。 ...
    贵鬼先生阅读 2,287评论 9 18
  • 今天乖乖生病去医院,外婆很着急,医生要求打点滴。我心态一下也被她的磁场感染了,也很着急。我突然觉察到,我妈妈是把无...
    weeklybright阅读 212评论 0 1
  • 姓名:王铃女 公司:宁波禾隆新材料股份有限公司 组别:312期乐观一组 【日精进打卡第15天】 【知~学习】 《六...
    埁埁妈阅读 99评论 0 0