我们在开发中会遇到各种对于时间戳的操作,这里分享下比较常用的几种。
1.当前时间戳
1 NSString *timeSp = [NSString stringWithFormat:@"%ld", (long)[[NSDate date]timeIntervalSince1970]]; 2 NSLog(@"现在时间戳%@",timeSp);
2.今天凌晨时间戳 (直接复制粘贴该方法即可,返回值即为今天凌晨时间戳)
1 #pragma mark 今天凌晨时间戳 2 3 -(NSString*)getLingChenTimeStamp 4 { 5 NSInteger year,month,day,hour,min,sec,week; 6 7 NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian]; 8 9 NSDate *now = [NSDate date];; 10 11 NSDateComponents *comps = [[NSDateComponents alloc] init]; 12 13 NSInteger unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitWeekday | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond; 14 15 comps = [calendar components:unitFlags fromDate:now]; 16 17 year = [comps year]; 18 19 week = [comps weekday]; 20 21 month = [comps month]; 22 23 day = [comps day]; 24 25 hour = [comps hour]; 26 27 min = [comps minute]; 28 29 sec = [comps second]; 30 31 NSString * dateString = @""; 32 33 dateString = [NSString stringWithFormat:@"%ld-%ld-%ld 00:00:00",(long)year,(long)month,(long)day];//修改此处的 00:00:00 可以得到想要的时间 例如 12:00:00
34 35 NSDateFormatter * dateFormat = [[NSDateFormatter alloc]init]; 36 37 [dateFormat setDateFormat:@"yyyy-MM-dd hh:mm:ss"]; 38 NSDate * date = [dateFormat dateFromString:dateString]; 39 40 NSString * today = [NSString stringWithFormat:@"%ld",(long)[datetimeIntervalSince1970]]; 41 42 NSLog(@"今天凌晨时间戳:%@",today); 43 return today; 44 }
3.周日凌晨时间戳 (直接复制粘贴该方法即可,返回值可以是 本周周日凌晨 上周周日凌晨 下周周日凌晨时间戳)
1 -(NSString*)getSundayTime 2 { 3 NSIntegeryear,month,day,hour,min,sec,week; 4 5 NSCalendar *calendar = [[NSCalendaralloc]initWithCalendarIdentifier:NSCalendarIdentifierGregorian]; 6 7 NSDate*now = [NSDate date];; 8 9 NSDateComponents *comps = [[NSDateComponents alloc] init]; 10 11 NSInteger unitFlags = NSCalendarUnitYear |NSCalendarUnitMonth |NSCalendarUnitDay|NSCalendarUnitWeekday | NSCalendarUnitHour |NSCalendarUnitMinute |NSCalendarUnitSecond; 12 13 comps = [calendar components:unitFlags fromDate:now]; 14 15 year = [comps year]; 16 17 week = [comps weekday]; 18 19 month = [comps month]; 20 21 day = [comps day]; 22 23 hour = [comps hour]; 24 25 min = [comps minute]; 26 27 sec = [comps second]; 28 29 NSString * todayDateString = @""; 30 31 todayDateString = [NSString stringWithFormat:@"%ld-%ld-%ld 00:00:00",(long)year,(long)month,(long)day];//修改这里00:00:00可以得到想要的时间 例如 12:00:00 32 33 34 NSDateFormatter * todayDateFormat = [[NSDateFormatter alloc]init]; 35 36 [todayDateFormat setDateFormat:@"yyyy-MM-dd hh:mm:ss"]; 37 38 NSDate * todayDate = [todayDateFormat dateFromString:todayDateString]; 39 40 NSDate* thisweek = [NSDate dateWithTimeIntervalSince1970:([todayDate timeIntervalSince1970] +(8 - week) * 24*60*60)]; 41 42 NSString * thisWeekStmp = [NSString stringWithFormat:@"%ld",(long)[thisweektimeIntervalSince1970]];//本周日凌晨时间戳 43 44 NSString * lastlastWeekStmp = [NSString stringWithFormat:@"%ld",(long)[thisweektimeIntervalSince1970]-3600*24*7*2];//上上周日凌晨时间戳 45 46 NSString * lastWeekStmp = [NSString stringWithFormat:@"%ld",(long)[thisweektimeIntervalSince1970]-3600*24*7];//上周日凌晨时间戳 47 48 NSString * nextWeekStmp = [NSString stringWithFormat:@"%ld",(long)[thisweektimeIntervalSince1970]+3600*24*7];//下周日凌晨时间戳 49 50 NSString * nextnextWeekStmp = [NSString stringWithFormat:@"%ld",(long)[thisweektimeIntervalSince1970]+3600*24*7*2];//下周日凌晨时间戳 51 52 NSLog(@"本周周日凌晨时间戳%@",thisWeekStmp); 53 54 NSLog(@"上周周日凌晨时间戳%@",lastWeekStmp); 55 56 NSLog(@"上上周周日凌晨时间戳%@",lastlastWeekStmp); 57 58 NSLog(@"下周日凌晨时间戳%@",nextWeekStmp); 59 60 NSLog(@"下下周日凌晨时间戳%@",nextnextWeekStmp); 61 62 return thisWeekStmp; 63 }
源码提取 :http://yunpan.cn/ccvmsdsdacw23 (提取码:b2e5)
时间: 2024-10-11 17:40:33