美文网首页
常用的日期处理

常用的日期处理

作者: Luy7788 | 来源:发表于2017-07-31 01:44 被阅读6次

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

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


相关文章

  • 聊聊java的日期处理类

    java里面常用的日期处理类: java里面常用的日期处理类主要有: Date Calendar TimeZone...

  • 常用的日期处理

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

  • 【手把手教你】Python处理金融数据

    时间序列分析之日期处理 datetime处理日期 python常用的处理时间的库有:datetime,time,c...

  • SQL中常用的文本处理函数和日期时间处理函数

    SQL中常用的文本处理函数和日期时间处理函数 常用文本处理函数 CONCAT(str1, str2, ...):拼...

  • 日期 Date() :常用处理

    日期加一天 减一天 日期转换为yy-mm-ss hh:mm:ss Date对象的toLocaleString([...

  • mysql常用函数

    1、常用日期时间处理函数 2、常用字符串处理函数 3、流程控制函数 4、进制转换函数

  • 日期类的常用处理

    时间戳 时间戳的概念:某一个日期到1970年的秒数大小 成为该日期的时间戳。 NSDate NSDate的基本概念...

  • Carbon 中文文档

    Carbon是日期及时间处理包了,我们这里来看 日期及时间处理包在Laravel框架中的一些常用的使用。 在编写 ...

  • Carbon 用法

    Carbon是日期及时间处理包了,我们这里来看 日期及时间处理包在Laravel框架中的一些常用的使用。 在编写 ...

  • pandas 对日期类型数据的处理

    pandas 的日期/时间类型有如下几种: 本文介绍在处理时点数 (point in time) 一些常用的处理方...

网友评论

      本文标题:常用的日期处理

      本文链接:https://www.haomeiwen.com/subject/wukllxtx.html