Objective-c语言_练习题3
来源:互联网 发布:周杰伦淘宝店是真的吗 编辑:程序博客网 时间:2024/04/28 03:07
//dateString 格式:2015-12-17 08:08:08
/*
传入时间与现在时间差距60秒内,输出@“刚刚”
差距1小时内,输出@“xx分钟前”
差距1-24小时内,输出@“xx小时前”
差距大于一天,输出@“xx天前”
差距大于30天,输出@“xx个月前”
差距大于365天,输出@“完整日期”
*/
#import <Foundation/Foundation.h>
#import "Tool.h"
int main(int argc,const char * argv[]) {
@autoreleasepool {
/*
char *Cstring = "This is a String!"; //C语言字符串
NSString *astring = [[NSString alloc] initWithCString:Cstring]; //转换到OC字符串
*/
// NSString *dateString;//距离时间
NSString *dateString=@"2015-12-17 8:08:08";
/*
//控制台输入方式
NSMutableArray *array=[NSMutableArray array];
char shuru[1000];
printf("输入时间(yyyy-MM-dd HH:mm:ss):\n");
gets(shuru);
NSString *dateString = [[NSString alloc] initWithCString:shuru];
*/
NSLog(@"%@",[ToolhandleDate:dateString]);
}
return 0;
}
=================================================================
Tool.h
#import <Foundation/Foundation.h>
@interface Tool : NSObject
+(NSString *)handleDate:(NSString *)dateString;
@end
#import "Tool.h"
@implementation Tool
+(NSString *)handleDate:(NSString *)dateString
{
NSString *string;
NSDateFormatter *dateFormatter=[NSDateFormatternew];
[dateFormatter setTimeZone:[NSTimeZonetimeZoneWithAbbreviation:@"UTC"]];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate *date=[dateFormatter dateFromString:dateString];
NSTimeInterval timeInterval=date.timeIntervalSinceNow;
timeInterval=-(timeInterval-28800);
/*
传入时间与现在时间差距60秒内,输出@“刚刚”
差距1小时内,输出@“xx分钟前”
差距1-24小时内,输出@“xx小时前”
差距大于一天,输出@“xx天前”
差距大于30天,输出@“xx个月前”
差距 大于365天,输出@“完整日期”
*/
if (timeInterval<60)
{
string=@"刚刚";
}
if (timeInterval>60 && timeInterval<3600)//大于1分小于1个小时
{
float mm;
mm=(timeInterval/60);
string=[NSString stringWithFormat:@"%.0f分钟以前",mm];
}
if (timeInterval>3600 && timeInterval<86400)
{
float HH;
HH=((timeInterval/60)/60);
string=[NSString stringWithFormat:@"%.0f小时以前",HH];
}
if(timeInterval>86400 && timeInterval<2592000)
{
float day;
day=((timeInterval/60)/60)/24;
string=[NSString stringWithFormat:@"%.0f天前",day];
}
if(timeInterval>2592000 && timeInterval<31104000)
{
float year;
year=(((timeInterval/60)/60)/24)/30;
string=[NSString stringWithFormat:@"%.0f个月前",year];
}
if(timeInterval>31104000)
{
string=[NSString stringWithFormat:@"%@",dateString];
}
//string=[NSString stringWithFormat:@"%f",timeInterval];
return string;
}
@end
- Objective-c语言_练习题3
- Objective-c语言_练习题3
- Objective-c语言_数组练习题1
- Objective-c语言_数组练习题2:
- Objective-C语言_数组练习题
- Objective-c语言_复合
- Objective-c语言_类目
- Objective-c语言_延展
- Objective-c语言_协议
- Objective-c语言_计算机网络
- 1004: C语言考试练习题_排列
- C语言考试练习题_排列
- c语言练习题3
- C语言练习题(3)
- Objective-c语言_面向对象(封装)
- Objective-c语言_面向对象(继承)
- Objective-c语言_面向对象(多态)
- Objective-c语言_不可变字典
- iOS分类的那些坑
- 广点通sdk接入 _原生广告
- Merge Two Sorted Lists
- C++时间标准库时间Time及系统时间的调用
- 网页去重(二)最小编辑法
- Objective-c语言_练习题3
- 访问SAP时提示报错SAP GUI for Windows 720窗口
- 60.NSNotification通知(换肤)
- 存储过程的优缺点
- React Native Android 使用友盟SDK功能
- 一些公司的2016年校招C/C++开发岗笔试题目(四)
- VS中的 MD/MT设置
- 设计模式之代理模式
- 118Pascal's Triangle