和JAVA一样,IOS也有自己的日期格式。JAVA里面是SimpleDateFormat进行日期格式化输出。下面提供三个函数对日期的操作。
//利用字符串获取该字符串所表示日期的上一天
-(NSString*)upDate:(NSString*)nowdate{
NSTimeInterval secondsPerDay = 24 * 60 * 60;
NSDate *yesterday;
yesterday=[[self convertDateFromString:nowdate] dateByAddingTimeInterval: -secondsPerDay];
NSDateFormatter *f=[[NSDateFormatter alloc] init];
[f setDateFormat:@"yyyy-MM-dd"];
NSString *date=[NSString stringWithFormat:@"%@",[f stringFromDate:yesterday]];
return date;
}
//利用字符串获取该字符串所表示日期的下一天
-(NSString*)downDate:(NSString*)nowdate{
NSTimeInterval secondsPerDay = 24 * 60 * 60;
NSDate *tomorrow;
tomorrow=[[self convertDateFromString:nowdate] dateByAddingTimeInterval: secondsPerDay];
NSDateFormatter *f=[[NSDateFormatter alloc] init];
[f setDateFormat:@"yyyy-MM-dd"];
NSString *date=[NSString stringWithFormat:@"%@",[f stringFromDate:tomorrow]];
return date;
}
//将字符串转化为日期
-(NSDate*) convertDateFromString:(NSString*)uiDate
{
NSDateFormatter *formatter = [[NSDateFormatter alloc] init] ;
[formatter setDateFormat:@"yyyy-MM-dd"];
NSDate *date=[formatter dateFromString:uiDate];
return date;
}
本文链接:https://it72.com/2250.htm