时间相关

来源:互联网 发布:手绘自拍软件 编辑:程序博客网 时间:2024/05/18 23:27

       NSDate *senddate = [NSDatedate];

        

        NSLog(@"date1时间戳 = %ld",time(NULL));

        NSString *date2 = [NSStringstringWithFormat:@"%ld", (long)[senddatetimeIntervalSince1970]];

        NSLog(@"date2时间戳 = %@",date2);

        

        NSDateFormatter *dateformatter = [[NSDateFormatteralloc]init];

        [dateformatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];

        NSString *date1 = [dateformatterstringFromDate:senddate];

        NSLog(@"获取当前时间   = %@",date1);

        

        // 时间戳转时间

        NSDate *confromTimesp = [NSDatedateWithTimeIntervalSince1970:[senddate timeIntervalSince1970]];

        NSString *confromTimespStr = [dateformatterstringFromDate:confromTimesp];

        NSLog(@"时间戳转时间   = %@",confromTimespStr);



// 两个时间戳的间隔   返回的是秒

        NSTimeInterval time = [nowtimeIntervalSinceDate:oldDate];

        NSLog(@"time:%f",time/60/60/24);

、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、


1.获取当前时间

+(NSString *)getCurrentDate

{

    //获取当前时间

    NSDate *now = [NSDate date];

    KELog(@"now date is: %@", now);

    

    NSCalendar *calendar = [NSCalendar currentCalendar];

    NSUInteger unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;

    NSDateComponents *dateComponent = [calendar components:unitFlags fromDate:now];

    

    NSInteger year = [dateComponent year];

    NSInteger month = [dateComponent month];

    NSInteger day = [dateComponent day];

    NSInteger hour = [dateComponent hour];

    NSInteger minute = [dateComponent minute];

    NSInteger second = [dateComponent second];

    NSString * currentDate = [NSString stringWithFormat:@"%ld-%.2ld-%.2ld %.2ld:%.2ld:%.2ld",year,month,day,hour,minute,second];

    

    return currentDate;

}


2.NSDate转换成时间戳

          NSDate *datenow = [NSDate date];//现在时间,你可以输出来看下是什么格式

        

        NSString *timeSp = [NSString stringWithFormat:@"%d", (long)[datenow timeIntervalSince1970]];

        NSLog(@"timeSp:%@",timeSp); //时间戳的值时间戳转时间的方法


//NSString NSDate

+(NSDate *)dateWithString:(NSString *)dataString

{

    NSDateFormatter * format = [[NSDateFormatter alloc] init];

    

    [format setDateFormat:@"yyyy-MM-dd HH:mm:ss"];

    

    NSDate * date = [format dateFromString:[self getCurrentDate]];

    

    return date;

}


//NSDate NSString

+(NSString *)stringWithDate:(NSDate *)date{

    

    NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init];

    

    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];

    

    NSString*strDate = [dateFormatter stringFromDate:[NSDate date]];

    

    NSLog(@"%@",strDate);

    

    [dateFormatter release];

    

    return strDate;

}



0 0
原创粉丝点击