文章目录
  1. 1. 时间戳转化为日期
  2. 2. //日期转化为时间戳
  3. 3. 距离现在的时间表示

时间戳转化为日期

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
-(NSString *)UTCchangeDate:(NSString *)utc{

NSTimeInterval time = [utc doubleValue];

NSDate *date=[NSDate dateWithTimeIntervalSince1970:time];

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

[dateformatter setDateFormat:@"MM.dd HH:mm"];

NSString *staartstr=[dateformatter stringFromDate:date];

return staartstr;

}

//日期转化为时间戳

1
2
3
4
5
6
7
-(NSString *)dataChangeUTC:( NSDate*)date{

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

return timeSp;

}

距离现在的时间表示

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
- (NSDateComponents *)deltaFrom:(NSDate *)from
{
// 日历
NSCalendar *calendar = [NSCalendar currentCalendar];

// 比较时间
NSCalendarUnit unit = NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;

return [calendar components:unit fromDate:from toDate:self options:0];
}

- (BOOL)isThisYear
{
// 日历
NSCalendar *calendar = [NSCalendar currentCalendar];

NSInteger nowYear = [calendar component:NSCalendarUnitYear fromDate:[NSDate date]];
NSInteger selfYear = [calendar component:NSCalendarUnitYear fromDate:self];

return nowYear == selfYear;
}

//- (BOOL)isToday
//{
// // 日历
// NSCalendar *calendar = [NSCalendar currentCalendar];
//
// NSCalendarUnit unit = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay;
//
// NSDateComponents *nowCmps = [calendar components:unit fromDate:[NSDate date]];
// NSDateComponents *selfCmps = [calendar components:unit fromDate:self];
//
// return nowCmps.year == selfCmps.year
// && nowCmps.month == selfCmps.month
// && nowCmps.day == selfCmps.day;
//}

- (BOOL)isToday
{
NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
fmt.dateFormat = @"yyyy-MM-dd";

NSString *nowString = [fmt stringFromDate:[NSDate date]];
NSString *selfString = [fmt stringFromDate:self];

return [nowString isEqualToString:selfString];
}

- (BOOL)isYesterday
{
// 2014-12-31 23:59:59 -> 2014-12-31
// 2015-01-01 00:00:01 -> 2015-01-01

// 日期格式化类
NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
fmt.dateFormat = @"yyyy-MM-dd";

NSDate *nowDate = [fmt dateFromString:[fmt stringFromDate:[NSDate date]]];
NSDate *selfDate = [fmt dateFromString:[fmt stringFromDate:self]];

NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *cmps = [calendar components:NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear fromDate:selfDate toDate:nowDate options:0];

return cmps.year == 0
&& cmps.month == 0
&& cmps.day == 1;
}
文章目录
  1. 1. 时间戳转化为日期
  2. 2. //日期转化为时间戳
  3. 3. 距离现在的时间表示