NSDate 基本用法
来源:互联网 发布:数据库安全防护 编辑:程序博客网 时间:2024/04/28 22:55
#import <Foundation/Foundation.h>
int main(int argc,constchar * argv[])
{
@autoreleasepool
{
//创建时间对象
NSDate *nowdate=[NSDatedate];
//打印的是0时区的时间,北京(东-8)
NSLog(@"%@",nowdate);
//创建一定间隔时间的对象,NSTimeInterval的默认单位是秒,当前对象设置的是1天
NSTimeInterval timeinterval=24*60*60;
nowdate=[NSDatedateWithTimeIntervalSinceNow:timeinterval];
NSLog(@"%@",nowdate);
nowdate=[NSDatedateWithTimeIntervalSinceNow:-timeinterval];
NSLog(@"%@",nowdate);
//时间转换成字符串
// 日期格式化类
NSDateFormatter *dateformatter=[[NSDateFormatteralloc]init];
// y 年 M 月 d 日
// m 分 s 秒 H (24)时 h(12)时
// e 周 a 上午或下午
[dateformattersetDateFormat:@"yyyy-MM-dd eee HH:mm:ss aaaa"];
[dateformattersetAMSymbol:@"上午"];
[dateformatter setPMSymbol:@"下午"];
//[dateformatter setDateStyle:NSDateFormatterFullStyle];
NSString *strDate=[dateformatterstringFromDate:[NSDatedate]];
NSLog(@"%@",strDate);
//字符串转换成时间
NSString *string=@"2013-12-7 21:23:50";
NSDate *date=[dateformatterdateFromString:string];
NSLog(@"%@",date);
//获取日期各个部分整数值
NSDate *now=[NSDatedate];
//年
NSInteger nowyear=[[nowdateWithCalendarFormat:niltimeZone:nil]yearOfCommonEra];
NSLog(@"%ld",(long)nowyear);
//月
NSInteger nowmonth=[[nowdateWithCalendarFormat:niltimeZone:nil]monthOfYear];
NSLog(@"%ld",nowmonth);
//日
NSInteger nowday=[[nowdateWithCalendarFormat:niltimeZone:nil]dayOfMonth];
NSLog(@"%ld",nowday);
//时
NSInteger nowhour=[[nowdateWithCalendarFormat:niltimeZone:nil]hourOfDay];
NSLog(@"%ld",nowhour);
//分
NSInteger nowmin=[[nowdateWithCalendarFormat:niltimeZone:nil]minuteOfHour];
NSLog(@"%ld",nowmin);
//秒
NSInteger nowsec=[[nowdateWithCalendarFormat:niltimeZone:nil]secondOfMinute];
NSLog(@"%ld",nowsec);
//周几
NSInteger nowweek=[[nowdateWithCalendarFormat:niltimeZone:nil]dayOfWeek];
NSLog(@"%ld",nowweek);
}
return0;
}
- NSDate基本用法
- NSDate 基本用法
- 黑马程序员-oc基础-NSDate基本用法
- NSDate基本使用方法
- NSDate的常用用法
- NSDate的常用用法
- NSDate的常用用法
- NSDate的常用用法
- NSDate的常用用法
- NSDate的常用用法
- NSDate的常用用法
- NSDate的常用用法
- NSDate的常用用法
- NSDate的常用用法
- NSDate的常用用法
- NSDate的常用用法
- NSDate的常用用法
- NSDate简单用法
- 菜鸟学JAVA之JDK安装(Win7)
- python 实现多线程
- c语言学习1
- C++编程规范之8:不要进行不成熟的优化
- Android进程的生命周期
- NSDate 基本用法
- 虚拟机的历史
- 博客收藏
- 【HDU 2594 KMP】
- Scanner 输入数组并读取的方法
- Dalvik虚拟机的目录结构
- iOS memory management 2
- 金士顿 8G u盘 红色 量产记录
- 微型发布系统:Android+FastJSON+PHP