• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
Yesi-悦思
"(program(computers) == *art) ? so : what" 修身岂为名传世,作事惟思利及人
博客园    首页    新随笔    联系   管理    订阅  订阅

NSDate

#pragma mark dateCreate

void dateCreate(){

NSDate  *date=[NSDate date];//调用当前时间 now

image

//返回以当前时间为基准,然后过了secs秒时间。

date=[NSDate  dateWithTimeIntervalSinceNow:10];//TimeInterval 是秒

//会比当前时间快10s

//基准为1970-1-1 00:00:00 然后过了secs秒时间

date=[NSDate  dateWithTimeIntervalSince1970:10];

//随机返回一个未来的时间

date=[NSDate distanceFuture];

//随机返回以前的某个时间

date=[NSDate distancePast];

NSLog(@”%@”,date);

}

#pragme mark 取回时间间隔

void dateUse(){

NSDate  *date=[NSDate date];

//返回1970走过的毫秒数

NSTimeInterval interval = [date timeIntervalSince1970];

//跟其他时间进行对比

NSDate date1=[NSDate dateWithTimeIntervalSinceNow:20];

[date timeIntervalSinceDate:date1];

//日期比较

NSDate *early=[date earlierDate:date1];//返回比较早的时间

NSDate *late=[date laterDate:date1];//返回比较晚的时间

image

}


void dateFormat(){

NSDate *date=[NSdate date];

//2015-6-4 22:48:45

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

//HH是24进制,hh是12进制

formatter.dateFormat=@”yyyy-MM-dd HH:mm:ss”;

//设置时区

formatter.locale=[[[NSLocale alloc]WithLocaleIdentinitifier:@”zh_CN“]autorelease];

NSString *str=[formatter stringFromDate:date]; //date->string

NSlog(@”%@”,string);

//返回的是格林治时间

NSDate *date2=[formatter dateFromString:@“2015-6-4 22:51:34”];

NSLog(@”%@”,date2);

[formatter release];

//NSCalendor

}

posted @ 2015-06-04 23:09  悦思  阅读(242)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3