NSDate
来源:互联网 发布:淘宝支点运动是真的吗 编辑:程序博客网 时间:2024/05/01 18:06
// 获取代表当前日期、时间的NSDate
NSDate* date1 = [NSDate date];
NSLog(@"%@" , date1);
// 获取从当前时间开始,一天之后的日期
NSDate* date2 = [[NSDate alloc]
initWithTimeIntervalSinceNow:3600*24];
NSLog(@"%@" , date2);
// 获取从当前时间开始,3天之前的日期
NSDate* date3 = [[NSDate alloc]
initWithTimeIntervalSinceNow: -3*3600*24];
NSLog(@"%@" , date3);
// 获取从1970年1月1日开始,20年之后的日期
NSDate* date4 = [NSDate dateWithTimeIntervalSince1970:
3600 * 24 * 366 * 20];
NSLog(@"%@" , date4);
// 获取系统当前的NSLocale
NSLocale* cn = [NSLocale currentLocale];
// 获取NSDate在当前NSLocale下对应的字符串
NSLog(@"%@" , [date1 descriptionWithLocale:cn]);
// 获取两个日期之间较早的日期
NSDate* earlier = [date1 earlierDate:date2];
// 获取两个日期之间较晚的日期
NSDate* later = [date1 laterDate:date2];
// 比较两个日期,compare:方法返回NSComparisonResult枚举值,
// 该枚举类型包含NSOrderedAscending、NSOrderedSame和
// NSOrderedDescending三个值,
// 分别代表调用compare:的日期位于被比较日期之前、相同、之后
switch ([date1 compare:date3])
{
case NSOrderedAscending:
NSLog(@"date1位于date3之前");
break;
case NSOrderedSame:
NSLog(@"date1与date3日期相等");
break;
case NSOrderedDescending:
NSLog(@"date1位于date3之后");
break;
}
// 获取两个时间之间的时间差
NSLog(@"date1与date3之间时间差%g秒"
, [date1 timeIntervalSinceDate:date3]);
// 获取指定时间与现在时间差
NSLog(@"date2与现在时间差%g秒"
, [date2 timeIntervalSinceNow]);
// 需要被格式化的时间
// 获取从1970年1月1日开始,20年之后的日期
NSDate* dt = [NSDate dateWithTimeIntervalSince1970:
3600 * 24 * 366 * 20];
// 创建两个NSLocale,分别代表中国、美国
NSLocale* locales[] = {
[[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"]
, [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"]};
NSDateFormatter* df[8];
//为上面2个NSLocale创建8个NSDateFormatter对象
for (int i = 0 ; i < 2 ; i++)
{
df[i * 4] = [[NSDateFormatter alloc] init];
// 设置NSDateFormatter的日期、时间风格
[df[i * 4] setDateStyle:NSDateFormatterShortStyle];
[df[i * 4] setTimeStyle:NSDateFormatterShortStyle];
// 设置NSDateFormatter的NSLocale
[df[i * 4] setLocale: locales[i]];
df[i * 4 + 1] = [[NSDateFormatter alloc] init];
// 设置NSDateFormatter的日期、时间风格
[df[i * 4 + 1] setDateStyle:NSDateFormatterMediumStyle];
[df[i * 4 + 1] setDateStyle:NSDateFormatterMediumStyle];
// 设置NSDateFormatter的NSLocale
[df[i * 4 + 1] setLocale: locales[i]];
df[i * 4 + 2] = [[NSDateFormatter alloc] init];
// 设置NSDateFormatter的日期、时间风格
[df[i * 4 + 2] setDateStyle:NSDateFormatterLongStyle];
[df[i * 4 + 2] setTimeStyle:NSDateFormatterLongStyle];
// 设置NSDateFormatter的NSLocale
[df[i * 4 + 2] setLocale: locales[i]];
df[i * 4 + 3] = [[NSDateFormatter alloc] init];
// 设置NSDateFormatter的日期、时间风格
[df[i * 4 + 3] setDateStyle:NSDateFormatterFullStyle];
[df[i * 4 + 3] setTimeStyle:NSDateFormatterFullStyle];
// 设置NSDateFormatter的NSLocale
[df[i * 4 + 3] setLocale: locales[i]];
}
for (int i = 0 ; i < 2 ; i++)
{
switch (i)
{
case 0:
NSLog(@"-------中国日期格式--------");
break;
case 1:
NSLog(@"-------美国日期格式--------");
break;
}
NSLog(@"SHORT格式的日期格式:%@", [df[i * 4] stringFromDate: dt]);
NSLog(@"MEDIUM格式的日期格式:%@", [df[i * 4 + 1] stringFromDate: dt]);
NSLog(@"LONG格式的日期格式:%@", [df[i * 4 + 2] stringFromDate: dt]);
NSLog(@"FULL格式的日期格式:%@", [df[i * 4 + 3] stringFromDate: dt]);
}
NSDateFormatter* df2 = [[NSDateFormatter alloc] init];
// 设置自定义的格式器模板
[df2 setDateFormat:@"公元yyyy年MM月DD日 HH时mm分"];
// 执行格式化
NSLog(@"%@" , [df2 stringFromDate:dt]);
NSString* dateStr = @"2013-03-02";
NSDateFormatter* df3 = [[NSDateFormatter alloc] init];
// 根据日期字符串的格式设置格式模板
[df3 setDateFormat:@"yyyy-MM-dd"];
// 将字符串转换为NSDate对象
NSDate* date2 = [df3 dateFromString: dateStr];
NSLog(@"%@" , date2);
-(void)createButton
{
button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(100, 100, 50, 50);
[button setTitle:@"发送" forState:UIControlStateNormal];
button.backgroundColor = [UIColor lightGrayColor];
button.tag = 10;
[button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
-(void)buttonClicked:(UIButton *)button
{
NSLog(@"123");
//启动定时器
_number =61;
//每隔1秒执行一次
_timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timer:) userInfo:@"123" repeats:YES];
[_timer fire];
}
-(void)timer:(NSTimer *)timer
{
_number -= 1;
if (_number > 0) {
[button setTitle:[NSString stringWithFormat:@"%ld",_number] forState:UIControlStateNormal];
[button setEnabled:NO];
}else{
if (_timer) {
[_timer setFireDate:[NSDate distantFuture]];
[button setEnabled:YES];
}
}
}
- NSDate
- NSDate
- NSDate
- NSDate
- NSDate
- NSDate
- NSDate
- NSDate
- NSDate
- NSDate
- NSDate
- NSDate
- NSDate
- NSDate
- NSDate
- NSDate
- NSDate
- nsdate
- Java Notes-13(Oberver, Sockets)
- Triangle
- 命中缺高人指点
- 大型网站核心的架构(6-2)高性能
- [2013]蟠桃记
- NSDate
- kmp理解
- Java笔试和面试题目
- 设计模式之观察者
- 转 编程: VC++学习方法及书籍
- 写个益智小游戏 看你能过关不?
- NSNotificationCenter
- 逗号运算符
- Centos6.7 Centos7.0 安装OpenVSwitch痛苦经历