自学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 ...
随机推荐
- 使用VS Code发布博客
使用VS Code 发布文章 这也是学习别人怎么去使用VS Code 发布文章 上传图片 这是我上传的图片 通过插件的方式上传 ctrl+alt+aQQ截图 使用插件 Markdown All in ...
- 目前.NET Core创建Windows Service比较好的一个开源框架:DasMulli.Win32.ServiceUtils
新建一个.NET Core控制台程序,搜索并下载Nuget包:DasMulli.Win32.ServiceUtils GitHub 链接及使用指南 Write a windows service us ...
- 内部元素一一相应的集合的算法优化,从list到hashmap
说是算法优化,基本上是在吹牛,仅仅只是算是记录下,我写代码时候的思路.毕竟还是小菜鸟. 我要开一个party,与会者都是情侣,可是情侣并非一起过来的,而是有先有后,可是每位与会者来的时候都拿着一束鲜花 ...
- jQuery,js如何扩展自定义方法
(jQuery.fn.myMethod=function () { alert('myMethod'); }) (function ($) { $.fn.extend({ myMethod : fun ...
- jQuery 学习笔记:jQuery 代码结构
jQuery 学习笔记:jQuery 代码结构 这是我学习 jQuery 过程中整理的笔记,这一部分主要包括 jQuery 的代码最外层的结构,写出来整理自己的学习成果,有错误欢迎指出. jQuery ...
- day 84 Vue学习四之过滤器、钩子函数、路由、全家桶等
本节目录 一 vue过滤器 二 生命周期的钩子函数 三 vue的全家桶 四 xxx 五 xxx 六 xxx 七 xxx 八 xxx 一 Vue的过滤器 1 moment.js 在这里我们先介绍一个 ...
- 百度地图API和高德地图API资料集锦
[高德地图API]从零开始学高德JS API(五)路线规划——驾车|公交|步行 [高德地图API]从零开始学高德JS API(四)搜索服务——POI搜索|自动完成|输入提示|行政区域|交叉路口|自 ...
- c++ 二叉树的遍历
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> ...
- BZOJ 2818 GCD 素数筛+欧拉函数+前缀和
题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=2818 题意:给定整数N,求1<=x,y<=n且Gcd(x,y)为素数的数对( ...
- Assert.notNull(sessionUser);
rg.springframework.util.Assert Assert翻译为中文为"断言".就是断定某一个实际的值就为自己预期想得到的,如果不一样就抛出异常.