自学iOS-获取当前时间
NSDate * senddate=[NSDate date]; NSDateFormatter *dateformatter=[[NSDateFormatter alloc] init]; [dateformatter setDateFormat:@"YYYYMMdd"]; NSString * locationString=[dateformatter stringFromDate:senddate]; NSLog(@"locationString:%@",locationString); [dateformatter release]; //这个有问题,显示日期不对
关大叔的写法
//获取当前时间
NSDate *now = [NSDate date];
NSLog(@”now date is: %@”, now); NSCalendar *calendar = [NSCalendar currentCalendar];
NSUInteger unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
NSDateComponents *dateComponent = [calendar components:unitFlags fromDate:now]; int year = [dateComponent year];
int month = [dateComponent month];
int day = [dateComponent day];
int hour = [dateComponent hour];
int minute = [dateComponent minute];
int second = [dateComponent second]; NSLog(@”year is: %d”, year);
NSLog(@”month is: %d”, month);
NSLog(@”day is: %d”, day);
NSLog(@”hour is: %d”, hour);
NSLog(@”minute is: %d”, minute);
NSLog(@”second is: %d”, second);
获取星期几(好用的)可以获取:年、月、日、星期几
NSDate *datet = [NSDate date];//现在时间
NSArray *weekdays = [NSArray arrayWithObjects: [NSNull null], @"Sunday", @"周一", @"周二", @"周三", @"周四", @"周五", @"周六", nil]; NSCalendar *calendars = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian]; NSTimeZone *timeZone = [[NSTimeZone alloc] initWithName:@"Asia/Shanghai"]; [calendars setTimeZone: timeZone]; NSCalendarUnit calendarUnit = NSCalendarUnitWeekday; NSDateComponents *theComponents = [calendars components:calendarUnit fromDate:datet]; NSInteger unitFlagss = NSCalendarUnitYear |
NSCalendarUnitMonth |
NSCalendarUnitDay |
NSCalendarUnitWeekday |
NSCalendarUnitHour |
NSCalendarUnitMinute |
NSCalendarUnitSecond; theComponents = [calendars components:unitFlagss fromDate:datet];
int week = [theComponents weekday];
int year=[theComponents year];
int month = [theComponents month];
int day = [theComponents day]; NSLog(@"%d年%d月",year,month);
NSLog(@"%d",day); NSLog(@"xingqi%@",[weekdays objectAtIndex:theComponents.weekday]);
自学iOS-获取当前时间的更多相关文章
- Swift3.0 iOS获取当前时间 - 年月日时分秒星期
Swift3.0 iOS获取当前时间 - 年月日时分秒星期func getTimes() -> [Int] { var timers: [Int] = [] // 返回的数组 let calen ...
- iOS 获取当前时间格式化字符串
iOS 获取当前时间格式化字符串 太阳火神的漂亮人生 (http://blog.csdn.net/opengl_es) 本文遵循"署名-非商业用途-保持一致"创作公用协议 转载请保 ...
- iOS - 获取当前时间日期星期几
//获取当前时间日期星期 - (NSString *)getCurrentTimeAndWeekDay { NSArray * arrWeek=[NSArray arrayWithObjects:@& ...
- iOS获取网络时间与转换格式
[NSDate date]可以获取系统时间,但是会造成一个问题,用户可以自己修改手机系统时间,所以有时候需要用网络时间而不用系统时间.获取网络标准时间的方法: 1.先在需要的地方实现下面的代码,创 ...
- iOS 获取当前时间以及计算年龄(时间差)
获取当前时间 NSDate *now = [NSDate date]; NSLog(@"now date is: %@", now); NSCalendar *calendar = ...
- iOS获取本地时间
NSDate *currentDate = [NSDate date];//获取当前时间,日期 NSDateFormatter *dateFormatter = [[NSDateFormatter a ...
- ios获取系统时间
//获取系统时间 NSDate * date=[NSDate date]; NSDateFormatter *dateformatter=[[NSDateFormatter alloc] init]; ...
- iOS - 获取系统时间年月日,阳历(公历)日期转农历的方法
//获取当前时间 NSDate *now = [NSDate date]; NSLog(@" now date is: %@ ",now); NSCalendar *calenda ...
- iOS获取当前时间
NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; // ----------设置你想要的格式,hh与HH的区别:分别表示12小时 ...
- IOS获取系统时间 NSDate
//返回当前时间,精确到毫秒.- (NSString *)getTimeNow { NSString* date; NSDateFormatter * formatter = [[NSDateForm ...
随机推荐
- gitblit-1.8.0域认证
gitblit-1.8.0\data\defaults.properties # # DEFAULTS.PROPERTIES # # The default Gitblit settings. # # ...
- C++中的随机函数
一.random函数不是ANSI C标准,不能在gcc,vc等编译器下编译通过. 可改用C++下的rand函数来实现. 1.C++标准函数库提供一随机数生成器rand,返回0-RAND_MA ...
- GIt帮助文档之创建新的Git仓库——现有目录下,通过导入所有文件来创建
1.新建仓库初始化操作 1.1打开Git Bash命令窗口,切换到项目文件夹目录: $ cd weixin 1.2执行命令: $ git init 初始化操作,把项目weixin纳入Git管理.初始化 ...
- Activiti6.0 java spring5 SSM 工作流引擎 审批流程 项目框架
1.模型管理 :web在线流程设计器.预览流程xml.导出xml.部署流程 2.流程管理 :导入导出流程资源文件.查看流程图.根据流程实例反射出流程模型.激活挂起 3.运行中流程:查看 ...
- Oracle 创建函数
Oracle创建函数的方法如下: CREATE OR REPLACE FUNCTION FunctionName ( --传入参数 para NCHAR ) RETURN NUMBER IS --函数 ...
- python爬虫#网络请求requests库
中文文档 http://docs.python-requests.org/zh_CN/latest/user/quickstart.html requests库 虽然Python的标准库中 urlli ...
- Markdown编辑器语言——30分钟入门到到精通
一.简要说明 开篇说明 其实吧这是我人生中写的第一篇博客,我也不知道怎么排版和编辑让博文显示的更加美观,现在正在学Markdown编辑语法,也是刚刚学编程的一个小菜鸟,目前是大二的在校生,我的初衷是把 ...
- (文章转载)MCI编程
使用MCI API,源文件中需要包含头文件 Mmsystem.h,在Project->Settings->Link->Object/libray module中加入库 Winmm.l ...
- 20155212 C语言实现linux下pwd命令的两种方法
20155212 C语言实现linux下pwd命令的两种方法 学习pwd命令 通过man pwd命令查看 pwd [OPTION],一般不加参数 -P显示当前目录的物理路径 -L显示当前目录的连接路径 ...
- 2017-2018-1 20155220 《信息安全系统设计基础》课下实践——实现mypwd
学习pwd命令 输入pwd命令 于是man 1 pwd查看pwd详细 然后查看pwd实现需要的系统调用man -k; grep 在这发现了一个功能相同的内核函数getcwd 到这步就很简单了,先查看这 ...